3 UNIX / Linux touch Command Examples

What is touch?
3 touch examples
Syntax and Options
Related Commands

What is touch?

touch command is used to change the timestamp of a file.

3 touch Examples

1. Change access, modify, and change timestamp to current timestamp

In the following example, file f1.c has different timestamps for the access, modification and change.

$ stat f1.c
  File: `f1.c'
  Size: 59              Blocks: 8          IO Block: 4096   Regular File
Device: 301h/769d       Inode: 280971      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-11-01 19:07:50.000000000 +0530
Modify: 2009-07-21 17:20:36.000000000 +0530
Change: 2009-07-21 17:20:36.000000000 +0530

When you touch the file as shown below, it will change all timestamps (access, modify and change) to current timestamp.

$ touch f1.c
$ stat f1.c
  File: `f1.c'
  Size: 59              Blocks: 8          IO Block: 4096   Regular File
Device: 301h/769d       Inode: 280971      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-11-01 20:35:58.000000000 +0530
Modify: 2010-11-01 20:35:58.000000000 +0530
Change: 2010-11-01 20:35:58.000000000 +0530

2. Change access and modification time to particular timestamp

The following example changes the access and modify timestamp to 15th June 13:30 using touch -t option.

Format for touch -t is : [[CC]YY]MMDDhhmm[.ss]

# touch -t 06151330 f1.c

# stat f1.c
  File: `f1.c'
  Size: 59              Blocks: 8          IO Block: 4096   Regular File
Device: 301h/769d       Inode: 280971      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-06-15 13:30:00.000000000 +0530
Modify: 2010-06-15 13:30:00.000000000 +0530
Change: 2010-11-01 20:43:59.000000000 +0530

Note: You cannot modify the “Change” time, as it will be set to the current time as the inode change time is current time.

3. Change only the modification time using touch -m

To update only the modification time of a file, use touch -m option along with the above explained -t option, as shown in the example below.

$ touch -m -t 08210820 f1.c

$ stat f1.c
  File: `f1.c'
  Size: 59              Blocks: 8          IO Block: 4096   Regular File
Device: 301h/769d       Inode: 280971      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-06-15 13:30:00.000000000 +0530
Modify: 2010-08-21 08:20:00.000000000 +0530
Change: 2010-11-01 20:45:05.000000000 +0530

Note: In the same way you can use touch -a option (along with touch -t option) to change only the access time for the file.

Syntax and Options

Short Option Long Option Option Description
-a change only the access time
-B –backward=SECONDS Modify the time by going back SECONDS seconds. For example, touch -r foo -B 5 bar will make the file bar 5 seconds older than file foo.
-c –no-create do not create any files
-d –date=STRING parse STRING and use it instead of current time
-F –forward=SECONDS Modify the time by going forward SECONDS seconds.
-m change only the modification time
-r –reference=FILE use this file’s times instead of current time
–time=WORD set time given by WORD: access atime use (same as -a) modify mtime (same as -m)


Related Commands

Comments on this entry are closed.

  • Mayuri August 30, 2012, 8:27 am

    Hi,The information is excellent.I learned touch command within a single read of it.