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.
How about using the
findcommand?e.g.,
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
printfto tailor the output to what you need/want. Section-printf formatcontains all the details.Compare
lsoutput tofind: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:
or if combined with other shell commands:
both return:
Wed Jun 13 00:05:29 2012