What is the Touch Command?
As we progress through our GNU Core Utilities series, we move on to the touch command. The touch command in Linux is used to manipulate the timestamp associated with files. With it you can change the modification time, access time, or even copy another files timestamps. The touch command is also often used just to create an empty file. Below we will show some examples of how to use the touch command.
Create an Empty File with Touch
To create an empty file, you simple run the touch command and specify a filename after it.
$ touch myfile.txt
This creates a empty file named my file. Let's take a look at the file and notice the timestamp which marks the time the file was created.
$ ls -l myfile.txt
-rw-rw-r--. 1 savona savona 0 Feb 1 12:01 myfile.txt
Timestamp formats
There are several formats you can use while manipulating timestamps.
Stamp Format
The STAMP uses a rigid format which can be used the with -t option.
touch -t [[CC]YY]MMDDhhmm[.ss]
- CC = First two digits of the year
- YY = Last two digits of the year
- MM = Month
- DD = Specifies the Day (If less that 10 use leading zero ie. 05 for May)
- hh = Hour (If less than 10 use leading zero)
- mm = Minutes (If less than 10 use leading zero)
- ss = Seconds (If less than 10 use leading zero)
As an example this is what it would look like to change a file to October 8th 2018, at 07:30 and 30 seconds.
$ touch -t 201810080730.30 myfile.txt
String Format
The STRING format a free format human readable date string. Below are some of the options you can use with the string format.
$ touch -d "Fri, 1 Feb 2019 18:30:00 -0500" myfile.txt
$ touch -d "2019-02-01 18:30" myfile.txt
$ touch -d "last Friday" myfile.txt
Changing a File Timestamp
You can use touch to modify two different timestamps of a file. If you do not specify which you want to modify, the will both be changed.
Modifying the Access Time
The access time or atime of a file is changed whenever a file it opened for reading. If we want to use touch to modify the atime only, we must specify the -a option.
Using the example above, we created a blank file on February 1 at 12:01. We can use the ls command to check the last access time of the file.
$ ls -l --time=atime myfile.txt
-rw-rw-rw-. 1 savona savona 0 Feb 1 12:01 myfile.txt
This shows us that the file has not been read since the file was created. Now let's use the cat command to read the file, and see what happens.
$ cat myfile
$ ls -l --time=atime myfile.txt
-rw-rw-rw-. 1 savona savona 0 Feb 2 11:38 myfile.txt
As you can see the atime attribute was updated because we access the file with cat.
Now we can use the touch command to modify the atime to be whatever we want. The format for setting a timestamp is Let's change it to January 22 2019 at 07:30 like so.
$ touch -a -t 201901220730.00 myfile.txt
Let's use "ls -l ---time=atime" to check the change.
$ ls -l --time=atime myfile.txt
-rw-rw-rw-. 1 savona savona 0 Jan 22 07:30 myfile.txt
We successfully changed the atime of the file. We could have also used the String format like so:
$ touch -a -d "2019-01-22 07:30:00" myfile.txt
Modifying the Modified Time
The mtime or last modified time can easily be modified using the same syntax. If we want to use touch to modify the mtime only, we must specify the -m option. Here are some examples.
Using the stamp format:
$ touch -m -t 201901220730.00 myfile.txt
Using the string format:
$ touch -m -d "2019-01-22 07:30:00" myfile.txt
Copying Another File's Timestamp
You can copy the timestamp from another file using the -r (reference) option.
$ touch oldfile.txt -r myfile.txt
As you can see, this copied the timestamp from oldfile to myfile:
$ ls -l oldfile.txt
-rw-rw-r--. 1 savona savona 0 Jan 26 07:30 oldfile.txt
$ ls -l myfile.txt
-rw-rw-rw-. 1 savona savona 0 Jan 26 07:30 myfile.txt
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
1 Comment
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
I'm using tcsh on a machine running (I think) BSD, and your line above which reads
$ touch oldfile.txt -r myfile.txt
SHOULD read
$ touch -r oldfile.txt myfile.txt