21

I'd like to construct a Linux command to list all files (with their full paths) within a specific directory (and subdirectories) ordered by access time.

ls can order by access time, but doesn't give the full path. find gives the full path, but the only control you have over the access time is to specify a range with -atime N (accessed at least 24*N hours ago), which isn't what I want.

Is there a way to order by access time and get the full path at once? I could just write a script, but it seems there should be a way to do this with the standard Linux programs.

Andrew
  • 897
  • 1
  • 9
  • 19

5 Answers5

20
find . -type f -exec ls -l {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7

This will find all files, and sort them by date and then time. You can then use awk or cut to extract the dates and files name from the ls -l output

Alex
  • 10,470
  • 8
  • 40
  • 62
  • Works quite well. I take it that this isn't limited by getconf ARG_MAX? – Andrew Mar 10 '12 at 00:04
  • Right, it's not limited by ARG_MAX because it doesn't pass it as arguments, but pipes the output instead. There's no limit to how much data can be piped between programs. – Alex Mar 11 '12 at 17:22
  • Great! Thanks, mate! Very good to know about that limitation as my directory has many, many files in it. – Andrew Mar 12 '12 at 12:16
  • I did this a little different to always use the best time format 'YYYY-MM-DD HH:MM:SS': `find . -type f -exec ls -l --time-style=+"%Y-%m-%d %H:%M:%S" {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7` – loco.loop Feb 19 '18 at 23:45
11

you could try:

 ls -l $(find /foo/bar -type f )
  • you can add other options (e.g. -t for sorting) to ls command to achieve your goal.
  • also you could add your searching criteria to find cmd
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 1
    I slightly modified this and got exactly what I want with ls -ult $(find DIRECTORY -type f ) | grep -o "[^ ]\+$" Thanks! – Andrew Mar 08 '12 at 17:09
  • 2
    Keep in mind that so as the output of `$(find DIRECTORY -type f )` is used as the command line argument to `ls -ult`, you'll be limited to `getconf ARG_MAX` files – Alex Mar 08 '12 at 17:57
  • Thanks for the heads up, Alex! – Andrew Mar 09 '12 at 10:08
  • 1
    To do it in the current directory, just put `\`pwd\`` *(including those backticks, it's with the tilde `~` key)* instead of `/foo/bar`. And to avoid problems when filenames have spaces, use this: `find \`pwd\` -type f -print0 | xargs -0 ls -l` (you can add further options to `ls` and finally apply `| grep` if you want to filter the results). – ADTC Jan 22 '16 at 08:55
3

find . -type f | xargs ls -ldt should do the trick as long as there's not so many files that you hit the command like argument limit and spawn 2 instances of ls.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
2
pwd | xargs -I % find % -type f
ericj
  • 2,138
  • 27
  • 44
1
find . -type f -exec ls -l --full-time {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7

Alex's answer did not work for me since I had files older than one year and the sorting got messed up. The above adds the --full-time parameter which nuetralizes the date/time values and makes them sortable regardless of how old they are.

Ron
  • 798
  • 4
  • 13