Sponsored Content
Full Discussion: mtime, ctime, and atime
Top Forums UNIX for Beginners Questions & Answers Answers to Frequently Asked Questions Tips and Tutorials mtime, ctime, and atime Post 302134305 by Perderabo on Thursday 30th of August 2007 09:13:22 AM
Old 08-30-2007
Using perl to display the file timestamps

The ls program will display mtime if you use "ls -l". And you can get atime or ctime with "ls -lu" or "ls -lc". But ls uses a strange format. It displays the month and day in all cases. If the timestamp is recent, it also displays hour and minute. If the timestamp is older than 6 months, it display the year instead of hour and minute. A clever script can reformat this to year, month, day, hour, and minute. But ls will not display the seconds. The gnu version of ls (which is usually the only version on linux) does have extended options like --fulltime. But these extended options are non-standard and won't be available on other versions of Unix.

The perl language is also non-standard, but perl tends to be available on most versions of unix. For example, a version of perl is supplied with HP-UX and Solaris. Perl can easily display the timestamps of files. Here are some perl one-liners to display atime, mtime, and ctime.


Code:
$ echo hello > testfile ; date
Thu Aug 30 08:31:57 EDT 2007
$ chmod 700 testfile ; date
Thu Aug 30 08:32:48 EDT 2007
$ cat testfile ; date
hello
Thu Aug 30 08:33:30 EDT 2007
$
$
$
$
$ perl -e '@d=localtime ((stat(shift))[8]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile
20070830083330
$ perl -e '@d=localtime ((stat(shift))[9]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile
20070830083157
$ perl -e '@d=localtime ((stat(shift))[10]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' testfile
20070830083248
$

These 4 Users Gave Thanks to Perderabo For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

mtime vs ctime

:D i have a slight problem and would appreciate if someone could clarify the confusion.. i use find alot and so far i have done ok.. but it just struck me a couple of days ago that I am not quite sure what the difference between the modification time and the change time as in ctime and mtime and... (3 Replies)
Discussion started by: moxxx68
3 Replies

2. UNIX for Dummies Questions & Answers

atime, ctime, mtime somewhere along csize..

i have used all forms of the unix find command.. and right now this is the only command i can think of that might have this option..: if i use mtime i am looking at a time interval.. but if i wanted to find out intervals of access, change and modification according to when a file changed size... (4 Replies)
Discussion started by: moxxx68
4 Replies

3. Programming

read() without changing atime ?

Hey, First of all I want to know How do I see the atime of a file ?? Whats the command ?? I think ls -l shows the last modified time right ? Because when I use cat to read a file, the timestamp shown by ls -l does not change. Its not ls -lu ! man ls did not help ! How do I see the last... (8 Replies)
Discussion started by: tantric
8 Replies

4. Shell Programming and Scripting

how to find ot ctime , mtime ,atime

Can any one tell me how to find out ctime , mtime ,atime for a file/directory on unix. Cheers, Nilesh (5 Replies)
Discussion started by: nilesrex
5 Replies

5. UNIX for Dummies Questions & Answers

(find) mtime vs. (unix) mtime

Hi I've made some test with perl script to learn more about mtime... So, my question is : Why the mtime from findfind /usr/local/sbin -ctime -1 -mtime -1 \( -name "*.log" -o -name "*.gz" \) -print are not the same as mtime from unix/linux in ls -ltr or in stat() function in perl : stat -... (2 Replies)
Discussion started by: hiddenshadow
2 Replies

6. UNIX for Advanced & Expert Users

mtime VS atime VS ctime

hi, in trying to maintain your directories, one needs to do some housekeeping like removing old files. the tool "find" comes in handy. but how would you decide which option to use when it comes to, say, deleting files that are older than 5 days? mtime - last modified atime - last accessed... (4 Replies)
Discussion started by: pinoy43v3r
4 Replies

7. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

8. UNIX for Advanced & Expert Users

Why updating atime doesn't update ctime?

Hi, ctime is the inode change time. If reading a file, its atime will be updated, which should cause inode member i_atime changed, which is an inode change. So ctime should also be updated. But if I try to ls a directory on redhat, only the directory atime gets updated, not ctime. Why? THANKS! (2 Replies)
Discussion started by: password636
2 Replies

