Get Directory Recursive Last Modified Date – Bash Script Guide

bashfilesshell-scripttimestamps

I would like to know a command or function in linux bash to get the timestamp (and maybe format its output) of the last modified file inside a directory.

Let's say I have /path/mydir and this directory has a big bunch of files inside. Given its path, I want to output the timestamp of the most recent modified file.

I guess that the procedure could be do a foreach file recursive, check them all and update a variable with the most recent time each time a more recent one is found.

Edit: Sorry for the confusion but I wanted to intend the Epoch timestamp 🙂

Best Answer

One option: use GNU find to recurse through all files; print timestamp with filepath and sort by date:

find /path/mydir -type f -printf "%T+\t%p\n" | sort | tail -1

For just the epoch timestamp,

find /path/mydir -type f -printf "%T@\n" | sort | tail -1
Related Question