A Comprehensive Guide To Linux touch Command

By | 18/03/2013

Have you ever been caught in a situation where, through command line, you had to backup or delete files that were created in a specified time period?

Have you ever got frustrated when your image viewer program mixed up your latest photographs with old photographs just because your camera messed up with the time stamps of the latest photographs at the time when they were taken?

Or, do you just want to play cool in front of your friends or peers by accessing a file or document even before it was created?

Well, if you have been through the above mentioned or similar situations and want to know how can we use a couple of easy command line utilities in Linux  to solve these problems then it’s time for you to read on.

 

Linux touch command

Linux provides a small command line utility known as touch command. Using this command, you can touch a file to just change it’s time stamps. Also, new files can be created using this command. In Linux, there are mainly three type of time stamps associated with a file.

These are :

Access time               – Changes when file is accessed
Modification time   – Changes when file contents are actually modified
Last change time     – Changes when inode information or the meta data related to file changes.

Now, one may think that why would changes in time stamps of a file be required? Well, there may be several reasons, I’ll start off with an example where on one fine day, one of my friends took my laptop at 2:00pm but claimed that he has already cracked the login password and has already used the laptop at around 9:00am earlier in the morning. I thought he was kidding but he immediately came up with a document (which he was supposed to create and work on) showing that the access and modification time of the document was that of 9:00am in the morning. Well, I was surprised that how could he do this as I never shared login password with him.

I’ll decode the trick that he used to do all this but let’s learn the basic usage of touch command first.

Here is the syntax of touch command :

touch [OPTION]... FILE...

FILE is name of a file.

Now, lets look at an example :

$ ls mylinuxbook
ls: cannot access mylinuxbook: No such file or directory

$ touch mylinuxbook

$ ls mylinuxbook 
mylinuxbook

$

You’ll observe that first I checked if there exists a file named mylinuxbook in the current directory through ls command. After confirming that no such file exists, I used the touch command with mylinuxbook as the argument to it. Next, through ls command I again checked if a file named mylinuxbook is created and this time a file with the same name was indeed created.

Creating new files is helpful specially in cases where an application demands an empty configuration file to be present before it starts writing configuration data to it.

Ok, let’s play with the time stamps of the newly created file now.

$ stat mylinuxbook 
  File: `mylinuxbook'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 546509      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-03-02 18:57:02.793389192 +0530
Modify: 2013-03-02 18:57:02.793389192 +0530
Change: 2013-03-02 18:57:02.793389192 +0530
 Birth: -
$ touch mylinuxbook 

$ stat mylinuxbook 
  File: `mylinuxbook'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 546509      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-03-02 18:58:34.493843902 +0530
Modify: 2013-03-02 18:58:34.493843902 +0530
Change: 2013-03-02 18:58:34.493843902 +0530
 Birth: -

In the exercise above, I used the stat command to check the Access and Modify time of the file mylinuxbook. Next, I used the touch command on the file mylinuxbook and again used the stat command to see the time stamps. You’ll observe that the time stamps changed.

I created the file mylinuxbook at around 18:57:02 on 2013-03-02. Touch(ed) it to change it’s time stamps to the current time ie 18:58:34. Now comes an interesting thought, can I change the various time stamps to a time which was before the file’s creation time? The answer to this thought is YES. This can be done through the -t option provided by the touch command.

$ touch -t 201303020900.01 mylinuxbook 

$ stat mylinuxbook 
  File: `mylinuxbook'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 546509      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-03-02 09:00:01.000000000 +0530
Modify: 2013-03-02 09:00:01.000000000 +0530
Change: 2013-03-02 19:02:12.442924663 +0530
 Birth: -

You’ll observe that the time stamps (Access and Modification) got changed to a time (around 9:00am) that is well before the time (around 7:00pm) at which the file mylinuxbook was actually created.

Can you guess now whether my friend was telling the truth? Yes, you got it right, he was lying. This is the trick that my friend used to fool me. He used touch command to set the time stamps to a time before his document was actually created.

Though I did not easily give up as I had a doubt as to how come the access and modification time stamps are still at around 9:00am in the morning but the change time stamp is showing the current time? The logic behind my question was that if he would have accessed or modified the contents of the document then the access or modification time stamps would also be equal to the current time. So what did he do due to which the change time stamp was updated to current time.

Well, he seemed to have an answer for this too. He said that he renamed the file due to which only the change time stamp was updated. Though logically it is correct that if any meta-data related changes happen to a file then also the change time stamps are updated (while if a file is accessed or modified then the corresponding access and modification time stamps are also updated) but it was enough for me to surrender and ask him that how did he manage to break my login password. :-)

So, this was a little touchy :-) trick through which a friend of mine acted as if he broke pass the login mechanism of my laptop.

When I initially learned this command, I was not much impressed with the way -t option works. This is because I had to write a weird looking long number that is nothing but a representation of the date and time in [YearMonthDayHourMinutes.Seconds] form. I was mostly interested in changing the hour and minutes of the file. Gradually I did find an option that made this task really easy for me. I’ll discuss that later but let’s discuss some details of the representation that I highlighted in bold above.

Here is the syntax of this option :

 -t [[CC]YY]MMDDHHMM[.SS]

In the syntax above:

CC represents first two digits of the year
YY represent last two digits of the year
MM represents month
DD represents day
HH represents hour
MM represents minutes
SS represents seconds

Here is an excerpt from the info page of touch command :

Use the argument (optional four-digit or two-digit years, months, days, hours, minutes, optional seconds) instead of the current time. If the year is specified with only two digits, then CC is 20 for years in the range 0 … 68, and 19 for years in 69 … 99. If no digits of the year are specified, the argument is interpreted as a date in the current year. Note that SS may be `60′, to accommodate leap seconds.

