The touch command is a standard utility on Unix-like operating systems. While its primary purpose is to change file timestamps, it is also commonly used to create new, empty files. Let's explore how the linux touch command works.
Creating New Files
The simplest way to create an empty file is by using the touch command followed by a filename. If the file does not exist, touch will create it for you. This is a fundamental bash touch operation for scripting and daily tasks.
touch mysuperduperfile
After running this command, a new empty file named mysuperduperfile will appear in your current directory. You can create multiple files at once by listing their names.
touch file1.txt file2.txt file3.log
Updating File Timestamps
The original function of the touch command in linux is to update the access and modification timestamps of a file or directory. If you use touch on an existing file, it will update its timestamps to the current time.
You can verify this by using ls -l to check a file's timestamp, running touch on it, and then checking again.
# Check the original timestamp
ls -l mysuperduperfile
# Update the timestamp
touch mysuperduperfile
# Check the new timestamp
ls -l mysuperduperfile
Advanced Timestamp Control
The linux touch command also provides options for more precise timestamp manipulation.
Using a Reference File
The linux touch -r option allows you to set a file's timestamp to match that of another file (a reference file). This is useful for synchronizing timestamps across related files.
# Set file2.txt's timestamp to match file1.txt's timestamp
touch -r file1.txt file2.txt
Setting a Specific Date
With the touch -d option, you can set a file's timestamp to a specific date and time. The touch -d linux functionality accepts various string formats for the date.
# Set the timestamp to a specific date and time
touch -d "2023-01-01 12:30:00" mysuperduperfile
Mastering touch is a great step in learning to manage your file system efficiently from the command line.