Linux Touch Command

Linux touch command examples

Touch Command


The touch command is generally used for creating new empty files. It is also frequently used for changing the timestamps (access and modification times) on existing files and directories.





Basic Syntax of command: touch [OPTION]... FILE...

When the touch command is used without any options, touch will create a new empty file for any names that are passed.



tux001@ls001a:~/touch> touch file1 file2 file3
tux001@ls001a:~/touch> ls -l
total 0
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file1
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file2
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file3

In the above example "file1, file2 and file3" were created with a size of "0" bytes.

One of the commonly used options for the touch command is its ability to modify the "access time" and "modification time". Touch accomplishes this by use of the "-a" and "-m" flags. ("-a" is for access times and"-m" is for modification times)



tux001@ls001a:~/touch> ls -l
total 0
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file1
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file2
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file3

tux001@ls001a:~/touch> touch -a file1

tux001@ls001a:~/touch> stat file1
  File: `file1'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 8145        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  tux001)   Gid: (  100/   users)
Access: 2013-07-23 11:36:49.000000000 +0100
Modify: 2013-07-23 11:13:57.000000000 +0100
Change: 2013-07-23 11:36:49.000000000 +0100
 Birth: -
tux001@ls001a:~/touch> touch -m file2
tux001@ls001a:~/touch> stat file2
  File: `file2'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 8146        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  tux001)   Gid: (  100/   users)
Access: 2013-07-23 11:13:57.000000000 +0100
Modify: 2013-07-23 11:37:29.000000000 +0100
Change: 2013-07-23 11:37:29.000000000 +0100
 Birth: -

In the above example the touch command was used with the "-a" parameter. We can see what effect this had by issuing the "stat" command to display the time stamps of the file.

Modifying the Date and Time with touch


Touch can also specify a specific time and date stamp when creating a file:



tux001@ls001a:~/touch> touch -t 201307101459.00 file4
tux001@ls001a:~/touch> ls -l
total 0
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file1
-rw-r--r-- 1 tux001 users 0 Jul 23 11:37 file2
-rw-r--r-- 1 tux001 users 0 Jul 23 11:13 file3
-rw-r--r-- 1 tux001 users 0 Jul 10 14:59 file4
tux001@ls001a:~/touch> stat file4
  File: `file4'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 8148        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  tux001)   Gid: (  100/   users)
Access: 2013-07-10 14:59:00.000000000 +0100
Modify: 2013-07-10 14:59:00.000000000 +0100
Change: 2013-07-23 11:44:18.000000000 +0100

Using the "-t" option we can specify a time and date to be used instead of the current time. The syntax of the command is as follows:

touch -t CCYYMMDDhhmm.ss filex

CC = Century
YY = Year
MM = Month
DD = Date
hh = hours
mm = minutes
.ss = Seconds

Another commonly used option is that of "-d". The "-d" option accepts dates in a string format.

touch -d '25 Dec 2012 11:00' file5



tux001@ls001a:~/touch> touch -d '25 Dec 2012 11:00' file5
tux001@ls001a:~/touch> ls -l file5
-rw-r--r-- 1 tux001 users 0 Dec 25  2012 file5
tux001@ls001a:~/touch> stat file5
  File: `file5'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 8149        Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  tux001)   Gid: (  100/   users)
Access: 2012-12-25 11:00:00.000000000 +0000
Modify: 2012-12-25 11:00:00.000000000 +0000
Change: 2013-07-23 11:56:01.000000000 +0100
 Birth: -


More help with touch


touch --help
info touch



Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
  --time=WORD            change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.