11 Linux Touch commands with Examples

By Aditya varma

August 14, 2017

Linux, touch command

The touch command can be used to modify the access/modification timestamps of files. More often it would be used to create an empty file quickly.

A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.

In this post we are going to explain you few simple and quick examples of using the touch command to modify timestamps or create files.​

1. ​How to create a blank file

​To create a simple blank file using touch command, use the below syntax. If the filename you choose is already there, than it will just change the access time.

touch fastwebhost.txt​

2. How to create multiple files

When you are suppose to create multiple files using touch command, just type the files name with extension separated by a space. You can check the below syntax.

touch fast.​txt web.txt host.txt

3. How to create bulk files with one command

If you were suppose to create bulk files, for example for 20 or 30 files and more than that with and without extensions. The below example will surely help you. You can check if the files were created or not with the command ls or ll

1. ​Create files with names A to Z

touch {A..Z}​

2. Create files from 1 to 100

touch {1..100}​

3. Create bulk files with extensions

touch {2..200}.txt​

4. To create 10000 files with an extension

touch {1..10}{1..1000}.txt​

4. How to avoid creating new files

Do you just want to update the access time of an existing file? Yes, you can surely do that without creating a new file. Use the option "-c". If the file exists, touch will update the access time, else will do nothing​.

touch -c fas​twebhost.txt

5. How to change access time of a file

To change only access time of a file use the "-a" option with the file name. You can check the below example.

touch -a fastwebhost.txt​

Once you done with the above command, to check if it is changed or not? try the below command.

stat fastwebhost.txt

​And the output looks like below.

File: ‘fastwebhost.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 804h/2052d Inode: 29645431 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ aditya) Gid: ( 1001/ aditya)
Access: 2017-08-14 13:30:24.029067639 +0530
Modify: 2017-08-14 13:30:24.029067639 +0530
Change: 2017-08-14 13:30:24.029067639 +0530

6. How to change modified time

Use the '-m' option to change the modified time of the file. You can check the time change in the following with stat command.

touch -m fastwebhost.txt​
File: ‘fastwebhost.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 804h/2052d Inode: 29645431 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1001/ aditya) Gid: ( 1001/ aditya)
Access: 2017-08-14 13:30:24.029067639 +0530
Modify: 2017-08-14 13:38:18.989096045 +0530
Change: 2017-08-14 13:38:18.989096045 +0530

7. How to change modify time for multiple files

To change the modify time of multiple files using wildcard.​

touch -m *.txt​

8. How to use both access and modifying time options

Let's use the "a" and "m" option together to modify both access and modification time​s. You can also check the changed timestamp using the "stat" command.

touch -am fastwebhost.txt​

​9. How to set particular time for access/modify

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

List of options with Touch Command

  • ​-a - Change access time of a file
  • -c - Do not create a specified file if it does not exist.
  • -m - Change the modification time of file.
  • -r - ref_file Use the corresponding times of the file named by ref_file instead of the current time.
  • -t - time Use the specified time instead of the current time. time will be a decimal number of the form
  • MM – The month of the year [01-12]
  • DD – The day of the month [01-31]
  • hh – The hour of the day [00-23]
  • mm – The minute of the hour [00-59]
  • CC – The first two digits of the year
  • YY – The second two digits of the year
  • SS – The second of the minute [00-61]
​touch -c -t 2017080711.15 fastwebhost.txt

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

10. How to ​use the timestamp of another file as reference

The below command will set the access/modify time of fast.txt to that of fastwebhost.txt

​touch -r fastwebhost.txt fast.txt

11. How to specify date and time as string

Otherthan the option "t" , there is another option "-d" which accepts datetime in general human readable formats.

The below example provides the date only. The time will automatically set to 00:00​

touch -c -d '14 Aug' fastwebhost.txt

Conclusion

In this article we covered basic touch commands which should be use more frequently. You​ can search more command options from its man pages. We are providing other Linux Commands articles in the below list.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.