Directory "recursive" last modified date

16,378

Solution 1

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

Solution 2

Given the and 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.

Solution 3

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])'

Solution 4

find . -type f -exec ls -ltr {} \; | awk '{print $6"-"$7"-"$8" "$9}' | sort -k3 | sort -nr | head -1

Output format is Month-date-hour-minute filename

Dec-30-03:10 ./1

Since the file was created this year, the year is not shown. The code can be modified to always show the year if you always want the year shown.

Share:
16,378
user3450548
Author by

user3450548

Updated on September 18, 2022

Comments

  • user3450548
    user3450548 over 1 year

    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 :)

    • ilkkachu
      ilkkachu over 6 years
      Do you want just the latest timestamp, or the corresponding file name too? :)
  • ilkkachu
    ilkkachu over 6 years
    I think find -printf is also GNU-specific, but it's probably the easiest way to get the timestamps. But the usual problems with newlines in file names come to play here, too
  • Jeff Schaller
    Jeff Schaller over 6 years
  • user3450548
    user3450548 over 6 years
    I find the solution short and elegant, however I'm not sure of the proposed answers which one is the fastest..
  • user3450548
    user3450548 over 6 years
    Oh I need the output be in Epoch timestamp.. it can be done directly inside the find ?
  • Dave Rove
    Dave Rove over 5 years
    Great solution that I'll use. A minor correction for the epoch timestamp would be numeric sorting, sort -n, otherwise smaller epoch numbers that start with a 9 (year 2000, say) could be picked.