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

  • Do you want just the latest timestamp, or the corresponding file name too? :) – ilkkachu Dec 28 '17 at 19:38

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

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

For just the epoch timestamp,

find /path/mydir -printf "%T@\n" | sort | tail -1
  • 1
    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 – ilkkachu Dec 28 '17 at 19:43
  • I find the solution short and elegant, however I'm not sure of the proposed answers which one is the fastest.. – user3450548 Dec 29 '17 at 0:21
  • Oh I need the output be in Epoch timestamp.. it can be done directly inside the find ? – user3450548 Dec 29 '17 at 0:52
  • 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. – Dave Rove Sep 10 '18 at 17:20

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

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
  for f in "$1"/**
    x=$(stat -c "%Y" "$f")
    if [ $x -gt $t ]
  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.

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.

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.