May 092015
 

Touch is a useful command to be aware for those new to Linux.

System administrators use touch to quickly create empty files.

But that’s not the only use for the touch command.

Linux and Unix administrators routinely use touch to change timestamp of files.

So what are timestamps?

In the Unix and Linux environments, all files are associated with timestamps. Timestamps provide information about files such as their last access, modification and change time.

Timestamps are valuable in compiling source code, used in scripts, creating backups and for applications deployed or used across multiple time zones.

Also, commands like ls and find leverage timestamps for listing and finding files respectively.

With that brief introduction to touch, let’s consider a few key options for touch and then follow it up with some examples.

Key Touch Command Options

-a, change access time only
-c, if file does not exist, do not create it
-d, update access and modification times
-m, change modification time only
-r, use access and modification times of file
-t, creates a file using a specified time

Touch Illustrations

1. Create an Empty File

The most frequent use of touch is to quickly create an empty file.

Let’s start with an example.

$ touch Example.txt

Voila, you now have a file called Example.txt.

2. Create Multiple Files

You can also create multiple files in one go using touch.

$ touch Example-1.txt Example-2.txt Example-3.txt Example-4.txt

You’ll have four new empty files – Example-1-4 in your directory.

3. Change File Access Time

You can change access time of a file using -a option.

Here’s an illustration.

We’ll first consider our Example.txt file before the touch -a command is issued and then after it’s executed.

michael@sasha ~/Desktop $ stat Example.txt
  File: ‘Example.txt’
  Size: 57        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 121375543   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/michael)   Gid: ( 1000/michael)
Access: 2015-05-08 19:59:01.849472115 -0400
Modify: 2015-05-08 19:59:00.841472089 -0400
Change: 2015-05-08 19:59:00.841472089 -0400
michael@sasha ~/Desktop $ touch -a Example.txt

After issuing the above touch -a command, you’ll see that the access time is changed).

$ stat Example.txt
  File: ‘Example.txt’
  Size: 57        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 121375543   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/michael)   Gid: ( 1000/michael)
Access: 2015-05-08 20:07:07.349484546 -0400
Modify: 2015-05-08 19:59:00.841472089 -0400
Change: 2015-05-08 20:07:06.337484520 -0400

4. Modify Timestamp

To change modification time of a file, we use the -m option.

Again, we’ll look at the Example.txt file before and after running the touch -m option.

michael@sasha ~/Desktop $ stat Example.txt
  File: ‘Example.txt’
  Size: 57        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 121375543   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/michael)   Gid: ( 1000/michael)
Access: 2015-05-08 20:07:07.349484546 -0400
Modify: 2015-05-08 19:59:00.841472089 -0400
Change: 2015-05-08 20:07:06.337484520 -0400

Let’s run the -m option now.

michael@sasha ~/Desktop $ touch -m Example.txt

Let’s look at the file again with the stat command.

michael@sasha ~/Desktop $ stat Example.txt
  File: ‘Example.txt’
  Size: 57        	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 121375543   Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/michael)   Gid: ( 1000/michael)
Access: 2015-05-08 20:35:15.613527774 -0400
Modify: 2015-05-08 20:35:14.601527748 -0400
Change: 2015-05-08 20:35:14.601527748 -0400

You can change modification time of multiple files simultaneously.

In the following example, we’re changing modification time of all HTML files.

$ touch -m *.html

As we’ve seen above, both -a and -m options change file timestamp into current time.

To change a file with a custom timestamp, we must use a different option.

5. Custom Timestamp

For creating custom timestamp (i.e. other than using than the default current time), we’ll go with the -t option.

-t option
[[CC]YY]MMDDhhmm[.ss] instead of current time

CC – First two digits of the year
YY – Second two digits of the year
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-61]

touch -t Example.txt

6. Avoid Creating New File

When you use the -c option with touch, you can avoid creating new files.

For instance, the below command will not create a file called Example.txt if it does not exist.

$ touch -c Example.txt

7. Use Time Stamp of Another File

When you use the -r option, touch will change timestamp of one file with that of another.

Below, we’re changing the timestamp of file Washington with the timestamp of file Example.txt.

Go ahead, try it out.

$ touch -r Example.txt Washington

8. Change to Specified Date

When you use touch with -d option, you can change the date of the file.

$ touch -d 18-October-2012 Example.txt

Note: For some reason, a few of the touch options did not work on my Linux Mint system but ran smoothly on the CentOS 7 box.

Related touch Content
What are the legitimate uses of the `touch` command?

Sorry, the comment form is closed at this time.