Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 dont want to show in the sort

share|improve this question
I demo how to exclude the extension in my answer. – mikeserv Apr 2 '14 at 4:51
up vote 8 down vote accepted
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

share|improve this answer
Can i exclude an file extension from the folder.... – Danny Apr 2 '14 at 4:44
@Danny Yes, that's easy with find: find -not -iname '*.ext'. – n.st Apr 2 '14 at 6:13
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 – Danny Jun 9 '14 at 7:21

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.

share|improve this answer
this give me folder wise where as what I need is all together what are the recently modified files – Danny Apr 2 '14 at 4:45

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)
share|improve this answer
This is the only answer here that works for Solaris 10, thanks! :) – laxxy Feb 9 at 22:19

Assuming you are usuig GNU find, try:

find $SOMEPATH -exec stat -c '%Y %n' '{}' + | sort -n
share|improve this answer
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. – n.st Apr 1 '14 at 23:53
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... – yeti Apr 2 '14 at 4:43
Btw... not every find has -printf... – yeti Apr 2 '14 at 4:49
@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. – mikeserv Apr 2 '14 at 6:00
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... – yeti Apr 2 '14 at 6:06

To robustly list the filenames only using recent GNU tools:

find . -printf '%A@ %p\0' |
  sort -nz |
  sed -z 's/^[^ ]* //' |
  tr '\0' '\n'
share|improve this answer
find $DIR -depth -maxdepth 3 \
    -type d -readable -printf \
    'printf "\\n%p\\n"
    ls -t --color=always "%p"\n' |\
    . /dev/stdin 2>&-

This avoids any argument list problems because the only argument ls will ever receive is the name of the directory you want listed. You can do this with anything you like.

The shell just . sources the |pipe as a shell script - it's the same process and doesn't have the issues you can encounter when execve is called.

In any case - the above only goes three-deep which is adjustable by changing maxdepth. It also deep first - so you work your way backwards to your current directory from depth.

You might notice you also get $LSCOLORS - on my machine it also provides neatly printed columns and everything else you would want from ls - or, for that matter, anything you can imagine you'd wanna do with . source.


Again, this'll do anything you expect of ls. So hiding .svn files is as simple as changing the ls line to:

ls -t --color=always --hide="*svn" "%p"\n

Or if you want to see svn extensions in every folder BUT ./svn you could change it so the whole command looks like this:

    find $DIR -depth -maxdepth 3 \
        -type d -readable -printf \
            'printf "\\n%p\\n" ; hide=
            [ "%p" = "./svn" ] && hide="*svn"
            ls -t --color=always --hide="$hide" "%p"\n' |\
    . /dev/stdin 2>&-

Personally, I like it with ls's -s and, if you care, -u will sort by access time rather than mod time.

share|improve this answer

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.

share|improve this answer
true, I forgot about that – rMistero Apr 1 '14 at 23:26
for reference about command: arg list too long in-ulm.de/~mascheck/various/argmax – rMistero Apr 1 '14 at 23:36
@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. – Scott Apr 29 at 2:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.