9. UNIX for Dummies Questions & Answers

Ls -l displays ctime or mtime?

commands ls -l or just l displays ctime (changed time) or mtime (modified time)? (10 Replies)
Discussion started by: rupeshkp728
10 Replies

10. Solaris

Zfs - what does atime update?

It is widely documented that on zfs atime updates the access time on zfs. Where is the access time updated on Solaris 11.2? If I create file atimetest.txt under rpool/export/home: # zfs list rpool/export/home NAME USED AVAIL REFER MOUNTPOINT rpool/export/home 13.3G ... (5 Replies)
Discussion started by: jabberwocky
5 Replies
STAT(3) 								 1								   STAT(3)

stat - Gives information about a file

SYNOPSIS
array stat (string $filename) DESCRIPTION
Gathers the statistics of the file named by $filename. If $filename is a symbolic link, statistics are from the file itself, not the sym- link. lstat(3) is identical to stat(3) except it would instead be based off the symlinks status. PARAMETERS
o $filename - Path to the file. RETURN VALUES
stat(3) and fstat(3) result format +--------+--------------------------------------+---+ |Numeric | | | | | | | | | Associative | | | | | | | | Description | | | | | | +--------+--------------------------------------+---+ | 0 | | | | | | | | | dev | | | | | | | | device number | | | | | | | 1 | | | | | | | | | ino | | | | | | | | inode number * | | | | | | | 2 | | | | | | | | | mode | | | | | | | | inode protection mode | | | | | | | 3 | | | | | | | | | nlink | | | | | | | | number of links | | | | | | | 4 | | | | | | | | | uid | | | | | | | | userid of owner * | | | | | | | 5 | | | | | | | | | gid | | | | | | | | groupid of owner * | | | | | | | 6 | | | | | | | | | rdev | | | | | | | | device type, if inode device | | | | | | | 7 | | | | | | | | | size | | | | | | | | size in bytes | | | | | | | 8 | | | | | | | | | atime | | | | | | | | time of last access (Unix timestamp) | | | | | | | 9 | | | | | | | | | mtime | | | | | | | | time of last modification (Unix | | | | timestamp) | | | | | | | 10 | | | | | | | | | ctime | | | | | | | | time of last inode change (Unix | | | | timestamp) | | | | | | | 11 | | | | | | | | | blksize | | | | | | | | blocksize of filesystem IO ** | | | | | | | 12 | | | | | | | | | blocks | | | | | | | | number of 512-byte blocks allocated | | | | ** | | | | | | +--------+--------------------------------------+---+ * On Windows this will always be 0. ** Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1. In case of error, stat(3) returns FALSE. Note Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB. ERRORS
/EXCEPTIONS Upon failure, an E_WARNING is emitted. EXAMPLES
Example #1 stat(3) example <?php /* Get file stat */ $stat = stat('C:phpphp.exe'); /* * Print file access time, this is the same * as calling fileatime() */ echo 'Access time: ' . $stat['atime']; /* * Print file modification time, this is the * same as calling filemtime() */ echo 'Modification time: ' . $stat['mtime']; /* Print the device number */ echo 'Device number: ' . $stat['dev']; ?> Example #2 Using stat(3) information together with touch(3) <?php /* Get file stat */ $stat = stat('C:phpphp.exe'); /* Did we failed to get stat information? */ if (!$stat) { echo 'stat() call failed...'; } else { /* * We want the access time to be 1 week * after the current access time. */ $atime = $stat['atime'] + 604800; /* Touch the file */ if (!touch('some_file.txt', time(), $atime)) { echo 'Failed to touch file...'; } else { echo 'touch() returned success...'; } } ?> NOTES
Note Note that time resolution may differ from one file system to another. Note The results of this function are cached. See clearstatcache(3) for more details. Tip As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to "Supported Protocols and Wrappers" to determine which wrappers support stat(3) family of functionality. SEE ALSO
lstat(3), fstat(3), filemtime(3), filegroup(3). PHP Documentation Group STAT(3)
All times are GMT -4. The time now is 01:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy