1. Overview

In this tutorial, we’ll learn about the touch command. In short, this command allows us to update the last modified time and last accessed time of a file or directory.

So, we’re going to focus on how to use this command and its various options.

Note that we tested all the commands shown here using Bash; however, they should work with any POSIX-compliant shell.

2. Default Behavior

By executing touch, one or more files or directories on the filesystem are updated such that their last modified and their last accessed times are set to the current system time.

So, let’s say today’s date is February 1st, 2020, and the time is 7:00 am.

As described, the command will update both timestamps of the example-file.txt file:

# ls -l example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 20:00 example-file.txt

# touch example-file.txt

# ls -l example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-file.txt

2.1. Creating a New File

Additionally, when the file specified doesn’t exist, the command will create an empty file and set the times accordingly:

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

# touch sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

If a new file is created, the permissions of the newly created file will default to the standard permissions of the given filesystem.

2.2. With Multiple Files or Directories

If multiple files or directories are specified, the command will update the timestamps of all of them:

# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 baeldung  baeldung  0 Jan  1 22:00 example-dir
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 20:00 example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan  1 16:00 sample-file.txt

# touch example-file.txt sample-file.txt example-dir

# ls -l example-file.txt sample-file.txt example-dir
drw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-dir
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 example-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

3. Defining a Specific Time

Optionally, we can define the timestamp to set using the -t option. As a result, this changes the default behavior of using the system time and instead uses the time specified by the option’s settings. The format used to specify time with this option is “[[CC]YY]MMDDhhmm[.SS]“:

  • CC: the century
  • YY: the second two digits of the year; if YY is specified, but CC is not, a value for YY between 69 and 99 results in a CC value of 19; otherwise, a CC value of 20 is used
  • MM: the month of the year (01-12)
  • DD: the day of the month (01-31)
  • hh: the hour of the day (00-23)
  • mm: the minute of the hour (00-59)
  • SS: the second of the minute (00-59)

So, to update a file or directory using our defined timestamp, we can execute:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt
 
# touch -t 202001262359.59 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 26 23:59 sample-file.txt

When CC and YY are not specified, they default to the current year:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# touch -t 01282359.59 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 28 23:59 sample-file.txt

If SS is not specified, the value defaults to 0.

4. Adjusting Times

Another alternative to the default behavior is to change the time relatively instead of setting it explicitly or using the system time. Using -A allows us to adjust the timestamp of a file or directory relative to its current timestamps. The format used to specify the time adjustment is “[-][[hh]mm]SS“:

  • -: makes the adjustment negative
  • hh: the number of hours (00-99)
  • mm: the number of minutes (00-59)
  • SS: the number of seconds (00-59)

So, to adjust the access time by -25 hours, we execute:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 27 22:59 sample-file.txt

# touch -A -250000 sample-file.txt

# ls -l sample-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Jan 26 21:59 sample-file.txt

5. Other Options

5.1. Using the Timestamp From a Reference File

The -r option uses timestamps from a specified reference file as the timestamps to use when updating. Here, running the command with -r takes the timestamps from example-file.txt and applies them to sample-file.txt:

# ls -l example-file.txt sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 17:00 example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 22:45 sample-file.txt

# touch -r example-file.txt sample-file.txt

# ls -l example-file.txt sample-file.txt
-rw-r--r-- 1 baeldung baeldung 0 Feb 1 17:00 example-file.txt 
-rw-r--r-- 1 baeldung baeldung 0 Feb 1 17:00 sample-file.txt

5.2. Working With Symbolic Links

If we execute the command on a symbolic link to a file or directory, the timestamps of the link target are updated:

# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 22:00 example-file.txt
lrw-r--r--  1 baeldung  baeldung  0 Jan 23 22:30 link-to-example-file.txt -> example-file.txt

# touch example-file-link.txt

# ls -l example-file.txt link-to-example-file.txt
-rw-r--r--  1 baeldung  baeldung 0  Feb  1 07:00 example-file.txt
lrw-r--r--  1 baeldung  baeldung 0  Jan 23 22:30 link-to-example-file.txt -> example-file.txt

With -h, we can update the timestamps of the symbolic link to a target file or directory and not the target itself.

5.3. Preventing File Creation

We can disable file creation with -c. When executed with this option, if the file doesn’t exist, the command does nothing:

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

# touch -c sample-file.txt

# ls -l sample-file.txt
ls: sample-file.txt: No such file or directory

5.4. Updating Last Accessed Time Only

In case we only want to update the last accessed time of a file or directory, we can use the -a option:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt  

# touch -a sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

As described previously, the default behavior is to update both the last accessed time and the last modified time. Using this option overrides that default behavior of setting both timestamps. Using -a and -m together results in the default behavior of updating both timestamps.

5.5. Updating Last Modified Time Only

Similarly, in the case where we only want to update the last modified time of a file or directory we can use the -m option:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# ls -l -u sample-file.txt 
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

# touch -m sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb  1 07:00 sample-file.txt

# ls -l -u sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 20 19:00 sample-file.txt

6.  Compatibility

To maintain backward compatibility with touch on older BSD systems, a few obsolete features and options are still supported.

The command still supports an older, obsolete version of touch. It allows us to specify the time format first in the format “MMDDhhmm[YY]” followed by the name of the file or directory to update on the filesystem.

The MM, DD, hh and mm letter pairs are treated like their counterparts specified by the -t option. If the YY is between 39 and 99, the year is in the 20th century. Otherwise, the year is in the 21st century:

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Feb 10 12:00 sample-file.txt

# touch 0123000020 sample-file.txt

# ls -l sample-file.txt
-rw-r--r--  1 baeldung  baeldung  0 Jan 23 00:00 sample-file.txt

The command once had the ability to force updates to files using the -f option. This option, however, is now ignored.

7. Conclusion

In this article, we explored the touch command-line utility, its various options, and its backward compatibility.

In conclusion, touch is handy when we do not have any changes to make to a file, but we want to update its last accessed or its last modified time.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments