Tuesday, September 11, 2018

9 Useful Examples of Touch Command in Linux

https://linuxhandbook.com/touch-command

Learn to use touch command in Linux with these useful and practical examples.

Touch command in Linux

Touch command in Linux is used for changing file timestamps however one of the most common usages of touch command includes creating a new empty file.
With the touch command, you can change access, modify and change time of files and folders in Linux. You can update the timestamps or modify them to a date in the past.
The syntax for touch command is quite simple:
touch [option] file
Touch command syntax

What are file timestamps in Linux, again?

I have written about timestamps in Linux in detail in an earlier article. I would recommend reading it for a better and clearer understanding. For the sake of a quick recall, I’ll list the timestamps here:
  • access time – last time when a file was accessed 
  • modify time – last time when a file was modified 
  • change time – last time when file metadata (file permission, ownership etc) was changed  
You can see the timestamps of a file using the stat command in the following manner:
stat abhi.txt 
  File: abhi.txt
  Size: 10        	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11940163    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
Access: 2018-09-02 14:12:24.476483367 +0530
Modify: 2018-09-02 14:12:24.476483367 +0530
Change: 2018-09-02 14:12:24.476483367 +0530
 Birth: -

9 Practical examples of touch command in Linux

Touch command examples in Linux
Now let’s see how to use the touch command with some simple but useful examples.

1. Create an empty  file

As I mentioned earlier, this is the most common use of touch command. All you have to do is to use touch with the file name.
touch 
This will create an empty file if the file doesn’t exist.
touch empty_file
ls -l empty_file 
-rw-r--r-- 1 abhishek abhishek 0 Sep  2 14:37 empty_file
But what if the file already exists? In that case, it will update all three timestamps of the file to the current time.

2. Create multiple empty files

You can use touch to create more than one empty file as well. Just provide the names of the files you want to create.
touch   
If you think it’s tiring to write all filenames, you can auto-generate filenames in this way:
touch new-file-{1..10}.txt
This will create new-file-1.txt, new-file-2.txt upto new-file-10.txt.

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

Touch will update the timestamps of input file if it exists and will create an empty file if the input file does not exist.
But what if you don’t want touch to create a new empty file? You want it to update the timestamps of the file but if the file doesn’t exist, it should not be created.
You can use the touch command with -c option in such cases:
touch -c 
Remember: Touch will create a new empty file if it doesn’t exist else it will modify the timestamps of the existing file. You can stop the creation of a new file with the -c option. 

4. Change all timestamps of a file

If you use touch on an existing file, it will change access, modify and change time of that file.
For example, I have this file named sherlock.txt with the following timestamps:
stat sherlock.txt 
  File: sherlock.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
Access: 2018-08-25 09:44:56.092937000 +0530
Modify: 2018-08-09 09:41:05.028309000 +0530
Change: 2018-08-25 09:44:56.096937182 +0530
If I use touch on this command, all timestamps will be changed to the current timestamps.
touch sherlock.txt 
stat sherlock.txt 
  File: sherlock.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
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 should not be bothered with ctime (change time). It’s a system property and cannot/shouldn’t be controlled by the user. Your focus should be on access and modify time.

5. Update only access time of file

You may not always want to change all the timestamps of a file. If you just want to change the access time of a file, you can use the -a option with touch.
touch -a sherlock.txt 
stat sherlock.txt 
  File: sherlock.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
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 modify time of file

If you just want to update the modify time of a file to the current timestamp, use the -m option of touch command.
touch -m sherlock.txt 
stat sherlock.txt 
  File: sherlock.txt
  Size: 356       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928277    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
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 timestamps of another file

You can also use the timestamps of another file as a reference with the -r option in the following manner:
touch -r  
This will set the access and modify time of the target file same as the access and modify time of the source file.

8. Set specific access and modification time 

You might have noticed that in almost all the cases (except the reference file one), the timestamps are changed to the current timestamp.
But you are not bound with that. Touch allows you to set access and modification time to a past or future date. You can use the -t option and a timestamp in the following format:
[[CC]YY]MMDDhhmm[.ss]
  • CC – First two digits of a year
  • YY – Second two digits of a 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 optional as well, it will take the current year in that case. Similarly, seconds are optional as well, it defaults to 00.
Let me 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 agatha.txt 
stat agatha.txt 
  File: agatha.txt
  Size: 457       	Blocks: 8          IO Block: 4096   regular file
Device: 10305h/66309d	Inode: 11928279    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/abhishek)   Gid: ( 1000/abhishek)
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’ll see an error. You’ll also notice that change time is using the current timestamp, not the same as access and modify. It’s because it’s system property.

9. Change timestamp of a symbolic link

You can also use touch command with symbolic links. You just have to use the -h option while dealing with symbolic links. The rest stays the same as the regular files.
touch -h 
I hope you find these touch command examples in Linux helpful. If you have any questions or suggestions, do let me know.

No comments:

Post a Comment