I'd like to sort all the directories/files in a specific directory based on their size (using du -sh "name").
I need to apply this command to all directories in my location, then sort them based on this result. How can I do that ?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI'd like to sort all the directories/files in a specific directory based on their size (using du -sh "name").
I need to apply this command to all directories in my location, then sort them based on this result. How can I do that ?
With GNU sort and GNU du (which it appears you have, since you state you are using du
's -h
option):
du -sh -- * | sort -rh # Files and directories, or
du -sh -- */ | sort -rh # Directories only
The output looks something like this:
22G foo/
21G bar/
5.4G baz/
2.1G qux/
1021M wibble/
4.0K wobble/
du
depends on the order of the directories on the command line. Also, -h
is available on other du
implementations than GNU.
Sort by sizes (as unformatted numbers of kibibytes) and then turn those into human readable:
du -sk -- * | sort -nr | cut -f2 | xargs du -sh
This is an improved version based on jabalv's answer. It works with a GNU as well as a BSD userland.
IFS='\n' du -sk -- * | sort -n | cut -f2 |
while read line ; do
xargs du -sh "$line"
done
sample output:
4.0K games
2.7M local
6.7M lib32
19M sbin
152M src
177M include
321M bin
2.2G share
2.9G lib
To reverse the sort order and list the largest files and directories first, use sort -nr
.
IFS='\n'
do anything useful? (3) Does setting IFS
at the point in your answer where you have set it have any effect on the execution?
Apr 20, 2022 at 0:23
This improves on jabalv's answer, fixing the space in names issue:
du -sk -- * | sort -nr | awk -F '\t' '{print "\""$2"\""}' | xargs du -sh
"
). Also, before I fixed it, it could have failed for filenames beginning with -
(hyphen/dash). … … … … … … … … … … … … … P.S. Please don’t refer to “the above answer”, as different people see the answers in different orders.
Apr 18, 2022 at 18:23
As inspired by jabalv’s answer,
sort by sizes (as unformatted numbers of kibibytes)
and then turn those into human readable numbers
— without running du
a second time:
du -s -- * | sort -n | numfmt --from-unit=1024 --to=iec
or
du -s --block-size=1 -- * | sort -n | numfmt --to=iec
-k
option to the first command line (i.e., du -sk
)
to specify that output should be in kibibytes,
but this seems to be the default.*/
instead of *
to list directories only,
as in Chris Down’s answer.-r
option to the sort command (i.e., sort -nr
)
if you want to sort from high to low.--from-unit=1024
because du
uses binary prefix notation
(i.e., K=1024, M=220, etc.) by default.--to=iec
to output binary-prefix numbers.du
’s output with spaces.
Add --padding=-7
to the numfmt
command
to get the output to look like it has tabs.
(I guess it is counting zero-based.) --padding=7
(without the -
) would right-justify the numbers.)This command should work:
sudo du -sh $(sudo ls) | sort -h
sudo
and why you're using ls
? Note that this would break for anything containing spaces.
sudo
. I wrote that because sometimes I was denied access to run this command. I use ls to list all the directories and use them as input to the du
command. But yes, you are absolutely correct, this would definitely break for directories with whitespaces.