≡ Menu

5 Linux Touch Command Examples (How to Change File Timestamp)

Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time.

Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically.

Touch command is used to change these timestamps (access time, modification time, and change time of a file).

1. Create an Empty File using touch

You can create an empty file using touch command. The following example will create a zero byte new file named tgs.txt.

$ touch tgs.txt

You can also use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file.

$ touch -c a.txt

Commands like ls command and find command uses these timestamp information for listing and finding files.

You can also create more than 1 files from a single touch command. The following example will create 4 files named a, b, c, and d.

$ touch a b c d

2. Change File’s Access Time using -a

We can change the access time of a file using -a option. By default it will take the current system time and update the atime field.

Before touch command is executed:

$ stat tgs.txt

  File: `tgs.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2012-10-18 23:58:21.663514407 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-18 23:58:21.663514407 +0530
$ touch -a tgs.txt

After the above touch command (Please note that the access time is changed):

$ stat tgs.txt

  File: `tgs.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2012-10-19 00:08:23.559514525 +0530
Modify: 2012-10-18 23:58:21.663514407 +0530
Change: 2012-10-19 00:08:23.559514525 +0530

3. Change File’s Modification Time using -m

You can change the modification time of a file using -m option.

$ touch -m *.o

The above method can be used to change the mtime of all obj files, when using make utility.

NOTE: It is not possible to change the ctime using touch command

4. Explicitly Setting Access and Modification time using -t and -d

Instead of taking the current time-stamp, you can explicitly specify the time using -t and -d options.

The format for specifying -t is [[CC]YY]MMDDhhmm[.SS]

$ touch -t [[CC]YY]MMDDhhmm[.SS]

The following explains the above format:

  • CC – Specifies the first two digits of the year
  • YY – Specifies the last two digits of the year. If the value of the YY is between 70 and 99, the value of the CC digits is assumed to be 19. If the value of the YY is between 00 and 37, the value of the CC digits is assumed to be 20. It is not possible to set the date beyond January 18, 2038.
  • MM – Specifies the month
  • DD – Specifies the date
  • hh – Specifies the hour
  • mm – Specifies the minute
  • SS – Specifies the seconds

For example:

$ touch -a -m -t 203801181205.09 tgs.txt

Verify the above change using stat command:

$ stat tgs.txt
  File: `tgs.txt'
  Size: 3         	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 394283      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/lakshmanan)   Gid: ( 1000/lakshmanan)
Access: 2038-01-18 12:05:09.000000000 +0530
Modify: 2038-01-18 12:05:09.000000000 +0530
Change: 2012-10-19 00:40:58.763514502 +0530

You can also use a string to change the time

Another example:

$ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txt

For developers, touch command will be really helpful when you are working with Makefiles

5. Copy the Time-stamp from Another File using -r

You can also take a file as a reference, and update the time for other files, so that both file will hold the same time.

The following touch command example will update the time-stamp of file a.txt with the time-stamp of tgs.txt file.

$ touch a.txt -r tgs.txt
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bob November 19, 2012, 11:29 am

    Cool. Thanks for the refresher!!!

  • Walter November 19, 2012, 11:50 am

    I must say I always look forward to your newsletter. Everyday commands with everyday examples.

  • siniranji November 19, 2012, 9:46 pm

    Thanks boss,
    This information is specially meant for those who use backtrack extensively, and sysadmins who wants to dodge any auditor, cyber forensic investigator. I think everybody know what i mean. we should practice such commands in all syntax to save ourselves

    Thanks for refreshing
    siniranji

  • vasilis November 20, 2012, 7:56 am

    thanks!great job and great site!

  • Satheesaran November 21, 2012, 7:04 am

    Thanks for examples. I was really looking for a way to change atime, ctime, mtime and now I could atleast do that with ease.

  • Jalal Hajigholamali November 21, 2012, 1:19 pm

    Hi,
    Thanks a lot, very nice examples…

  • sbarex November 21, 2012, 2:52 pm

    If I remember, if you wont to change the date before the current time, touch must be invoked from the same user that is the owner of the file…

  • shiv November 23, 2012, 8:24 am

    nice examples 🙂

  • Mitra Kaseebhotla November 28, 2012, 4:53 pm

    Thank You very much !!

  • Lamarr December 8, 2012, 11:22 am

    Thanks for the information. However, I am curious if there’s a way to change the change date and time also.

  • Indhu December 9, 2012, 7:44 am

    Hi I am new to linux os So these examples are really useful for me…..
    Thank you vary much……

  • Mehboob October 30, 2014, 6:39 am

    Cool, Helps in many ways

  • Daryl Strawberry February 1, 2016, 7:36 am

    Thanks

  • harikrishna muppa April 18, 2016, 5:04 am

    Hi,
    I am hari.With touch command i am unable to modify the time stamp.
    -t option is not working properly.when i used -t option but it will not display timestamp.
    touch -m -t 201502041205.20 filename—its k k verify this one time stamp will not display.
    ls -l filename
    if u know the answer please give me the reply