Linux touch utility

linux touch utility

How to change the last access time of a file in linux? How do I set the modification time of a file? How to create an empty file on a linux system? We will talk about this in this article, talking about the linux touch utility.

Description of the touch utility

The Linux touch utility is designed to change access time stamps and file modifications. Full documentation can be viewed at this link.

Using the touch command

touch [OPTION] … FILE …

The Linux touch command updates the access and modification timestamps of the FILE files to the current one. Moreover, if the specified file does not exist, then a new empty file will be created. Suppose we have a regular text file test.txt:

$ ls -l
total 4
-rw-rw-r--. 1 user user 2035 Oct 26 14:30 test.txt

Let’s use the touch utility and see not the result:

$ touch test.txt

$ ls -l
total 4
-rw-rw-r--. 1 user user 2035 Oct 26 14:35 test.txt

If we specify touch as an argument a non-existent file, it will be created (empty):

$ touch test2.txt

$ ls -l
total 4
-rw-rw-r--. 1 user user    0 Oct 26 14:41 test2.txt
-rw-rw-r--. 1 user user 2035 Oct 26 14:35 test.txt

Because of this feature of the linux touch utility is often used to create new files.

Touch command options

The Linux touch utility accepts the following options:

-a – changes only the last access time to the file
-c, –no-create – doesn’t create any files if they don’t exist
-d, –date=String – set a timestamp (as a string) that will be used instead of the current time:

$ touch -d '25 Oct 2020 15:00' test.txt

$ ls -l
total 4
-rw-rw-r--. 1 user user    0 Oct 26 14:41 test2.txt
-rw-rw-r--. 1 user user 2035 Oct 25 15:00 test.txt

-h, –no-dereference – change symbolic link timestamps instead of referenced files (only works on systems that can change symbolic link timestamps)
-m – only changes the file modification time
-r, –reference = FILE – use the time of the specified file instead of the current time:

$ touch -r test.txt test2.txt

$ ls -l
total 4
-rw-rw-r--. 1 user user    0 Oct 25 15:00 test2.txt
-rw-rw-r--. 1 user user 2035 Oct 25 15:00 test.txt

-t STAMP – use timestamp [[CC]YY]MMDDhhmm[.ss] instead of current time:

$ touch -t 202010251513 test.txt

$ ls -l
total 4
-rw-rw-r--. 1 user user    0 Oct 25 15:00 test2.txt
-rw-rw-r--. 1 user user 2035 Oct 25 15:13 test.txt

–time=WORD – change only access or modification times. Equivalent to the -a and -m options, except in this case access / atime or modify / mtime is specified instead of WORD
–help – show help
–version – show the version of the utility:

$ touch --version
touch (GNU coreutils) 8.30

You can find even more useful Linux utilities on this page.