So you will agree that this option is for those users or system administrators that have to precisely mention the changed time stamps up to seconds. I really never had a situation where I had to take care of such precision while changing the time stamps. Within some days of learning this command, I felt a need to have a simpler way to provide time stamps and what I found was simply amazing.

The touch command provides a wonderful option -d through which the desired time stamps can be passed in form of various types of strings.

$ touch -d "2013-01-10" mylinuxbook_new

$ stat mylinuxbook_new 
  File: `mylinuxbook_new'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 539828      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-01-10 00:00:00.000000000 +0530
Modify: 2013-01-10 00:00:00.000000000 +0530
Change: 2013-03-02 19:05:38.579946832 +0530
 Birth: -

$ touch -d "next sunday" mylinuxbook_new

$ stat mylinuxbook_new 
  File: `mylinuxbook_new'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 539828      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-03-03 00:00:00.000000000 +0530
Modify: 2013-03-03 00:00:00.000000000 +0530
Change: 2013-03-02 19:06:04.280074273 +0530
 Birth: -

$ touch -d "2 hours ago" mylinuxbook_new

$ stat mylinuxbook_new 
  File: `mylinuxbook_new'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 539828      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-03-02 17:06:37.717485904 +0530
Modify: 2013-03-02 17:06:37.717485904 +0530
Change: 2013-03-02 19:06:37.712240057 +0530
 Birth: -

I have presented 3 examples of using -d option in the image above. The first one is fine but just look at the next two, isn’t the way you can just say “next sunday” or “2 hours ago” amazing? I would certainly recommend -d option followed by these type of human readable strings to those who are new to touch command and do not want to get into options like -t that expects complete information in a complex form.

There are multiple ways in which the string can be constructed for -d option. Here is an excerpt from the info page of the touch command :

`-d’
`–date=TIME’
Use TIME instead of the current time. It can contain month names, time zones, `am’ and `pm’, `yesterday’, etc. For example, `–date=”2004-02-27 14:19:13.489392193 +0530″‘ specifies the instant of time that is 489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east of UTC. *Note Date input formats::. File systems that do not support high- resolution time stamps silently ignore any excess precision here.

There are other options (worth mentioning here) like -a and -m that can be used to change the access and modification time stamps respectively. These are simple options which are easy enough to use so I am moving on without focusing much on them.

We initially learned that if touch command is run with the name of a file that does not exist then it will be created. There could be a situation where you run touch command with a file name in order to update it’s time stamps but there is no file (with that name) present in the directory. In this case, the touch command will silently create a file with the same name and update it’s time stamps. What if you do not want to create a new file to make sure that time stamps of only an existing file is updated always.?

The -c option caters to this need. It makes sure that no new file is created by touch command.

$ touch -c mlb

$ ls mlb
ls: cannot access mlb: No such file or directory

$ touch mlb

$ ls mlb
mlb

So we see that when -c option was used with touch command, no file named mlb was created. But, when -c was not used, file named mlb got created.

Once you know how to play with touch by creating new files and changing their time stamps, you start expecting more out of it. Well, I think it’s the reverse. The command has provided it’s capabilities like an open book and it expects you to use it in more useful and interesting ways.

A small example would be to create multiple files in one go.

Here is the example :

$ touch /tmp/{MLB1,MLB2,MLB3}

$ ls /tmp/MLB*
/tmp/MLB1  /tmp/MLB2  /tmp/MLB3

So you see that the touch command comes in handy in situations like these where it helps you to save a bit of your precious time.

From my experience, the best use of touch command that I have done is with the find command. The touch commands functionality acts as a plug-in to the functionality of find command to perform some really useful tasks and vice-versa in some cases. For example, touch command does not provide a functionality where it can recursively touch the files within subdirectories. But what if a situation like this arises? For example, you have to provide a complete directory structure (containing a good number files) to your client and before that you want to make sure that the time stamps of all the files in the parent directory and subdirectory are latest.

Well, in this case you can use the find command with touch command to achieve the goal.

$ ls -Rl
.:
total 8
drwxrwxr-x 2 himanshu himanshu 4096 Jan 19 18:28 dir1_1
-rw-rw-r-- 1 himanshu himanshu   17 Jan 19 18:28 new.txt
-rw-rw-r-- 1 himanshu himanshu    0 Jan 19 16:00 ref_file

./dir1_1:
total 0
-rw-rw-r-- 1 himanshu himanshu 0 Jan 19 18:28 new1.txt

$ find * -exec touch {} \;

$ ls -Rl
.:
total 8
drwxrwxr-x 2 himanshu himanshu 4096 Mar  2 19:14 dir1_1
-rw-rw-r-- 1 himanshu himanshu   17 Mar  2 19:14 new.txt
-rw-rw-r-- 1 himanshu himanshu    0 Mar  2 19:14 ref_file

./dir1_1:
total 0
-rw-rw-r-- 1 himanshu himanshu 0 Mar  2 19:14 new1.txt

In the example above, I used the ls command first to have a view at the complete directory structure
and the time stamp information of the files in directory and sub directories. Next, I executed the following command :

find * -exec touch {} \;

The find command (shown above) will find all (*) the files. As -exec option is used, so touch command will be executed for each result found by find command. So, this way the find command searches for files recursively and touch command changes the time stamps of each file found by the find command. Lastly, I checked the updated time stamps by running the ls command again.

This was an example where the capability of find command helped touch command to work recursively. Similarly, there can be use-cases where touch helps save the day for find command too.

Suppose you have a user who is learning web development on Linux. You asked him to play with HTML or PHP files in a particular directory. When he/she is done with the task, it’s the time for you to see what all files he/she created or played with. In order to do this, you need a tool that can find the files that were created/accessed/modified in a specified time period. Let’s say after 4:00pm.

Here is a situation where the touch command coupled with find command can let you find out the files that were created or modified during this time period.

$ touch -t 201302021600.00 ref_file

$ stat ref_file 
  File: `ref_file'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d	Inode: 524337      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/himanshu)   Gid: ( 1000/himanshu)
