How To Use Touch Command.
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.
Creating a new file requires write permissions on the parent directory. Otherwise, you will receive a permission denied error.
How to Use the touch Command
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:
To create or modify multiple files at once, specify the file names as arguments:
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:
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 -a and -m options, you can change only one of these timestamps.
Change only the access time
Use the -a option to change only the file’s access time:
Change only the modify time
Use the -m option to change the file’s modify time:
When changing the modify time, the change time will be also updated.
Setting specific timestamps
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:
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:
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:
For example, the following command will set the last access and modification times of file1 to 1 June 11:02 of the current year.
Using the timestamp of another file
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:
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.
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:
By now you should have a good understanding of how to use the Linux touch command.