Unix command (other than 'stat' and 'ls') to get file modification date without parsing
How about using the find
command?
e.g.,
$ find filenname -maxdepth 0 -printf "%TY-%Tm-%Td %TH:%TM\n"
This particular format string gives output like this: 2012-06-13 00:05
.
The find man page shows the formatting directives you can use with printf
to tailor the output to what you need/want. Section -printf format
contains all the details.
Compare ls
output to find
:
$ ls -l uname.txt | awk '{print $6 , "", $7}'
2012-06-13 00:05
$ find uname.txt -maxdepth 0 -printf "%TY-%Tm-%Td %TH:%TM\n"
2012-06-13 00:05
Of course you can write scripts in any number of languages such a Python or Perl etc, to get the same information, however asking for a "unix command" sounded as if you were looking for a "built-in" shell command.
EDIT:
You could also inovke Python from the command line like this:
$ python -c "import os,time; print time.ctime(os.path.getmtime('uname.txt'))"
or if combined with other shell commands:
$ echo 'uname.txt' | xargs python -c "import os,time,sys; print time.ctime(os.path.getmtime(sys.argv[1]))"
both return: Wed Jun 13 00:05:29 2012
depending on your OS you could just use
date -r FILENAME
The only version of unix this doesn't appear to work on is Mac OS, where according to the man files, the -r option is :
-r seconds
Print the date and time represented by seconds, where seconds is
the number of seconds since the Epoch (00:00:00 UTC, January 1,
1970; see time(3)), and can be specified in decimal, octal, or
hex.
instead of
-r, --reference=FILE
display the last modification time of FILE
Do you have perl?
If so, you can use its built-in stat
function to get the mtime (and other information) about a named file.
Here's a small script that takes a list of files and prints the modification time for each one:
#!/usr/bin/perl
use strict;
use warnings;
foreach my $file (@ARGV) {
my @stat = stat $file;
if (@stat) {
print scalar localtime $stat[9], " $file\n";
}
else {
warn "$file: $!\n";
}
}
Sample output:
$ ./mtime.pl ./mtime.pl nosuchfile
Tue Jun 26 14:58:17 2012 ./mtime.pl
nosuchfile: No such file or directory
The File::stat
module overrides the stat
call with a more user-friendly version:
#!/usr/bin/perl
use strict;
use warnings;
use File::stat;
foreach my $file (@ARGV) {
my $stat = stat $file;
if ($stat) {
print scalar localtime $stat->mtime, " $file\n";
}
else {
warn "$file: $!\n";
}
}