1. Overview
Sometimes, we require empty files. In other cases, we need a quick way to change metadata such as dates for an existing file.
In this tutorial, we’ll learn about the touch command. One application of this command is to update the last modified time and last accessed time of a file or directory. Further, if a file doesn’t exist, touch can create it.
In particular, we’re going to focus on how to use the command and its various options.
Notably, 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. We assume the current system time is 7:00 AM on the 1st of February 2020.
Before using the touch command to alter the timestamps of a file, let’s check the timestamps of an already existing file:
$ ls -l example-file.txt
-rw-r--r-- 1 baeldung baeldung 0 Jan 1 20:00 example-file.txt
The ls command lists the information about the specified file, in our case, example-file.txt. The -l option specifies a long listing format, which, by default, shows the last modified time along with other details.
The touch command updates both the last accessed and last modified timestamps of the example-file.txt file:
$ touch example-file.txt
$ ls -l example-file.txt
-rw-r--r-- 1 baeldung baeldung 0 Feb 1 07:00 example-file.txt
We can also check a file’s last accessed and modified timestamps using the stat command.
2.1. Creating a New File
Additionally, when the specified file doesn’t exist, touch creates an empty file and sets 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 default to the standard permissions of the given filesystem.
2.2. With Multiple Files or Directories
If multiple files or directories are specified, the touch command updates 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
As expected, the dates change for each object.
3. Defining a Specific Time
Optionally, we can define the timestamp we want to set via 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: century
- YY: 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: month of the year (01-12)
- DD: day of the month (01-31)
- hh: hour of the day (00-23)
- mm: minute of the hour (00-59)
- SS: second of the minute (00-59)
So, let’s see an example of updating a file or directory using a predefined timestamp:
$ 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 isn’t specified, the value defaults to 0.
4. Adjusting Times
Another alternative to the default touch behavior is to change the time relatively instead of setting it as an absolute time or using the system time.
Using -A enables us to adjust the timestamp of a file or directory relative to its current value. The format used to specify the time adjustment is [-][[hh]mm]SS:
- -: makes the adjustment negative
- hh: number of hours (00-99)
- mm: number of minutes (00-59)
- SS: number of seconds (00-59)
So, let’s adjust the access time by -25 hours:
$ 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
As expected, the resulting timestamp changes by exactly 25 hours.
5. Other Options
Now, let’s explore some additional options that the touch command provides.
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
At this point, both files are in sync.
5.2. Working With Symbolic Links
If we execute the touch 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:
$ 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
When executed with this option, if the file doesn’t exist, the command does nothing
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 the 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
Now, we only see the change in the modified time.
6. Compatibility
To maintain backward compatibility with the touch command in older BSD systems, a few obsolete features and options are still supported.
For example, we can first specify the time 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 could once 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.
Simply put, touch is handy when we don’t need to make any changes to a file but want to update its last accessed or modified time.