Size: 97 Blocks: 8 IO Block: 4096 regular fileĭevice: 801h/2049d Inode: 1278451 Links: 1Īccess: (0644/-rw-r-r-) Uid: ( 0/ root) Gid: ( 0/ root)īy specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours: ubuntu$ touch -r /etc/lsb-release. A reference file is the file which already has the timestamps you’d like to copy: ubuntu$ stat /etc/lsb-release tryĬhange: 04:07:12.000000000 -0600 Using a reference file to set atime and mtimeįinally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. If you’re interested in updating the modification date only, use -m option: ubuntu$ touch -mt 200510161000. Similar to the commands above, you can use -a option to make touch only update the access time field of a file: ubuntu$ touch -at 200010161000. Use stat again and we can see that the modification time is changed. Hence, we have modified the existing content with the new one. Next, we use the cat command to redirect some new content into file access. Below is shown the modification time of file access which is 22:24. tryĬhange: 03:57:30.000000000 -0600 Modifying atime of a file in Unix Now let’s try to change the modification time of a file. Here’s how easy it is to set atime and mtime to the Oct 16th, 2010 date: ubuntu$ touch -t 201010161000. If you fancy adding a year to the timestamp specification, you can specify something from both past and future. In other words, it’s set to Nov 18th 2008 because of the date of writing this example. The reason change time (ctime) is set to a different date is because this field reflects the last update to the inode behind a file, and always reflects the current time. tryĪs you can see from the output, both access time and modification time got updated. This example resets the date to October 16th: ubuntu$ touch -t 10161000. Template for the timestamp is quite thorough: YY]MMDDhhmm, but it’s entirely up to you whether to specify the year (either two-digit or a full form) or not. If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly accempt a timestamp template with -t command line option. Changing file timestamps to a specific date and time Size: 0 Blocks: 0 IO Block: 4096 regular empty fileĭevice: 801h/2049d Inode: 655596 Links: 1Īccess: (0644/-rw-r-r-) Uid: ( 1000/ greys) Gid: ( 113/ admin)Īs you can see from the example, the file which isn’t originally found, gets created by the touch command and gets its timestamps set to the current system time and date. If there isn’t a file with the specified name, touch command will create it for you if permissions allow it: ubuntu$ ls try You simply specify the filename as a command line parameter, no oother options are needed. The default behavior of touch command is to change all three timestamps associated with a file to the current system time. Changing timestamps of a time to the current system time Ranging from source control approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory of yours.Īfter all, it’s always useful to know when the file was last modified or when somebody tried to access its contents. There are quite a few legitimate reasons why you may want to update timestamps on a certain file. In short, it updates file timestamps – access and modification ones (atime and mtime respectively). If you need a time representation that is consistent across time zones, you should specify a time zone offset or UTC.Touch command is one of these little but extremely useful tools in Unix and Linux which you may have used for quite sometime before realizing their full potential. Please keep in mind that the above examples all assume local system time. If you want "safer" filenames (e.g., for compatibility with Windows), you can omit the colons from the time portion. Some examples of how to generate valid ISO 8601 datetime representations include: $ date +"%FT%T" If you don't have a requirement to use that specific date format, you might consider using an ISO 8601 compliant datetime format. You can avoid this issue by using 24-hour format timestamps instead. Please note that filenames containing 12-hour format timestamps will probably not sort by name the way you want them to sort. This results in the file 2016_04_25_10_30_AM.log (although, with the current datetime) being created with the md5 hash of /etc/mtab as its contents. If you want to use the current datetime as a filename, you can use date and command substitution.
0 Comments
Leave a Reply. |