Sort the files in the directory recursively based on last modified date
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
Related videos on Youtube
Danny
Updated on September 18, 2022Comments
-
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 over 10 yearsI demo how to exclude the extension in my answer.
-
-
UnX over 10 yearstrue, I forgot about that
-
UnX over 10 yearsfor reference about command: arg list too long in-ulm.de/~mascheck/various/argmax
-
n.st over 10 yearsYou 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 over 10 yearsKnowing 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 over 10 yearsCan i exclude an file extension from the folder....
-
Danny over 10 yearsthis give me folder wise where as what I need is all together what are the recently modified files
-
Admin over 10 yearsBtw... not every find has
-printf
... -
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 over 10 yearsI met lots of linux users never having read about
stat
. So sometimes I just want to direct some attention onstat
... I thinkstat
deserves it... -
n.st over 10 years@Danny Yes, that's easy with find:
find -not -iname '*.ext'
. -
Danny about 10 yearsfind -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 over 8 yearsThis is the only answer here that works for Solaris 10, thanks! :)
-
Scott - Слава Україні about 8 years@Gilles: You mean
xargs -0
(in both cases).xargs -print0
is equivalent toxargs -p -r -i -n -t -0
orxargs -inprt0
orxargs -0ntrip
, which is probably not what you meant. -
user3426706 over 3 yearsI get
find: illegal option -- p
-
nealmcb about 3 yearsNice!! Note, adding
-f
at the beginning of thefind
command restricts it to just files, which is very helpful to avoid e.g. the directory cruft in~.cache/pip/http