Images of Ls (UNIX)
Sistemas Operativos de red UNIX Novell Netware LANtastic LAN Manager NetWare Lite / Personal NetWare GNU/Linux Windows NT Mac OS X Server 1.0 (Rhapsody) Windows 2000 Server
unix architecture, architecture of unix, unix architecture diagram, unix kernel architecture, unix os architecture
How To Update atime and mtime for a File in UnixUnix TutorialIf you remember, all files and directories in Unix filesystems have three timestamps associated with them – atime, ctime and mtime. Since questions about modifying access time (atime) and modification time (mtime) are quite frequent in my website logs, I thought I’d explain how it is done.How to view atime, ctime and mtimeBefore we go any further, I’d like to remind you that using stat command is probably the easiest way to look at all the three timestamps associated with each file:ubuntu$ stat ./tryFile: `./try'Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: 801h/2049d Inode: 655596 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)Access: 2008-11-17 05:01:16.000000000 -0600Modify: 2008-11-17 05:01:16.000000000 -0600Change: 2008-11-17 05:01:16.000000000 -0600Even though ls command can be used to view the same times, we will depend on the stat command for today’s post simply because it shows all the times together – it’s great for explanations.Modifying atime and mtimeThere’s a very simple way to update either atime or mtime for a given file, or even both at the same time: you should use the touch command.Here’s how it can be used to update the atime:ubuntu$ touch -at 0711171533 ./tryThe -a in the command line parameters refers to atime, while -t and the following sequence are nothing but a timestamp we want assigned to the file. In my example, 0711171533 means this:07 – year of 200711 – November17 – 17th1533 – time of the day, 15:33 Now, if we run stat command again, you can see how the access time field got updated:ubuntu$ stat ./tryFile: `./try'Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: 801h/2049d Inode: 655596 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)Access: 2007-11-17 15:33:00.000000000 -0600Modify: 2008-11-17 05:01:16.000000000 -0600Change: 2008-11-17 05:01:48.000000000 -0600Similarly, we can set the mtime, in my particular example it’s the future – a day exactly one year from now.-m is the command line option to specify that mtime is our main focus:ubuntu$ touch -mt 0911171533 ./tryubuntu$ stat ./tryFile: `./try'Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: 801h/2049d Inode: 655596 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)Access: 2007-11-17 15:33:00.000000000 -0600Modify: 2009-11-17 15:33:00.000000000 -0600Change: 2008-11-17 05:05:41.000000000 -0600Changing atime and mtime to the current Unix timeIt’s probably useful to know that the default behavior of the touch command is to update both access time and modification time of a file, changing them to the current time on your system. Here’s what will happen if I run touch against the same file we used in all the examples:ubuntu$ touch ./tryubuntu$ stat ./tryFile: `./try'Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: 801h/2049d Inode: 655596 Links: 1Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)Access: 2008-11-17 05:09:33.000000000 -0600Modify: 2008-11-17 05:09:33.000000000 -0600Change: 2008-11-17 05:09:33.000000000 -0600As you can see, all three fields have been reset to the new (current time) value. That’s it for today, I hope this solved another one of your Unix mysteries!See alsomtimeatime, ctime and mtime in Unixfind out file permissions using Perl scriptfind mtime: identify recent files in Unixtime and date in Unix scripts var disqus_config = function () { this.page.url = 'https://www.unixtutorial.org/how-to-update-atime-and-mtime-for-a-file-in-unix/'; }; (function() { var d = document, s = d.createElement('script'); s.src = '//glebreys.disqus.com/embed.js'; s.setAttribute('data-timestamp', +new Date()); (d.head || d.body).appendChild(s); })(); Please enable JavaScript to view the comments powered by Disqus.