Unix command (other than 'stat' and 'ls') to get file modification date without parsing
Asked Answered
S

4

13

I am writing a shell script in which I have to find the last modification date of a file.

Stat command is not available in my environment.

So i am using 'ls' as below to get desired result.

ls -l filename | awk '{print $6 $7 $8}'

But I have read in many forums that parsing ls is generally considered bad practise. While it (probably) works fine most of time, it's not guaranteed to work everytime.

Is there any other way to get file modification date in shell script.

Stormi answered 26/6, 2012 at 21:20 Comment(0)
A
21

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

Alikee answered 26/6, 2012 at 21:34 Comment(3)
If stat isn't available, there is a good chance find -printf option isn't either. stat isn't a standard Unix command while -printf is a Gnu extension.Aracelis
@Aracelis well, hopefully OP will let us know if this is a workable solution or not. If not, we can think of other options.Alikee
OSX (10.9): We have stat but no printf.Catt
K
6

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
Ka answered 5/1, 2015 at 19:30 Comment(1)
date -r is not available on OS X :(Anaphrodisiac
K
4

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";
    }
}
Kelliekellina answered 26/6, 2012 at 22:0 Comment(0)
T
0
#!/bin/bash 
FILE=./somefile.txt

modified_at=`perl -e '$x = (stat("'$FILE'"))[9]; print "$x\n";'`

not_changed_since=`perl -e '$x = time - (stat("'$FILE'"))[9]; print "$x\n";'`

echo "Modified at $modified_at"
echo "Not changed since $not_changed_since seconds"
Tracitracie answered 1/7, 2016 at 9:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.