Access: 2013-02-02 16:00:00.000000000 +0530
Modify: 2013-02-02 16:00:00.000000000 +0530
Change: 2013-03-02 19:19:24.656043130 +0530
 Birth: -

So the first thing you do is to create a reference file with time stamp equal to 4pm of the day. Which is exactly what is shown in the above example. A reference file ref_file was created with time stamp as 4:00pm.

Next, you can use the -newer option of the find command to tell that we want to see all the files that were created/modified after 4:00pm.

$ find * -newer ref_file
bkup
bkup/testfile
bkup/ref_file2
cnstrDestr
cnstrDestr.cpp
dir1/new.txt
dir1/dir1_1
dir1/dir1_1/new1.txt
file name with space
firstPYProgram.py
macro
macro.c
macro.i
macro.o
macro.s
mlb
mylinuxbook
mylinuxbook_new

So we see that all the files whose time stamps were after 4:00pm, got displayed in the output.

NOTE : For systems where find command does not support -exec option, the xargs command can be used in a way as shown above.

Similarly you can use the ! operator with -newer option of the find command to do exactly the opposite ie all the files with time stamps earlier than 4:00pm.

Once the task is done, the reference files can be deleted using rm command.

TIP1 : You can use both the -newer and ! -newer options to find files created/modified between time X and time Y.

TIP2 : You can use both -exec option and -newer option to create backup of files created or modified in a certain time stamp range.

For example:

  • Backup files with time stamps older than the time stamps of reference file.
                   $ find ! -newer -exec mv '{}' \;
  • Backup files with time stamps more recent than time stamps of reference file.
                   $ find -newer -exec mv '{}' \;
  • Backup files with time stamps more recent than those of reference file 1 but older than those of reference file 2.
                   $ find -newer ! -newer -exec mv '{}' \;

Conclusion

Linux touch command is a small utility but can be very useful in cases where you have to deal with file time stamps. As we saw in the last few examples, it is extremely useful when used with Linux find command.

Leave a Reply

Your email address will not be published. Required fields are marked *