10 practical examples of Touch command on Linux

Linux Touch command

The touch command can be used to modify the access/modification timestamps of files. It is more often used to actually just create an empty file quickly.

This post shows some very simple and quick examples of using the touch command to modify timestamps or create files.

1. Create a blank file

To simply create a blank file with touch command, use the syntax below.

$ touch abc.txt

If the file already exists, its access time will be updated.

2. Create multiple files with touch

To create multiple files, specify their names together separated by a space.

$ touch abc.txt cde.txt xyz.txt

3. Create lots and lots of files

If for some reason you wish to create lots of files, then commands like these would be very helpful

# Create files with names A to Z
$ touch {A..Z}

# Create files with names 1 to 20
$ touch {1..20}

# Create files with extension
$ touch {1..1000}.txt

# Create 10K files
$ touch {1..10}{1..1000}

And then use the ls command to see what all has been created.

4. Avoid creating new files

If you want to just update the access time of existing file, without creating it, use the '-c' option. If the file exists, touch will update the access time, else will do nothing

$ touch -c hello.txt

5. Change file access time - 'a'

To change only access time of a file use the '-a' option with the file name.

$ touch -a abc.txt

To check the access time use the stat command

$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:04:24.281533071 +0530
Modify: 2016-03-10 15:00:16.117864128 +0530
Change: 2016-03-10 15:04:24.281533071 +0530

6. Change the modified time '-m'

Use the '-m' option to change the modified time of the file

$ touch -m a.txt 
[term]

Then check the file statistics with the stat command -

[term]
$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:04:24.281533071 +0530
Modify: 2016-03-10 15:05:03.409475551 +0530
Change: 2016-03-10 15:05:03.409475551 +0530






To change the modify time of multiple files using wildcard

$ touch -m *.txt

7. Change access and modification time together

Use the a and m option together to modify both access and modification time

$ touch -am a.txt
$ stat a.txt 
  File: ‘a.txt’
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d      Inode: 5904730     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/enlightened)   Gid: ( 1000/enlightened)
Access: 2016-03-10 15:07:39.633235119 +0530
Modify: 2016-03-10 15:07:39.633235119 +0530
Change: 2016-03-10 15:07:39.633235119 +0530

8. Set a specific access/modify time instead of current time

To set the access/modify time to a specific datetime use the t option and specify the datetime in format
[[CC]YY]MMDDhhmm[.ss]

$ touch -c -t 1603051015 a.txt 

or 

$ touch -c -t 201603051015 a.txt

Note - If you omit the c option, a new file will be created with the given datetime if it does not exist.

9. Use the timestamp of another file as reference

$ touch -r ref.txt abc.txt

The above command will set the access/modify time of abc.txt to that of ref.txt

10. Specify datetime as a string

Apart from the t option, there is another option '-d' which accepts datetime in general human readable formats.

The following example provides the date only. The time is automatically set to 00:00

$ touch -c -d '14 Mar' abc.txt

Or just provide the time, and the current date will be selected -

$ touch -d '14:24' abc.txt

To learn more, check the man page with "man touch".

Last Updated On : 6th May 2016

Subscribe to get updates delivered to your inbox

5 Comments + Add Comment

  • very good, the article is very clearly and the script very smart.

  • Actually the {A..Z} syntaxes have nothing to do with touch, but with your shell expansion capabilities.
    Try this with dash, for an example where it doesn’t work:

    bash:/tmp/test$ dash
    $ touch {A..Z}
    $ ls
    {A..Z}

  • Practical is when you combine touch with find command… if you are synchronizing two computers you may touch a file then work lot into of files and when you need to synchronize you use the command:

    tar jcvf modified_files.tar.bz2 `find ~ -newer ~/touched_file.dat`

    Then yoy may untar this modified_files into the other computer and BINGO!

  • I have another., generally used as part of a build process
    track releases just by the time portion of the file
    touch -d ’02:05′ my_script

    marks the file as second version, fifth revision.

  • touch is a nice command :D

    Pretty sure
    # Create 10K files
    $ touch {1..10}{1..1000}
    doesn’t give you a nice tidy range of numbers like you might/would/could want (it’s not multiplication!). It should give you a weird mix where your first number is 11 and your 10000th number will be named 101000 and because there are no leading zeros it wont sort nicely either. Which can be all fine and dandy.

    I haven’t bothered to try {1..10000} but maybe that will actually work and give you what’s intended (and faster to type). Still no leading zeros though.

    However instead one could do
    $ touch {0..9}{0..9}{0..9}{0..9}
    because each individual digit is 0 to 9 (it is a regular expression after all). First file is 0000 and 10000th file is 9999.

    Or if you’re allergic to counting from 0 and/or do NOT want tidy numbers with leading zeros and for whatever reason {1..10000} doesn’t work (because I didn’t test that one) then one can do
    $ for x in `seq 1 10000`; do touch $x; done

    (No I didn’t test it to 10000, I did something like “for i in `seq 2 12`; do touch $i.okay; done” instead).

    Anyway it all depends/it might not be important at all and one just want 10000 files named whatever :)

    *speeds off into the distance* :)

Leave a comment