Our Knowledge Base provides step-by-step guides, troubleshooting tips, and expert insights to help you manage VPS, dedicated servers, domains, DDoS protection, and more — all designed to make your experience with us fast, secure, and stress-free.
In this article we will show you hou to use touch command.
Before going into how to use the touch command, let’s start by reviewing the file timestamps.
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, use the stat command.
stat file_name
Creating a new file requires write permissions on the parent directory. Otherwise, you will receive a permission denied error.
In it’s simplest form when used without any options, if the file name specified as an argument doesn’t exist touch will create a new file.
If the file already exists 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:
touch file1
To create or modify multiple files 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 -c (–no-create) option.
For example, if the file file1 exist the following command will change the file timestamps otherwise, it will do nothing:
touch -c file1
By default, if no option is used touch will update the file last access and modification times to the current time. By using the -a and -m options, you can change only one of these timestamps.
Use the -a option to change only the file’s access time:
touch -a file1
Use the -m option to change the file’s modify time:
touch -m file1
When changing the modify time, the change time will be also updated.
The touch command also allows us to update or create a file with a specific time other than the current time.
Use the -d (–date=) option to specify a date string and use it instead of the 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. Providing only the date, automatically changes the year to the current one:
touch -d '12 June' file1
Use the -t option to specify a timestamp and use it instead of the current time. The timestamp argument needs to be in the following format:
use [[CC]YY]MMDDhhmm[.ss]
For example, the following command will set the last access and modification times of file1 to 1 June 11:02 of the current year.
touch -t 06011102 file1
The -r (–reference=) option allow us to specify a reference file and use its timestamps instead of the current time.
For example, the following command will tell touch to use the times of file1 for file2:
touch -r file1 file2
By default, if you use a touch command on a symbolic link it will change the timestamps of its referenced file.
Use the -h (–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.