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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this 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














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?







share|improve this 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












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?







share|improve this question












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









share|improve this question










share|improve this question




share|improve this question









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
















  • 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










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.






share|improve this answer























  • 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










  • 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


















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.






share|improve this answer























  • 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










  • 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















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.






share|improve this answer























  • 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










  • 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













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.






share|improve this answer















%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.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 14 at 23:22


























answered Jun 14 at 22:34









NickD

1,5471312




1,5471312











  • 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










  • 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

















  • 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










  • 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
















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



Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)