Unix systems record several time parameters associated with files. Sometimes you might need to set time parameters on a file to a particular value. Or you might need to modify a file without changing any of the timestamps. This may be necessary for backups or for managing NIS maps.
All time parameters associated with a file can be viewed using the ls or stat commands. Unix systems record the following time attributes of a file:
atime – access time; the time a file or directory data was accessed
mtime – modification time; the last time a file or directory data was modified
ctime – inode time; the last time inode status was modified
An inode is the data structure on the disk that describes a file and contains such information as owner, permissions, access and modification time. For example, if you change permissions on a file without modifying a file itself, the ctime value will change but the mtime value will stay the same. On the other hand, if you modify the filename or the file’s contents, the ctime will not change but the mtime will reflect the time of the modification.
If, for example, you use touch command to modify a file’s mtime value, the ctime will also change to reflect the time of this modification. Another difference between mtime and ctime is that users are allowed to modify the mtime value of files and directories they own. The ctime value of the same files cannot be modified by anyone but root.
The atime value simply shows when the file was last accessed for reading, writing or execution.
Here is a quick example of various timestamps available under Solaris. In this example we are looking at the /etc/passwd file which is accessed every time a user logs in and is modified every time a new user is added for access to the system.
# Show access time ls -laur /etc/passwd -rw-r--r-- 1 root sys 945 May 11 01:15 /etc/passwd # Show inode modification time ls -lacr /etc/passwd -rw-r--r-- 1 root sys 945 May 10 06:15 /etc/passwd # Show modification time ls -latr /etc/passwd -rw-r--r-- 1 root sys 945 Oct 12 2005 /etc/passwd
Various operating systems use different parameters with the ls command. The above example is for Solaris.
Let’s say we want to modify the /etc/passwd file while preserving atime, mtime, and ctime. First, make a copy of the original file:
cp -p /etc/passwd /etc/passwd.orig
After editing /etc/passwd, restore the atime and mtime values from the original file:
touch -r /etc/passwd.orig /etc/passwd
Editing the file does not change its ctime value. However, if you want to chmod a file without changing the ctime value, then you have your work cut out for you. The simplest, but also the most destructive way of doing this is to change the system time to match the ctime value of that file, chmod it, and then change system time back to what it should be.
Here’s an example of how this would work on Solaris. In this example we chmod /etc/passwd while preserving its original ctime value.
jtripper - 13:25:40 /tmp: ls -lacr /etc/passwd -rw-r--r-- 1 root sys 800 Jan 5 15:21 /etc/passwd CTIME="010515212006" CURTIME=$(date -u '+%m%d%H%M%Y') date -u $CTIME chmod +x /etc/passwd date -u $CURTIME