Touch Command in Linux/Unix with Examples

November 28, 2021

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".

touch to create empty file if non-existent

Touch Creating an Empty File

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:

linux touch changing timestamp

Linux Touch Command Changing Timestamp of an Existing File

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.
Triple Dots (Ellipsis) in Man Pages

How to Change Only the Access Time of a File

Syntax:

~$ touch -a FILE

Example:

touch -a Changing Only the Access Time

touch -a Changing Only the Access Time

How to Change Only the Modification Time of a File

Syntax:

~$ touch -m FILE

Example:

touch -m Changing only the Modification Time

touch -m changing only the modification mime

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:

  1. Using touch -t
  2. Using touch -d or touch --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).

Touch -t STAMP using Given Time instead of Current Time

_touch -t_ STAMP using Given Time instead of Current Time

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:

Touch -d in Action

touch -d taking argument in human-readable form.

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:

Touch Changing linked file instead of the link itself

Touch changing linked file's timestamp instead of the link itself.

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:

touch --no-dereference changing the link's timestamp directly

_touch --no-dereference_ changing the link's timestamp directly

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:

touch -r in action

touch -r in action

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:

touch -c in action

touch -c in action

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.