Read in 4 minutes
Linux Touch Command
The touch command allows us to update the timestamps on existing files and directories as well as creating new, empty files.
In this guide, we will show you how to use the touch command through practical examples and detailed explanations of the most common command options.
Linux Files Timestamps
Before going into how to use the touch command, let’s start by reviewing the file timestamps in Linux.
A file in Linux has three timestamps:
- atime (access time) - The last time the file was accessed/opened by some command or application such as cat, vim or grep.
- mtime (modify time) - The last time the file’s content was modified.
- ctime (change time) - The last time the file’s attribute or content was changed. The attribute includes file permissions, file ownership or file location.
To display the file status including the timestamps you can use the
How to Use the touch Command
In it’s simplest form when used without any options, if the file name specified as argument doesn’t exist touch will create a new file.
If the file already exist touch will change the file last access and modification times to the current time.
For example, if the file
file1 doesn’t exist the following command will create it otherwise it will change its timestamps:
To create od modify multiple files or change at once, specify the file names as arguments:
touch file1 file2 file3
If you don’t want the touch command to create new files, use the
For example, the following command will change the file
file1 timestamps if it exist, otherwise it will do nothing:
touch -c file1
Changing only access or modification times
By default if no option is used touch will update the file last access and modification times to the current time. By using the
-m options, you can change only one of these timestamps.
Change only the access time
-a option to change only the file’s access time:
touch -a file1
Change only the modify time
-m option to change the file’s modify time:
touch -m file1
When changing the modify time, the change time will be also updated.
Setting specific timestamps
The touch command also allow us to update or create a file with specific time other than the current time.
--date=) option to specify a date string and use it instead of current time.
For example, to change both the last access and modification times of
file1 to 1 June 2018 11:02 you would use the following command:
touch -d '1 June 2018 11:02' file1
The date string needs to be enclosed in single quotes.
You can also provide a partial date-time string to the touch command. For example providing only the date, automatically changes the year to the current one:
touch -d '12 June' file1
-t option to specify a timestamp and use it instead of current time. The timestamp argument needs to be be in the following format:
For example, the following command will set last access and modification times of
file1 to 1 June 11:02 of the current year.
touch -t 12061102 file1
Using the timestamp of another file
--reference=) option allow us to specify a reference file and use its time stamps instead of current time.
For example, the following command will tell touch to use the times of
touch -r file1 file2
Changing symbolic link timestamp
By default, if you use a touch command on a symbolic link it will change the timestamps of its referenced file.
--no-dereference) to modify the timestamp of the symlink.
For example, to change the timestamps of the symbolic link
symlink1 you would use:
touch -h symlink1
By now you should have a good understanding of how to use the Linux touch command.