How can I find the three largest files under a directory? [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
Sorting files according to size recursively
9 answers
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?
files stat
marked as duplicate by Jeff Schaller, Romeo Ninov, G-Man, schily, sebasth Jun 16 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.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
Sorting files according to size recursively
9 answers
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?
files stat
marked as duplicate by Jeff Schaller, Romeo Ninov, G-Man, schily, sebasth Jun 16 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.
using zsh
â Jeff Schaller
Jun 15 at 0:21
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Sorting files according to size recursively
9 answers
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?
files stat
This question already has an answer here:
Sorting files according to size recursively
9 answers
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?
This question already has an answer here:
Sorting files according to size recursively
9 answers
files stat
asked Jun 14 at 22:23
Tim
22.5k61222401
22.5k61222401
marked as duplicate by Jeff Schaller, Romeo Ninov, G-Man, schily, sebasth Jun 16 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.
marked as duplicate by Jeff Schaller, Romeo Ninov, G-Man, schily, sebasth Jun 16 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.
using zsh
â Jeff Schaller
Jun 15 at 0:21
add a comment |Â
using zsh
â Jeff Schaller
Jun 15 at 0:21
using zsh
â Jeff Schaller
Jun 15 at 0:21
using zsh
â Jeff Schaller
Jun 15 at 0:21
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
%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. Wouldfind . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3be better?
â Tim
Jun 14 at 22:46
Maybe - I generally prefer xargs because I find thefind -execsyntax yucky. I expanded the answer to include a solution that uses xargs.
â NickD
Jun 14 at 23:24
If yourfindhas-print0, it probably has-lsas well.
â muru
Jun 15 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 at 7:12
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
%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. Wouldfind . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3be better?
â Tim
Jun 14 at 22:46
Maybe - I generally prefer xargs because I find thefind -execsyntax yucky. I expanded the answer to include a solution that uses xargs.
â NickD
Jun 14 at 23:24
If yourfindhas-print0, it probably has-lsas well.
â muru
Jun 15 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 at 7:12
add a comment |Â
up vote
1
down vote
accepted
%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. Wouldfind . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3be better?
â Tim
Jun 14 at 22:46
Maybe - I generally prefer xargs because I find thefind -execsyntax yucky. I expanded the answer to include a solution that uses xargs.
â NickD
Jun 14 at 23:24
If yourfindhas-print0, it probably has-lsas well.
â muru
Jun 15 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 at 7:12
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
%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.
%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.
edited Jun 14 at 23:22
answered Jun 14 at 22:34
NickD
1,5471312
1,5471312
Thanks. My filenames contain whitespaces. Wouldfind . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3be better?
â Tim
Jun 14 at 22:46
Maybe - I generally prefer xargs because I find thefind -execsyntax yucky. I expanded the answer to include a solution that uses xargs.
â NickD
Jun 14 at 23:24
If yourfindhas-print0, it probably has-lsas well.
â muru
Jun 15 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 at 7:12
add a comment |Â
Thanks. My filenames contain whitespaces. Wouldfind . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3be better?
â Tim
Jun 14 at 22:46
Maybe - I generally prefer xargs because I find thefind -execsyntax yucky. I expanded the answer to include a solution that uses xargs.
â NickD
Jun 14 at 23:24
If yourfindhas-print0, it probably has-lsas well.
â muru
Jun 15 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 at 7:12
Thanks. My filenames contain whitespaces. Would
find . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3 be better?â Tim
Jun 14 at 22:46
Thanks. My filenames contain whitespaces. Would
find . -type f -exec ls -lh ; | sort -h -k5 | tail -n 3 be better?â Tim
Jun 14 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 at 23:24
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 at 23:24
If your
find has -print0, it probably has -ls as well.â muru
Jun 15 at 2:33
If your
find has -print0, it probably has -ls as well.â muru
Jun 15 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 at 7:12
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 at 7:12
add a comment |Â
using zsh
â Jeff Schaller
Jun 15 at 0:21