Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Changing time

Submitted by on May 11, 2006 – 12:12 am

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.

About time

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.

Preserving time

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:[639] 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
Print Friendly, PDF & Email

Leave a Reply