Touch command in Linux
- . More time is spent meditating on Insight Timer than all other meditation apps. Happiest App in the World - Tristan Harris. The number 1 free meditation app. Guided meditations and talks led by the world's top meditation and mindfulness experts, neuroscientists, psychologists and teachers from Stanford, Harvard, the University of Oxford.
- Instant touch access to complete workforce information. Today’s workforce is always evolving — and we’ve built a time clock that keeps pace. Inefficient and error-prone handwritten timecards and outdated time clock systems have had their day.
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.
Touch Bar Timer. 0:00 A little stopwatch for your Mac's touch bar. Tap to start/stop. Double-tap to reset. Hold to open the app preferences. V1.0.0 by Alex Zirbel. Code on GitHub with thanks to Pixel Point. MyTouchSmart timers are an incredibly easy way to automate your lights to be on when you need them most. Save energy, save money and feel secure with myTouchSmart plug-In indoor/outdoor digital timers. Featuring two grounded outlets, the timer delivers simple automation for your landscape, patio and seasonal lighting.
The syntax for touch command is quite simple:
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 command even shows the inode number of the file.
9 Practical examples of touch command 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.
This will create an empty file if the file doesn’t exist.
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.
If you think it’s tiring to write all filenames, you can auto-generate filenames in this way:
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:
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:
Touch Time Clock
If I use touch on this command, all timestamps will be changed to the current 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.
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.
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:
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:
Touch Time App
[[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.
I hope you find these touch command examples in Linux helpful. If you have any questions or suggestions, do let me know.
Become a Member for FREE
Touch Time Round Smartwatch
Join the conversation.