How can I programmatically get a folders latest edit?

I have already written the following script:

my @ls_command_result=qx(ls -l);
my @the_folder=grep /folder_i_want/, @ls_command_result;
print STDOUT "@the_folder";

Using this script i can get the latest edit date of the folder, but the problem is that I just get it as a string, but i will be needing to compare this outcome to other dates and therefore it would be vastly better to have it in the format of say, epoch second.

How would it be best to go about getting this information?

  • 4
    Why to run external command when perl can give you the information? stat – manatwork Aug 9 '13 at 14:25
  • 4
    And don't parse ls, use glob. – Mat Aug 9 '13 at 14:27

If you are using ls in the GNU coreutils package you can get the time of last modification in seconds since 1970-01-01 00:00:00 UTC with the --time-style=+%s and -d options to ls for the directory you are interested in:

ls -d --time-style=+%s /path/to/folder | cut -f 6 -d " "
  • This option is not in POSIX and is not generally available. – enzotib Aug 9 '13 at 14:25
  • @enzolib, thanks for the comment, I have updated the answer – Miguel de Val-Borro Aug 9 '13 at 14:32
  • I tested it and it does give me the time but still just as a string, what i was after was the epoch second of last modification. But thanks. – Hermann Ingjaldsson Aug 9 '13 at 14:34

The answer, which manatwork pointed out.. is to use stat.

  • As you probably already implemented it, better post the perl alternative of the code from the question. Detailed answers used to get more upvotes. – manatwork Aug 9 '13 at 16:27
  • Why is the question not flagged with the perl tag? – Miguel de Val-Borro Aug 14 '13 at 16:52
  • added the perl tag. – Hermann Ingjaldsson Aug 15 '13 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.