2024-02-08
touch do?Primarily, touch creates empty files. If a file with the specified name already exists, touch updates its last access and modification timestamps to the current time. This seemingly simple function has surprisingly versatile applications.
The most basic usage of touch is creating a new, empty file. Let’s say you want to create a file named my_new_file.txt:
touch my_new_file.txtThis command instantly creates my_new_file.txt in your current directory. You can verify its existence using the ls command:
ls -l my_new_file.txtThis will display detailed information about the file, including its size (which will be 0 bytes for a newly created empty file).
touch can efficiently create multiple files simultaneously. To create file1.txt, file2.txt, and file3.txt:
touch file1.txt file2.txt file3.txtIf a file already exists, touch updates its timestamps without altering its contents. Let’s create a file and then use touch to update its timestamps:
echo "Hello, world!" > my_file.txt
ls -l my_file.txt #Observe initial timestamps
touch my_file.txt
ls -l my_file.txt #Observe updated timestampsNotice the change in the “modified” and “accessed” timestamps after the second touch command.
touch also allows you to set specific timestamps using the -t option. The format is YYYYMMDDHHMM.SS. For example, to set the timestamp of my_file.txt to January 1st, 2024, at 10:00:00 AM:
touch -t 202401011000 my_file.txtNote that the seconds (SS) are optional.
-c Option (No Create)The -c option prevents touch from creating a new file if one doesn’t already exist. This is useful for only updating timestamps:
touch -c non_existent_file.txt # No error, no file created
touch -c my_file.txt # Timestamps of my_file.txt updatedUsing -c with a non-existent file results in no action, and no error message.
-r Option (Reference Timestamp)The -r option allows you to copy the timestamps from a reference file. To copy the timestamps of source_file.txt to destination_file.txt:
touch source_file.txt # Create source file
touch destination_file.txt # Create destination file
touch -r source_file.txt destination_file.txtNow destination_file.txt will have the same timestamps as source_file.txt.
The touch command’s simplicity makes it incredibly useful within shell scripts for automating file management tasks, such as creating temporary files, or managing log files with specific timestamps. Its ability to silently update timestamps without changing content makes it an essential tool for any Linux user.