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
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:
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:
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.
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:
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