Contents[Hide]

Author: Lubos Rendek

Date: 21.03.2010

1. Name

touch [man page] - change file timestamps

2. Usage

When working with a Linux operating system, sometimes a need arises to create an empty file. Some service, which an administrator is configuring, requires a certain file to be created before the service can start and what is more this file may need to have certain access time stamp. If you are, for example, running a photo album web application which automatically adds your images to the album and sorts them according to the image creation time, the images you upload must have set accurate creation time in order this can work. What if the time set in your camera is incorrect?

A tool which comes handy in both scenarios is the touch command. Touch command not only provides a simple way to create files, but it also allows a user to alter access and modification time for a given file.

NOTE: Linux system does not store a file creation time. The information stored instead is the access time. The access time does not necessarily represent creation time and in many situations it represents access time only. This is because access time gets updated every time a file is accessed / read.

NOTE: In this article, we are going to use the stat command to observe changes to file's access and modification time made by touch command.

2.1. File creation

As it was already mentioned a touch command provides a simple way of creating empty files. The syntax is rather simple:

linuxconfig.org:~$ ls
linuxconfig.org:~$ touch fileA
linuxconfig.org:~$ ls
fileA
linuxconfig.org:~$

Execution of touch command with an argument fileA will produce a single empty file named fileA. Touch command can accept more than one argument and as result:

linuxconfig.org:~$ touch fileB fileC fileD
linuxconfig.org:~$ ls
fileA fileB fileC fileD
linuxconfig.org:~$

it will create three additional files called fileB fileC and fileD. It is important to understand that all files created by touch command are empty, which means that touch command does not add any characters to newly created files.

2.2. Change file timestamps

At this point, we need to have a close look at earlier created files. stat command will return verbose information associated to a file given to it as its argument.

