Adam
Adam

Reputation: 20962

Get last modified file date in a folder structure

I'm trying to get the most recently modified file's (datetime - as a unixtimestamp) from a folder structure. There are many files but I only need the datetime of the most recently updated.

I'ved tried the following but I think I'm way of the mark:

stat --printf="%y %n\n" $(ls -tr $(find * -type f))

Upvotes: 0

Views: 635

Answers (2)

Darren Douglas
Darren Douglas

Reputation: 196

Try this:

ls -trF | grep -v '\/\|@' | tail -1 | xargs -i date +%s -r {}

ls -trF gives you symbols to filter out, '/' for directories and '@' for links. After that, grep out those files, pick the last one, and pass it to date command.

EDIT: Of note as well is the date -r option, which will display the last modified date of file given as argument.

Upvotes: 1

matchew
matchew

Reputation: 19675

something like this?

ls -ltr | tail -n1 | awk '{print "date -d\"" $6FS$7FS$8 "\" +%s"}' | sh

EDIT:

actually better yet,try the following

find -type f -exec ls -l --time-style +%s {} \+ | sort -n -k6 | tail -n1

this will iterate over the folder structure you desired, print the time as a unix timestamp and sort it so the newest is at the end. (hence tail -n1)

Upvotes: 1

Related Questions