Directory "recursive" last modified date

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

Given the linux and bash tags, as well as the "linux bash" specification in the question, here's a bash-specific function that will output both the filename and the timestamp of the most-recently modified file in a given directory structure:

function lastmodified () (
  shopt -s globstar
  latest=
  t=0
  for f in "$1"/**
  do
    x=$(stat -c "%Y" "$f")
    if [ $x -gt $t ]
    then
      latest="$f"
      t="$x"
    fi
  done
  printf "%s\n" "$latest"
  printf "%s\n" "$(date -d @${t})"
)

Use it like: lastmodified /path/mydir.

It runs in a subshell to isolate the globstar shell option as well as the various variable assignments. Change or remove the printf statements to capture the data you're interested in.

It works by asking bash to expand all of the filename paths under the given $1 parameter, then checks the "Time of last modification as seconds since Epoch" with the stat command. The most recent filename will end up in $latest, and its timestamp in t.


Make sure that zsh is installed (all major distributions have a package for it).

zsh -c 'ls -log **/*(.om[1])' | awk '{print $4, $5, $6}'

**/* is a wildcard pattern to match all files in subdirectories recursively and (.om[1]) are glob qualifiers to limit the matches to regular files (.), sort by modification time (om) and keep only the most recent file ([1]). Glob qualifiers are a unique zsh feature and much of its functionality, in particular sorting, is hard to reproduce in other shells.

I include only regular files because this is often what is needed — but you may want to remove the . qualifier if you want the timestamp to reflect the last time a file was deleted, for example.

GNU ls has an option to control the time format (--time-style). Note that depending on the time format, you may need to adjust the post-processing to extract the time field.

Alternatively, you can use Linux's stat command or zsh's stat builtin to print the timestamp in your desired format.

zsh -c 'zmodload zsh/stat; stat +mtime -- **/*(.om[1])'