Saturday, July 21, 2012

Tutorial on Time Stamps in Unix


Tutorial on Time Stamps in Unix

Unix Tips By Rahul Miglani
Unix Tips By Rahul Miglani
As a beginner we must know that , we have 3 different time stamps in Unix. Each file has three time stamps, which record the last time that certain operations were performed on the file. You can search for files whose time stamps are within a certain age range or compare them to other time stamps.

1: Modify Time:

This is last time when the actual contents of the file were modified.
We can get the list of files modified n days using find command.
find . –type f –mtime +n -exec ls –l {} \;
Where:
-n : files modified between today to n-1 days.
n : files that was modified on that particular n day.
+n : files modified from n+1 days back .
Needless to say that we get modified time of file using ls –lt command 
Another example would be : Finding 15 Days old files
find *.* -mtime -15
You can play with these files now : Deleting these 15 Days old files
find *.* -mtime -15 -exec rm -rf {}\;

2: Change time:

Change time is something like altering the label of the package (file) whereas modify time is altering the content of the file. So in Unix we can say, change time is last time change in inode of the file and inodechanges when we update the file, change permission, rename file, change in owner etc.
So whenever mtime change, ctime also does (change in content of file also update inode) but ctime change some extra time as explained above.
We use –ctime in find to get list of files changed in particular days back. ls –lc command is used to get the change time of a particular file.
Here file was zipped (renamed), means inode information of the file has been modified.
username@servername:username> ls -lt TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Dec 16 14:18 TestScript.sh.gz
username@servername:username> ls -lc TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:29 TestScript.sh.gz
Here ls –lc gives the time when the file was zipped.
Another example would be the use of find command with ctime
find *.* -ctime -15
or may be you want to delete these files.
find *.* -ctime -15 -exec rm -rf {}\;

3: Access Time:

It’s a time the file was last accessed means the file was read. Last time we read that file.
username@servername:username> ls -lu TestScript.sh.gz
-rwxrwxr-x 1 username aimsys 581 Jan 25 07:43 TestScript.sh.gz
Example: Finding files which were accessed with in 15 days.
find *.* -atime -15
You can play with these files now : Deleting these 15 Days old files which were accessed (Not Modified)
find *.* -atime -15 -exec rm -rf {}\;
NOTE: With find you can choose to type -print or not with the find command, because the basic principle of find is obviously to print what ever it is doing so you may choose to type -print .To know more about the find command you can type man find on unix, to know what has been written about the find in Unix.

Summary :

Access Time (atime ) : This is the time that the file was last accessed, read or written to.
Modify Time (mtime ) :This is the last time the actual contents of the file were last modified.
Change Time (ctime ):This is the time that the inode information (permissions, name, etc., the metadata, as it were) was last modified.
Enjoying Playing with the times in Unix.

No comments:

Post a Comment