linuxconfig.org:~$ stat fileA
File: `fileA'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 1867780 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/linuxconfig) Gid: ( 1001/linuxconfig)
Access: 2010-03-21 06:22:52.000000000 +1100
Modify: 2010-03-21 06:22:52.000000000 +1100
Change: 2010-03-21 06:22:52.000000000 +1100
linuxconfig.org:~$

stat command returned detailed information about fileA status in regard to a present file system. The only interesting bit connected to this article is access and modify time.

2.2.1. Access time

Access time refers to a time when to file was last time accessed. In other words, every time some utility reads a content of fileA, fileA will change its access time. Following example demonstrates this point:

linuxconfig.org:~$ date
Sun Mar 21 06:23:08 EST 2010
linuxconfig.org:~$ stat -c %x fileA
2010-03-21 06:22:52.000000000 +1100
linuxconfig.org:~$ cat fileA
linuxconfig.org:~$ stat -c %x fileA
2010-03-21 06:23:19.000000000 +1100
linuxconfig.org:~$

First, we have used the date command to retrieve a current date and time. Next command executed is stat command which returns an access time of fileA. Cat command is used to access / read a content of fileA. There is no output since fileA is an empty file. Executing a stat command again with fileA as an argument indicates that access time has been changed.

2.2.2. Modify time

Modify time is altered when the content of a file itself has been modified. In the following example, we use echo command to modify a content of fileA:

linuxconfig.org:~$ date
Sun Mar 21 06:23:23 EST 2010
linuxconfig.org:~$ stat -c %y fileA
2010-03-21 06:22:52.000000000 +1100
linuxconfig.org:~$ echo addline > fileA
linuxconfig.org:~$ stat -c %y fileA
2010-03-21 06:23:35.000000000 +1100
linuxconfig.org:~$

Once again, we have used a date command to show a current system time. Next command executed was a stat command which in this case displayed modify time of a fileA. Next, we have used an echo command to add new line into fileA. Lastly, stat command confirms that modify time has been changed. As a side-note it should be mentioned that modifying content of file will alter also file's change time.

2.2.3. Change time

In regard to a touch command, we are not interested in change time. Nevertheless, just for a completeness we shortly examine also change time. Change time holds a time when file's meta data or inode information is altered. Meta data includes change of permissions or file ownership. Following example illustrates this idea:

linuxconfig.org:~$ date
Sun Mar 21 06:23:40 EST 2010
linuxconfig.org:~$ stat -c %z fileA
2010-03-21 06:23:35.000000000 +1100
linuxconfig.org:~$ chmod +x fileA
linuxconfig.org:~$ stat -c %z fileA
2010-03-21 06:23:51.000000000 +1100
linuxconfig.org:~$

Same as in both preceding examples date command displays a current date and time. stat command reveals a current value of file's change time. chmod command added executable permissions to a file. When comparing both time values, it is clear that a modification of fileA's permissions resulted in updating a change time value to a current system time.

2.3. Video

2.4. Update the access and modification times

Now that we know something more about access and modify time, we are free to explore what more has touch command to offer. It would not be very efficient if every time system administrator needs to change modify time of a certain file, s/he would have to add a line into it as we seen in previous examples. What if s/he does not want to alter a file content and more importantly, what if the file is a binary file? This is exactly the situation where the touch command becomes very handy tool for that job. In the following example, we are going to update access and modify times of fileB:

linuxconfig.org:~$ alias stat='stat --printf="Access time:%x\nModify time:%y\n"'
linuxconfig.org:~$ date
Sun Mar 21 07:41:39 EST 2010
linuxconfig.org:~$ stat fileB
Access time:2010-03-21 07:41:16.000000000 +1100
Modify time:2010-03-21 07:41:16.000000000 +1100
linuxconfig.org:~$ touch fileB
linuxconfig.org:~$ stat fileB
Access time:2010-03-21 07:41:51.000000000 +1100
Modify time:2010-03-21 07:41:51.000000000 +1100
linuxconfig.org:~$

With the first command, we have composed an alias to a stat command, which will modify it's default output to display only access and modify time stamps. Using a date command we have retrieved current date. stat command then shows present values of access and modify time stamps. Execution of touch command with a file name as a single argument will modify access and modify time stamps to a current system time which is later confirmed by execution of stat command.

Until now we have used a touch command lacking any other options. We have simply just supplied file name as an argument to a touch command which updated access and modify times to match a current system time. In this section, we will talk more about other options available for Linux user when using a touch command as an instrument to update the access and modify times of given files as it's arguments.

Touch command is also capable to change access and modify time independently. Think over a following example:

linuxconfig.org:~$ date
Sun Mar 21 07:41:55 EST 2010
linuxconfig.org:~$ stat fileA
Access time:2010-03-21 07:41:16.000000000 +1100
Modify time:2010-03-21 07:41:16.000000000 +1100
linuxconfig.org:~$ touch -a fileA
linuxconfig.org:~$ stat fileA
Access time:2010-03-21 07:42:06.000000000 +1100
Modify time:2010-03-21 07:41:16.000000000 +1100
linuxconfig.org:~$

In this situation, we have used a touch command with -a option which means that touch command will only update access time of fileA. You should be familiar with the rest of commands executed from previous the examples. Please note that our alias to a stat command, which displays only access and modify times is still valid and it was assigned previously.
Same as for access time touch command has also -m switch to change only modify time. This thought is demonstrated in the following example:

linuxconfig.org:~$ date
Sun Mar 21 07:42:13 EST 2010
linuxconfig.org:~$ stat fileA
Access time:2010-03-21 07:42:06.000000000 +1100
Modify time:2010-03-21 07:41:16.000000000 +1100
linuxconfig.org:~$ touch -m fileA
linuxconfig.org:~$ stat fileA
Access time:2010-03-21 07:42:06.000000000 +1100
Modify time:2010-03-21 07:42:21.000000000 +1100
linuxconfig.org:~$

In the event that an administrator has a specific time to which the fileB's timestamp is required to change to, s/he can use -d option to supply a date string to touch command. Let's say that we would like to change a time stamps of fileB to 8.1.1942 16:50:

linuxconfig.org:~$ stat fileB
Access time:2010-03-21 07:41:51.000000000 +1100
Modify time:2010-03-21 07:41:51.000000000 +1100
linuxconfig.org:~$ touch -d '8 Jan 1942 16:50' fileB
linuxconfig.org:~$ stat fileB
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:1942-01-08 16:50:00.000000000 +1100
linuxconfig.org:~$

Option -d allows as to supply a time string to a touch command which will modify time stamps accordingly. The same result can also be accomplished with -t option where exact timestamp should be supplied in a following format: [[CC]YY]MMDDhhmm[.ss] . If we want to change 16.10.2002 11:43:28 to a timestamp in accordance to this format, the timestamp will look like something like this: 200210161143.28 . Let's se if that works by changing modify time of fileB to 200210161143.28:

linuxconfig.org:~$ stat fileB
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:1942-01-08 16:50:00.000000000 +1100
linuxconfig.org:~$ touch -m -t 200210161143.28 fileB
linuxconfig.org:~$ stat fileB
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:2002-10-16 11:43:28.000000000 +1000
linuxconfig.org:~$

As you can see touch command does not have any difficulties to change time stamps of any file to any date and time. Here are more examples of how time stamp can be used with a touch command:
NOTE: Following examples are related to EPOCH time, which on most linux systems started on 1.1.1970 and will end on 18.1.2038.

  • 200210161143 - will set timestamps to 16.10.2002 11:43:00
  • 3710161143 - change time to 16.10.2037 11:43:00, when YY in timestamp is between 00-38 the CC will become 20
  • 7810161143 - change time to 16.10.1978 11:43:00, when YY in timestamp is between 69-99 the CC will become 19
  • 10161143 - change time to 16.10.2009 11:43:00, where 2009 is current year
  • 4010161143 - touch: invalid date format `4010161143' the CC value is missing

Another way, to change time stamps with a touch command is to source another file as a reference. Suppose, there is a situation, where you would like to change time stamp of one file to be equal to the timestamp of another. In this case, it is possible to use -r option:

linuxconfig.org:~$ stat fileB
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:2002-10-16 11:43:28.000000000 +1000
linuxconfig.org:~$ stat fileA
Access time:2010-03-21 07:42:06.000000000 +1100
Modify time:2010-03-21 07:42:21.000000000 +1100
linuxconfig.org:~$ touch fileA -r fileB
linuxconfig.org:~$ stat fileA
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:2002-10-16 11:43:28.000000000 +1000
linuxconfig.org:~$ stat fileB
Access time:1942-01-08 16:50:00.000000000 +1100
Modify time:2002-10-16 11:43:28.000000000 +1000
linuxconfig.org:~$

2.5. Video

3. Synopsis

touch [OPTION]... FILE...

4. Frequently used options

-a change only the access time
-m change only the modification time
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time

5. Workshop

  1. How would you amend access and modify time stamps of FILE1 to a current system time?
  2. Answer: touch FILE1
  3. How would you change access time of fileB to 15.3.2001 14:15:53?
  4. Answer: touch -a -t 200103151415.53 fileB
  5. How would you change modify time of fileB to 15.3.2001 using time string?
  6. Answer: touch -m -d '15 Mar 2001' fileB
  7. How can you modify time stamps of fileA and fileB to reflect a current system time?
  8. Answer: touch fileA fileB
  9. How can you change access and modify time stamps of all files within /etc/ directory to be equal to fileA located in your current directory?
  10. Answer: touch /etc/* -r fileA


Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top