9 useful Linux Touch command examples

The Touch command in Linux is used to change the timestamps of files, however one of the most common uses of the Touch command involves creating a new empty file.

With the touch command, you can change access, change the time of files and folders in Linux. You can update timestamps or change them to a date in the past.

The touch command syntax is pretty simple:

touch [option] file

What are file timestamps in Linux?

For quick reference, we’ll list the timestamps here:

  • access time – the last time the file was accessed
  • change time – last time the file was changed
  • change time – the last time the file metadata was changed (file permission, ownership, etc.)

You can see the timestamps of a file using the stat command as follows:

stat andreyex.txt 
  File: andreyex.txt
  Size: 10        	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11940163    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-09-02 14:24:16.214425121 +0530
Modify: 2018-09-02 14:24:16.214425121 +0530
Change: 2018-09-02 14:24:16.214425121 +0530
 Birth: -

9 practical Linux touch command examples

Now let’s see how to use the touch command with simple yet useful examples.

1. Create an empty file

As we mentioned earlier, this is the most common use of the touch command. All you have to do is touch the filename.

touch <filename>

This will create an empty file if the file does not exist.

touch empty_file
ls -l empty_file 
-rw-r--r-- 1 andreyexshek andreyexshek 0 Sep  2 14:24 empty_file

But what if the file already exists? In this case, it will update all three temporary files to the current time.

2. Create some blank files

You can use touch to create multiple blank files. Just provide the names of the files you want to create.

touch <file1> <file2> <file3>

If you think you’re tired of writing all the filenames, you can automatically generate filenames like this:

touch new-file-{1..10}.txt

This will create file new-file-1.txt, new-file-2.txt to new-file-10.txt.

3. Avoid creating a file with touch if it doesn’t exist

Touch will update the timestamps of the input file if it exists and create an empty file if the input file does not exist.

But what if you don’t want to use touch to create a new empty file? You want it to update the file’s timestamps, but if the file doesn’t exist, it shouldn’t be created.

You can use the touch command with the -c option in these cases:

touch -c <filename>

Note Remember: Touch will create a new empty file, if it doesn’t exist, it will change the timestamps of the existing file. You can stop the creation of a new file with the -c option.

4. Change all file timestamps

If you touch on an existing file, it will change the access and also change the time of that file.

For example, I have a file Alex_many.txt with the following timestamps:

stat Alex_many.txt 
  File: Alex_many.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-08-14 11:24:45.092937000 +0530
Modify: 2018-08-16 15:22:55.028309000 +0530
Change: 2018-08-14 11:24:45.096937182 +0530

If you use the touch command, all timestamps will be changed to the current timestamps.

touch Alex_many.txt 
stat Alex_many.txt 
  File: Alex_many.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-09-02 15:22:47.017037942 +0530
Modify: 2018-09-02 15:22:47.017037942 +0530
Change: 2018-09-02 15:22:47.017037942 +0530
 Birth: -

Note You don’t have to worry about CTime (Time Change). This is a system property and cannot / should not be controlled by the user. You have to focus on accessing and changing times.

5. Update only file access time

You may not always be able to change all of the file’s timestamps. If you just want to change the file access time, you can use the -a option with the touch command.

touch -a Alex_many.txt 
stat Alex_many.txt 
  File: Alex_many.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-09-02 15:29:08.796926093 +0530
Modify: 2018-09-02 15:22:47.017037942 +0530
Change: 2018-09-02 15:29:08.796926093 +0530
 Birth: -

6. Update only when the file was modified

If you just want to update the file modification time to the current time stamp, use the -m option to the touch command.

touch -m Alex_many.txt 
stat Alex_many.txt 
  File: Alex_many.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-09-02 15:29:08.796926093 +0530
Modify: 2018-09-02 15:31:25.770866881 +0530
Change: 2018-09-02 15:31:25.770866881 +0530
 Birth: -

7. Use the timestamps of another file

You can also use the timestamps of another file as a link with the -r option like this:

touch -r <source_file> <target_file>

This will set the access and modification times of the target file the same as the access and modification times of the source file.

8. Set specific access and modification times

You may have noticed that in almost all cases (except for the referenced file) the timestamps are changed to the current timestamp.

But you are not connected with it. Touch allows you to set access and modification times to a past or future date. You can use the -t option and timestamp in the following format:

[[CC]YY]MMDDhhmm[.ss]

  • CC – the first two digits of the year
  • YY – Two two digits of the year
  • MM – month of the year (01-12)
  • DD – Day of the month (01-31)
  • hh – hour of the day (00-23)
  • mm – minute of the hour (00-59)
  • ss – Seconds (00-59)

In the above case, CC is optional. In fact, CCYY is also optional, in which case it will take the current year. Likewise, seconds are also optional, by default they are 00.

Let us show you an example by changing the timestamp to 12021301 i.e. 12th month, second day, 13th hour and first minute of the current year:

touch -t 12021301 destroyer.txt 
stat destroyer.txt 
  File: destroyer.txt
  Size: 457       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928279    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/andreyexshek)   Gid: ( 1000/andreyexshek)
Access: 2018-12-02 13:01:00.000000000 +0530
Modify: 2018-12-02 13:01:00.000000000 +0530
Change: 2018-09-02 15:59:47.588680901 +0530
 Birth: -

If you try to enter an invalid date, you will see an error message. You will also notice that the change time uses the current timestamp, not both access and change. This is because it is a system property.

9. Change the timestamp of a symbolic link

You can also use the touch command with symbolic links. You just need to use the -h option when dealing with symbolic links. The rest remain the same as regular files.

touch -h <symbolic_link>

Hope you find these Linux command line examples useful. If you have any questions or suggestions, please let us know.

Please disable your ad blocker or whitelist this site!

Related Posts