![]() I hope you find these touch command examples in Linux helpful. The rest stays the same as the regular files. You just have to use the -h option while dealing with symbolic links. You can also use touch command with symbolic links. You’ll also notice that change time is using the current timestamp, not the same as access and modify. ![]() If you try to enter an invalid date, you’ll see an error. 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 Similarly, seconds are optional as well, it defaults to 00. In fact, CCYY is optional as well, it will take the current year in that case. You can use the -t option and a timestamp in the following format: Touch allows you to set access and modification time to a past or future date. You might have noticed that in almost all the cases (except the reference file one), the timestamps are changed to the current timestamp.īut you are not bound with that. Set specific access and modification time This will set the access and modify time of the target file same as the access and modify time of the source file. You can also use the timestamps of another file as a reference with the -r option in the following manner: touch -r If you just want to update the modify time of a file to the current timestamp, use the -m option of touch command. If you just want to change the access time of a file, you can use the -a option with touch. You may not always want to change all the timestamps of a file. Your focus should be on access and modify time. It’s a system property and cannot/shouldn’t be controlled by the user. Note: You should not be bothered with ctime (change time). If I use touch on this command, all timestamps will be changed to the current timestamps. If you use touch on an existing file, it will change access, modify and change time of that file.įor example, I have this file named sherlock.txt with the following timestamps: stat sherlock.txt You can stop the creation of a new file with the -c option. 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. Touch will update the timestamps of input file if it exists and will create an empty file if the input file does not exist.īut 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. Avoid creating a file with touch if it doesn’t exist This will create new-file-1.txt, new-file-2.txt upto new-file-10.txt. If you think it’s tiring to write all filenames, you can auto-generate filenames in this way: touch new-file-.txt Just provide the names of the files you want to create. You can use touch to create more than one empty file as well. rw-r-r- 1 abhishek abhishek 0 Sep 2 14:37 empty_fileīut what if the file already exists? In that case, it will update all three timestamps of the file to the current time. ![]() This will create an empty file if the file doesn’t exist. All you have to do is to use touch with the file name. Create an empty fileĪs I mentioned earlier, this is the most common use of touch command. Now let’s see how to use the touch command with some simple but useful examples. 9 Practical examples of touch command in Linux Stat command even shows the inode number of the file. You can see the timestamps of a file using the stat command in the following manner: stat abhi.txt change time – last time when file metadata (file permission, ownership etc) was changed.modify time – last time when a file was modified.access time – last time when a file was accessed.For the sake of a quick recall, I’ll list the timestamps here: I would recommend reading it for a better and clearer understanding. I have written about timestamps in Linux in detail in an earlier article. The syntax for touch command is quite simple: touch file What are file timestamps in Linux, again? You can update the timestamps or modify them to a date in the past. With the touch command, you can change access, modify and change time of files and folders 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |