Andrew
Andrew

Reputation: 917

List all files (with full paths) in a directory (and subdirectories), order by access time

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.

Upvotes: 21

Views: 69906

Answers (5)

ericj
ericj

Reputation: 2301

pwd | xargs -I % find % -type f

Upvotes: 2

Ron
Ron

Reputation: 798

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.

Upvotes: 1

Daenyth
Daenyth

Reputation: 37461

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.

Upvotes: 3

Alex
Alex

Reputation: 11100

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

Upvotes: 20

Kent
Kent

Reputation: 195209

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

Upvotes: 11

Related Questions