11 touch Command Examples For Linux

The ‘touch’ command is used to modify the time stamp of an existing file, or to create new empty files.

Touch comes from the GNU Coreutils package and should be available by default in Unix/Linux based operating systems.

The practical examples throughout this guide will show you how to use the touch command.

Personally I use the touch command to quickly create a new file much more often than to edit the time stamps of a file.

How To Use touch – Command Examples

  • 1. Using The Touch Command

    In its most simplest form, the touch command can be used on a file specified to update the access, modify, and change time stamps as we’ll see below. We can see our test.txt example file currently has not been accessed, modified, or changed since the 2nd of September. After running touch against it, we can see that the time stamps are updated to the current time, the 7th of September.

    [root@centos7 ~]# stat test.txt
      File: ‘test.txt’
      Size: 69              Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 67191495    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-02 09:55:35.298208314 +1000
    Modify: 2016-09-02 09:55:33.348208164 +1000
    Change: 2016-09-02 09:55:33.348208164 +1000
     Birth: -
    
    [root@centos7 ~]# touch test.txt
    
    [root@centos7 ~]# stat test.txt
      File: ‘test.txt’
      Size: 69              Blocks: 8          IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 67191495    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-07 11:01:36.117150795 +1000
    Modify: 2016-09-07 11:01:36.117150795 +1000
    Change: 2016-09-07 11:01:36.117150795 +1000
     Birth: -
    
  • 2. Change Access Time

    Rather than changing all time stamps, we can specify the -a option to only modify the access time. Note that this also updates the change time, as the file has changed.

    [root@centos7 ~]# stat test2.tar
      File: ‘test2.tar’
      Size: 10240           Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 68496231    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-02 15:47:33.940838420 +1000
    Modify: 2016-09-02 15:47:33.940838420 +1000
    Change: 2016-09-02 15:47:33.940838420 +1000
     Birth: -
    [root@centos7 ~]# touch -a test2.tar
    
    [root@centos7 ~]# stat test2.tar
      File: ‘test2.tar’
      Size: 10240           Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 68496231    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-07 11:04:55.267166167 +1000
    Modify: 2016-09-02 15:47:33.940838420 +1000
    Change: 2016-09-07 11:04:55.267166167 +1000
     Birth: -
    
  • 3. Change Modification Time

    Alternatively we can specify -m to update the modify time stamp instead of the access time. Note again that this also updates the change time, as the file has been changed.

    [root@centos7 ~]# touch -m test2.tar
    [root@centos7 ~]# stat test2.tar
      File: ‘test2.tar’
      Size: 10240           Blocks: 24         IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 68496231    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-07 11:04:55.267166167 +1000
    Modify: 2016-09-07 11:06:18.771172613 +1000
    Change: 2016-09-07 11:06:18.771172613 +1000
     Birth: -
    
  • 4. Create New File

    If the file that we specify after the touch command does not exist, by default touch will create a new empty file of 0 bytes in size.

    [root@centos7 ~]# touch new-file-123
    [root@centos7 ~]# stat new-file-123
      File: ‘new-file-123’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 68496243    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-07 11:07:07.419176368 +1000
    Modify: 2016-09-07 11:07:07.419176368 +1000
    Change: 2016-09-07 11:07:07.419176368 +1000
     Birth: -
    

    Access, modify and change time stamps are all set to when the file was created with touch.

  • 5. Create Multiple Files

    We can create multiple new files with the touch command by simply specifying the filenames after one another.

    [root@centos7 files]# touch 1 2 3
    [root@centos7 files]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Sep  7 11:54 1
    -rw-r--r--. 1 root root 0 Sep  7 11:54 2
    -rw-r--r--. 1 root root 0 Sep  7 11:54 3
    

    With the help of bash we can very quickly create a large number of new files.

    [root@centos7 files]# touch {a..e}
    [root@centos7 files]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Sep  7 11:57 a
    -rw-r--r--. 1 root root 0 Sep  7 11:57 b
    -rw-r--r--. 1 root root 0 Sep  7 11:57 c
    -rw-r--r--. 1 root root 0 Sep  7 11:57 d
    -rw-r--r--. 1 root root 0 Sep  7 11:57 e
    
  • 6. Do Not Create New File

    We also have the option of preventing new files from being created with the touch command by using the -c option.

    [root@centos7 ~]# touch -c this-does-not-exist
    [root@centos7 ~]# stat this-does-not-exist
    stat: cannot stat ‘this-does-not-exist’: No such file or directory
    

    Touch will still work normally on existing files, this only prevents it from creating new files that do not already exist.

  • 7. Use Time String

    We can set a custom time stamp with the -d option which takes the string given to it rather than using the current time. This may be useful as it allows us to specify a human readable format.

    [root@centos7 ~]# touch test123 -d '20 Jun 2020'
    [root@centos7 ~]# stat test123
      File: ‘test123’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 68496245    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2020-06-20 00:00:00.000000000 +1000
    Modify: 2020-06-20 00:00:00.000000000 +1000
    Change: 2016-09-07 11:23:08.186250528 +1000
     Birth: -
    

    In this example we have created the new file ‘test123’ with the access and modify date of June 20 in the year 2020.

  • 8. Set Specific Time

    Similarly to the -d option above, we can use the -t option to edit the time stamps on a file in the format of [[CC]YY]MMDDhhmm[.ss]

    [root@centos7 ~]# touch file -t 205001010000
    [root@centos7 ~]# stat file
      File: ‘file’
      Size: 0               Blocks: 0          IO Block: 4096   regular empty file
    Device: fd00h/64768d    Inode: 68496244    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2050-01-01 00:00:00.000000000 +1100
    Modify: 2050-01-01 00:00:00.000000000 +1100
    Change: 2016-09-07 11:19:13.919232445 +1000
     Birth: -
    

    In this example we have created a new file called ‘file’ which has its last access and modify times set far into the future in the year 2050.

  • 9. Use A Reference File

    Rather than specifying a custom time stamp to apply to a file, we can read in the time stamps that exist from another file with the -r option followed by the file to copy the time stamps for.

    [root@centos7 ~]# stat z.mp3
      File: ‘z.mp3’
      Size: 3407872         Blocks: 6656       IO Block: 4096   regular file
    Device: fd00h/64768d    Inode: 67183574    Links: 1
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-02 14:35:14.767503488 +1000
    Modify: 2016-09-01 11:29:15.743511080 +1000
    Change: 2016-09-01 11:29:15.743511080 +1000
     Birth: -
    
    [root@centos7 ~]# touch new -r z.mp3
    
    [root@centos7 ~]# stat new
      File: ‘new’
      Size: 4096            Blocks: 8          IO Block: 4096   directory
    Device: fd00h/64768d    Inode: 67191499    Links: 2
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:admin_home_t:s0
    Access: 2016-09-02 14:35:14.767503488 +1000
    Modify: 2016-09-01 11:29:15.743511080 +1000
    Change: 2016-09-07 11:10:37.477192582 +1000
     Birth: -
    

    In this example we see that the z.mp3 file was last accessed September 2nd, and modified September 1st. We create a new file called ‘new’ with the -r option and confirm that after creation it has the same access and modify times as the z.mp3 file. Note that the change time stamp is updated to the time that the file was created.

  • 10. Display Version

    We can print out the version of touch that is currently installed on our system with the --version option.

    [root@centos7 ~]# touch --version
    touch (GNU coreutils) 8.22
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later .
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Paul Rubin, Arnold Robbins, Jim Kingdon,
    David MacKenzie, and Randy Smith.
    
  • 11. Further Help

    Finally if you need any further help we can always check the documentation with the --help option. Note that for brevity I have not included the full output of this command.

    [root@centos7 ~]# touch --help
    Usage: touch [OPTION]... FILE...
    

Summary

We have seen that the touch command can be used to both create empty new files, as well as edit the access or modify time stamp on an existing specified file quite easily.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>