Sort the files in the directory recursively based on last modified date

30,687

Solution 1

find -printf "%TY-%Tm-%Td %TT %p\n" | sort -n

will give you something like

2014-03-31 04:10:54.8596422640 ./foo
2014-04-01 01:02:11.9635521720 ./bar

Solution 2

If you want to flatten the directory structure (thus sorting by date over all files in all directories, ignoring what directory the files are in) the find-approach suggested by @yeti is the way to go. If you want to preserve directory structure, you might try

$ ls -ltR /path/to/directory

which sorts directory based.

Solution 3

In bash, run shopt -s globstar first. In ksh93, run set -o globstar first. In zsh, you're already set.

ls -dltr **/*

This will return an error if you have so many files that the command line length limit on your system is exceeded. In zsh, you can use this instead:

print -rl -- **/*(Om)

Solution 4

This one will list all files in <dir> with topmost being oldest modified

find <dir> -type f -print0 | xargs -0 ls -ltr

And with this the latest modified is topmost

find <dir> -type f -print0 | xargs -0 ls -lt

Note that this only works if the list of file names doesn't exceed the total command line length limit on your system.

Solution 5

Assuming you are usuig GNU find, try:

find $SOMEPATH -exec stat -c '%Y %n' '{}' + | sort -n
Share:
30,687

Related videos on Youtube

Danny
Author by

Danny

Updated on September 18, 2022

Comments

  • Danny
    Danny almost 2 years

    Sort the files in the directory recursively based on last modified date

    I have modified a lot of files in my directory want to know what are those files by sorting them by the last modified date and in that I want some of the extensions to be excluded

    in the svn directory I have a lot of .svn files too which I don't want to show in the sort

    • mikeserv
      mikeserv over 10 years
      I demo how to exclude the extension in my answer.
  • UnX
    UnX over 10 years
    true, I forgot about that
  • UnX
    UnX over 10 years
    for reference about command: arg list too long in-ulm.de/~mascheck/various/argmax
  • n.st
    n.st over 10 years
    You could use find's -printf option to avoid invoking a separate process (or several, depending on the number of files). See my answer for an example.
  • Admin
    Admin over 10 years
    Knowing that there are other ways to do it, I wanted to give stat a chance and show -exec ... +. Really! Sometimes I think, stat deserves more attention... and noone should take recipes from here without reading about the ingredients and thinking about consequences...
  • Danny
    Danny over 10 years
    Can i exclude an file extension from the folder....
  • Danny
    Danny over 10 years
    this give me folder wise where as what I need is all together what are the recently modified files
  • Admin
    Admin over 10 years
    Btw... not every find has -printf...
  • mikeserv
    mikeserv over 10 years
    @yeti - If your find doesn't have -printf, you can use -exec sh -c 'printf...' to get much of the same functionality. That would include access to $(pwd) for fully qualified paths as well.
  • Admin
    Admin over 10 years
    I met lots of linux users never having read about stat. So sometimes I just want to direct some attention on stat... I think stat deserves it...
  • n.st
    n.st over 10 years
    @Danny Yes, that's easy with find: find -not -iname '*.ext'.
  • Danny
    Danny about 10 years
    find -not -path "svn" -not -name "*svn" -printf "%TY-%Tm-%Td %TT %p\n" | sort -n this is exactly what I needed thanks a lot @ n.st
  • laxxy
    laxxy over 8 years
    This is the only answer here that works for Solaris 10, thanks! :)
  • Scott - Слава Україні
    Scott - Слава Україні about 8 years
    @Gilles: You mean xargs -0 (in both cases).  xargs -print0 is equivalent to xargs -p -r -i -n -t -0 or xargs -inprt0 or xargs -0ntrip, which is probably not what you meant.
  • user3426706
    user3426706 over 3 years
    I get find: illegal option -- p
  • nealmcb
    nealmcb about 3 years
    Nice!! Note, adding -f at the beginning of the find command restricts it to just files, which is very helpful to avoid e.g. the directory cruft in ~.cache/pip/http