Mac unix script: given a list of file paths, find the one with latest modified date? [closed]

StackOverflow https://stackoverflow.com/questions/20462234

  •  30-08-2022
  •  | 
  •  

Question

If I have a list of file names (absolute path), how do I determine which one is last modified in command line?

Thanks!

Was it helpful?

Solution

For OS X's stat:

tr \\n \\0<files.txt|xargs -0 stat -f'%m %N'|sort -rn|head -n1

Use -c'%Y %n' with GNU stat.

Different ways to find files sorted by modification date:

find . -type f -exec stat -f'%m %N' {} +|sort -rn|cut -d' ' -f2-
  for OS X's stat; use -c'%Y %n' with GNU stat
gfind -type f -printf '%T@ %p\n'|sort -rn|cut -d' ' -f2-
  %T@ is absolute modification time and %p is pathname
zsh -o dotglob -c 'printf %s\\n **/*(.om)'
  . is a qualifier for regular files
  om orders files by modification time

OTHER TIPS

Try this:

find . -type f | xargs stat --format '%Y :%y %n' | sort -nr | cut -d' ' -f5- | head


find . -type f # find all FILES only in the current directory
xargs stat --format '%Y :%y %n' # perform a stat on the file 
                                # ( xargs helps in 'stringing together commands')
                                # %Y time of last modification since Epoch
                                # %y time of last modification human readable
                                # %n file name
sort -nr                        # -n, sort according to numerical value, -r reverse
cut -d' ' -f5                   # cut output by ' ' (space) and print column five
head                            # show the first 10 lines
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top