0

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?

1

1 Answer 1

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.

4
  • Thanks. My filenames contain whitespaces. Would find . -type f -exec ls -lh {} \; | sort -h -k5 | tail -n 3 be better? Commented Jun 14, 2018 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. Commented Jun 14, 2018 at 23:24
  • If your find has -print0, it probably has -ls as well. Commented Jun 15, 2018 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... Commented Jun 15, 2018 at 7:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.