Linux Touch Command: What It Is + Examples of How to Use It to Modify Timestamps
People usually associate the touch command in Linux with creating files. However, like many other Linux commands, it offers much broader functionality.
For example, Linux VPS users can use the touch command to change the timestamps of files or folders.
In this tutorial, we’ll go over the Linux touch command and talk about its options and use cases.
Linux Touch Command Options
Linux Touch Command Syntax
Important! Before proceeding, make sure to connect to your VPS via an SSH client such as Putty.
The syntax of the touch command is:
touch [options] [file_name]
Check out the table below for all the touch command options, also known as flags:
Flag | Use Case |
-a | Change the access time |
-m | Change the modification time |
-c | Prevent creating a new file |
-h | Change the symbolic link timestamp |
-h | Change the timestamp for symbolic links |
-t <stamp> | Modify the timestamp. In this case, <stamp> follows the date-time format |
-d=<string> | Change the timestamp based on the date string |
-r=<file> | Change the timestamp based on the reference file |
-v or –version | Display the touch command version |
–help | Display the help menu |
File Timestamps
In Linux, every file and folder has a timestamp that shows when a file’s content or attributes were modified. There are three types of timestamps:
- Access time (atime) – last time a file was read.
- Modification time (mtime) – last time a file’s content was modified. Like access time, it is also part of the file status metadata.
- Changed time (ctime) – last time a file’s metadata was changed. For example, permissions.
As a result, the Linux touch command is mainly used to manipulate file or folder access and modification time.
Keep in mind that there is no way to set or change ctime manually. Since atime and mtime are part of a file’s status metadata, changing atime or mtime of the file results in ctime, which is automatically set to the current time.
Linux touch Command Examples
Here are a few useful examples of the touch command on a Linux system.
Using touch to Create a File
If you use the touch command without any options, it will simply create a new empty file. If the file already exists, the touch command will update the access and modification times to the current time without changing the file contents.
touch file_name.txt
Using touch to Create Multiple Files
It is also possible to create multiple files using a single touch command. To do that, specify the names of the files with spaces between them. It would 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:
touch file_name{1..3}.txt
The above touch command will create three files named file_name1.txt, file_name2.txt, and file_name3.txt.
Using touch to Change Access Time
To change the access time of a file to the current time, use the a option followed by the file name with the touch command like in the following example:
touch -a file_name.txt
Then, check the access time with the following command:
ls -lu file_name.txt
Using touch to Change Modification Time
The m option, along with the touch command, changes the modification time of a file to the current time:
touch -m file_name.txt
Using touch to Change Access and Modification Time
To change both access time and modification time with a single command, use the options a and m together:
touch -am file_name.txt
Now, check the date with both of these commands:
ls -l file_name.txt ls -lu file_name.txt
Using touch to Change Access Time Without Creating a New File
In some situations, you want to change an existing file’s access and modification time to the current time without actually creating a new one. To do that, use the c option followed by the file name with the touch command.
touch -c new_file.txt
Using touch to Set Specific Access and Modification Time
It is also possible to set a file’s access and modification time to a particular date by using the t option followed by a date-time. It would look like this:
touch -t 202203081047.30 file_name.txt
Make sure to check whether the date changed with the following command:
ls -lu file_name.txt
Remember that the date-time format must follow the CCYYMMDDhhmm.ss style:
- CC – the first two digits of the year
- YY – the second two digits of the year
- MM – the month of the year (01-12)
- DD – the day of the month (01-31)
- hh – the hour of the day (00-23)
- mm – the minute of the hour (00-59)
- ss – the second of the minute (00-59)
Using touch to Change Timestamp Using a Symbolic Link
When you use a symbolically linked file name with the Linux touch command, the timestamp information for the original file, that was 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 SymbolicLinkFile
Using touch to Set Timestamp Using a Reference File
The Linux touch command can also set a file’s access and modification time 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
Using touch to Specify the Date and Time as a String
You can also specify the date and time as a string by using the d option. The following Linux touch command example sets the date to the 8th of 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 select the time as one. In that case, the date will be set to the current date automatically:
touch -d '20:10' file_name.txt
Conclusion
Manipulating access, modification, and changing times for files and folders can be helpful for any Linux user. That is where the touch command comes in.
In this tutorial, we’ve covered the usage of the Linux touch command and included the most commonly used options for it. We’ve also provided a few use cases for this command that you can try on your system.
If you have any questions, let us know in the comments section below.
Learn More Linux Commands for File Management
How to Remove Files and Directories
How to Locate a File
How to Compress a File with Tar Command
How to Change File Ownership with Chown Command
How to Unzip Files in Linux
How to Change FIle Permissions with Chmod Command
How to Rename a File
How to Check File Type
How to Create a Symbolic Link (Symlink)
How to Synchronize Files Using rsync Command