April 9, 2019
April 9, 2019
The Linux touch command is mainly used to create empty files, and change timestamps of files or folders. The timestamp information of files consists of three attributes – access time, modification time, and change time.
This tutorial will explain the Linux touch command, its options, and usages while providing useful examples.
In Linux, every file and folder has a timestamp associated with it that provides information about when a file’s content or its attributes were modified. There are three types of timestamps:
Since atime and mtime are part of a file’s status metadata, changing atime or mtime of a file results in ctime which automatically set to the current time. There is no way to set or change ctime manually. The Linux touch command is mainly used to manipulate the access and modification time of files by using various options as described below. Remember, before using the touch command you need to access your VPS using SSH!
The syntax of the touch command is:
touch [options] [file_name]
The following section lists the usages of the Linux touch command by including each options.
The touch command without any options creates a new file. If the file exists, the touch command will update the access and modification to the current time without changing its content:
It is also possible to create multiple files by using a single touch command. To do that, specify the names of the files with spaces between them. It woul look like this in the command line:
touch file_name1.txt file_name2.txt file_name3.txt
You can auto generate file names using curl braces while creating multiple files like in the following example:
The above touch command will create three files named file_name1.txt, file_name2.txt, and file_name3.txt.
To change the access time of a file to the current time, use the a option followed by the file name with touch command like in the following example:
touch -a file_name.txt
The m option along with the touch command changes the modification time of a file to the current time:
touch -m file_name1.txt
To change both access time and modification time with a single command use the options a and m together:
touch -am file_name1.txt
In some situation, you want to change the access and modification time of an existing file to the current time without actually creating a new file. To do that use the c option followed by the file name with touch command.
touch -c file_name.txt
It is also possible to set access and modification time of a file to a particular date by using t option followed by datetime. It would look like this:
touch -t 201903081047.30 file_name.txt
The datetime format must be in CCYYMMDDhhmm.ss where:
When you use a symbolically linked file name with the Linux touch command, the timestamp information for the original file i.e the file which is pointed by the link file gets modified. To change the access and modification time to the current time for a symbolically linked file, use the h option:
touch -h symbolic_link_file
The Linux touch command can also set the access and modification time of a file by reading the timestamp information from another file. For example, the following touch command with the r option will scan the timestamp information from reference.txt and set these timestamp values to file_name.txt. Here’s an example of the command:
touch -r reference.txt file_name.txt
You can also specify date and time as a string by using the d option. The following Linux touch command example sets the date to 8th March and the time is automatically set to 00:00
touch -d '8 Mar' file_name.txt
Instead of specifying the date as a string, you can specify the time as a string. In that case, the date will be set to the current date automatically:
touch -d '20:10' file_name.txt
This tutorial covers the usages of the Linux touch command by including the most common options. For any difficulties related to the Linux touch command, you can invoke its manual page in the terminal!