0

This question already has an answer here:

https://unix.stackexchange.com/a/240424/674 shows a way to find the three most recent changed files (directly or indirectly) under a directory.

find . -type f -exec stat -c '%Y %n' {} \; | sort -nr | awk 'NR==1,NR==3 {print $2}'

I try to find the three largest files under a directory by replacing stat -c '%Y %n' with stat -c '%B %n'. but it doesn't seem to work correctly. because:

 %b - Number of blocks allocated (see ‘%B’)
 %B - The size in bytes of each block reported by ‘%b’

My guess is that %b doesn't report the size of a file, but I am not sure.

So what shall I do?

marked as duplicate by Jeff Schaller, Romeo Ninov, G-Man, schily, sebasth Jun 16 '18 at 12:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

%b does report the size of the file, but it reports in blocks. That may or not be good enough for your purposes. You can always use ls -l to get bytes if you want:

find . -type f | xargs ls -l | sort -n -k5 | tail -n 3

If filenames contain white spaces, then the standard solution is

find . -type f -print0 | xargs -0 ls -l | ...

The -print0 makes find use a null byte as a separator between the names, which is then used as the separator with xargs -0.

  • Thanks. My filenames contain whitespaces. Would find . -type f -exec ls -lh {} \; | sort -h -k5 | tail -n 3 be better? – Tim Jun 14 '18 at 22:46
  • Maybe - I generally prefer xargs because I find the find -exec syntax yucky. I expanded the answer to include a solution that uses xargs. – NickD Jun 14 '18 at 23:24
  • If your find has -print0, it probably has -ls as well. – muru Jun 15 '18 at 2:33
  • Using xargs with find is an artefact from the 1980s. David Korn introduced execplus in 1989 for SVr4. The only find implementations without -ls are on AIX and HP-UX. Find -ls has been introduced in 1988 with SunOS/4.0 this is 30 years ago now... – schily Jun 15 '18 at 7:12

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