In Linux/Unix, the touch command is used to change the "modify-time" and "access-time" of a file without modifying the contents of the file. If the file does not exist, it is created.
But before I start, you should know a little bit about these terms. Modify Time is the last time the content in the file was changed. On the other hand, the access time is the last time the file was read. I will be using the stat command to find these.
Now, we will understand the application of the touch command using a few examples.
How to Create an Empty File
If you have followed my recent post of setting up URxvt, you might have seen that I created an empty file ~/.Xresources
before symlinking it to ~/.Xdefaults
. There are many such real-life examples/commands where a file needs to exist before these commands can be executed. And in this scenario, we use touch commands to create the empty file.
Syntax:
~$ touch NON-EXISTANT-FILE
For the example shown below, there is no such file called empty.txt
but as soon as I execute the touch empty.txt
command, I create it. You can also notice its size as "0".
How to Change the Timestamps of an Existing File
To set the timestamp to the current time, we follow the following Syntax:
~$ touch EXISTING-FILE
Example:
How to Change Times of Multiple Files Together
Syntax:
~$ touch FILE1 FILE2 FILE3
The command is the same as touch FILE1 except that all three files are given the same timestamps.
😃 Fun Fact: In any man page, "triple dots" (...) means "repeatable argument". You can have these arguments as many times as you want.
How to Change Only the Access Time of a File
Syntax:
~$ touch -a FILE
Example:
How to Change Only the Modification Time of a File
Syntax:
~$ touch -m FILE
Example:
All of the above touch
commands were putting a current
timestamp on an existing file. What if we want to put a timestamp other than the current one?
How to Put a Given Timestamp instead of Current Time on a File
Two ways are possible:
- Using
touch -t
- Using
touch -d
ortouch --date
1. Using touch -t
Syntax:
~$ touch -t STAMP FILE
The above command assumes timestamp in the format of CCYYMMDDhhmm.ss
Example:
~$ touch -t 202103040100.10 ~/.Xresources
Here,
CCYY
=2021
MMDD
=0304=4 March
hhmm.ss
=0100.10=01 hour 00 min 10 sec
If you omit CCYY
, it becomes the current year. You can omit CC as well but I prefer not to do that because it is more confusing and not sustainable (for more, see info touch command).
2. Using touch -d
Syntax:
~$ touch -d STRING FILE
Here, touch parses the STRING in human-readable form like that in the date -d command.
| -d STRING | Meaning |
| --- | --- |
| -d '14:02'
| 14:02 @ Today |
| -d 'yesterday'
| current time @ yesterday |
| -d 'today'
| current day |
| -d 'next sunday'
| upcoming Sunday |
| -d 'tomorrow
‘ | current time but tomorrow |
| -d 'June 15, 2004'
| June 15 2004 @ 12:00:00 AM |
| -d '@1619076682
‘ | Thu Apr 22 01:01:22 PM IST 2021; explained below |
| -d '00:03:00'
| 00:03:00 @ Today |
| -d '-1 hour'
| 1 hour ago |
| -d '+1 hour'
| 1 hour later |
Example:
How to Change Timestamps of Links
By default, the touch command changes the timestamps of the linked file instead of the symlink itself as can be seen from the following example:
However, if we like to change the timestamp of the link instead we need to use the --no-dereference
(or -h
) flag.
Syntax:
~$ touch --no-dereference LINK-FILE
Continuing with the same example:
How to Assign Timestamp of Another File
Syntax:
~$ touch --reference=REFERENCE-FILE FILE
Here the touch --reference
(or -r
) command will impose REFERENCE-FILE's timestamp on the FILE.
Example:
How to Use Touch Command without Creating Any File
By default, touch creates an empty file if the file does not exist. To avoid this, use --no-create
(or -c
) syntax:
~$ touch --no-create FILE
Example:
Wrapping Up
I have tried my best in covering up everything about the touch command in the Linux/Unix system. But if you are still dissatisfied, please comment below. In that case, you can also dig in man touch and info touch commands. I have also tried my best to remove all kinds of errors. But if you find any, please let me know in the comment section. It will also help the Linux Community.