How to list the size of each file and directory and sort by descending size in Bash?
Solution 1
Simply navigate to directory and run following command:
du -a --max-depth=1 | sort -n
OR add -h for human readable sizes and -r to print bigger directories/files first.
du -a -h --max-depth=1 | sort -hr
Solution 2
Apparently --max-depth
option is not in Mac OS X's version of the du
command. You can use the following instead.
du -h -d 1 | sort -n
Solution 3
du -s -- * | sort -n
(this willnot show hidden (.dotfiles) files)
Use du -sm
for Mb units etc. I always use
du -smc -- * | sort -n
because the total line (-c
) will end up at the bottom for obvious reasons :)
PS:
- See comments for handling dotfiles
- I frequently use e.g. 'du -smc /home// | sort -n |tail' to get a feel of where exactly the large bits are sitting
Solution 4
Command
du -h --max-depth=0 * | sort -hr
Output
3,5M asdf.6000.gz
3,4M asdf.4000.gz
3,2M asdf.2000.gz
2,5M xyz.PT.gz
136K xyz.6000.gz
116K xyz.6000p.gz
88K test.4000.gz
76K test.4000p.gz
44K test.2000.gz
8,0K desc.common.tcl
8,0K wer.2000p.gz
8,0K wer.2000.gz
4,0K ttree.3
Explanation
du
displays "disk usage"h
is for "human readable" (both, in sort and in du)max-depth=0
meansdu
will not show sizes of subfolders (remove that if you want to show all sizes of every file in every sub-, subsub-, ..., folder)r
is for "reverse" (biggest file first)
ncdu
When I came to this question, I wanted to clean up my file system. The command line tool ncdu
is way better suited to this task.
Installation on Ubuntu:
$ sudo apt-get install ncdu
Usage:
Just type ncdu [path]
in the command line. After a few seconds for analyzing the path, you will see something like this:
$ ncdu 1.11 ~ Use the arrow keys to navigate, press ? for help
--- / ---------------------------------------------------------
. 96,1 GiB [##########] /home
. 17,7 GiB [# ] /usr
. 4,5 GiB [ ] /var
1,1 GiB [ ] /lib
732,1 MiB [ ] /opt
. 275,6 MiB [ ] /boot
198,0 MiB [ ] /storage
. 153,5 MiB [ ] /run
. 16,6 MiB [ ] /etc
13,5 MiB [ ] /bin
11,3 MiB [ ] /sbin
. 8,8 MiB [ ] /tmp
. 2,2 MiB [ ] /dev
! 16,0 KiB [ ] /lost+found
8,0 KiB [ ] /media
8,0 KiB [ ] /snap
4,0 KiB [ ] /lib64
e 4,0 KiB [ ] /srv
! 4,0 KiB [ ] /root
e 4,0 KiB [ ] /mnt
e 4,0 KiB [ ] /cdrom
. 0,0 B [ ] /proc
. 0,0 B [ ] /sys
@ 0,0 B [ ] initrd.img.old
@ 0,0 B [ ] initrd.img
@ 0,0 B [ ] vmlinuz.old
@ 0,0 B [ ] vmlinuz
Delete the currently highlighted element with d, exit with CTRL + c
Solution 5
ls -S
sorts by size. Then, to show the size too, ls -lS
gives a long (-l
), sorted by size (-S
) display. I usually add -h
too, to make things easier to read, so, ls -lhS
.
Comments
-
TheOneTeam over 3 years
I found that there is no easy to get way the size of a directory in Bash?
I want that when I type
ls -<some options>
, it can list of all the sum of the file size of directory recursively and files at the same time and sort by size order.Is that possible?
-
Keith Thompson almost 13 yearsWhat exactly do you mean by the "size" of a directory? The number of files under it (recursively or not)? The sum of the sizes of the files under it (recursively or not)? The disk size of the directory itself? (A directory is implemented as a special file containing file names and other information.)
-
TheOneTeam almost 13 yearsshould be The sum of the sizes of the files under it recursively
-
Keith Thompson almost 13 years@Kit: Then
du
is the answer. -
ztank1013 almost 13 years@KeithThompson @KitHo
du
command estimates file space usage so you cannot use it if you want to get the exact size. -
Keith Thompson almost 13 years@ztank1013: Depending on what you mean by "the exact size",
du
(at least the GNU coreutils version) probably has an option to provide the information. -
ztank1013 almost 13 years@KeithThompson By "the exact size" I mean the size in bytes of a file (or the sum of file sizes if we are talking about more files), which is probably the size @KitHo is talking about, isn't it? As an example try to
ls -l filename
and compare it withdu filename
. And please if such a option fordu
exists, I'd be more than curios to know it. -
TheOneTeam almost 13 yearsYes. The sum of the file sizes
-
ztank1013 almost 13 years@KitHo What about my answer below? does it respond to your needs?
-
jww almost 6 yearsPossible duplicate of Using ls to list directories and their total sizes
-
-
Thanatos almost 13 yearsAh, sorry, that was not clear from your post. You want
du
, seems someone has posted it. @sehe: Depends on your definition of real — it is showing the amount of space the directory is using to store itself. (It's just not also adding in the size of the subentries.) It's not a random number, and it's not always 4KiB. -
Arnaud Le Blanc almost 13 years
du --max-depth=1|sort -n
orfind . -mindepth 1 -maxdepth 1|xargs du -s|sort -n
for including dotfiles too. -
sehe almost 13 years@arnoud: I use that too, but it didn't seem the right addition for this question (/answer) :)
-
Ego almost 13 yearsNevermind, sehe has presented a much simpler solution. I learn something new every day!
-
ztank1013 almost 13 yearsI don't think using
du
is an option, It will give you just an approximate result. -
TheOneTeam almost 13 yearsif the diretory is too large, it hangs up for a long time, try to work on your home directory :p
-
ztank1013 almost 13 years@KitHo well, I am afraid there is no easy and fast way to get a precise result without searching every single file and adding its size, hence command laziness mainly depends on how many files are underneath the searched directory... But I believe there is margin for improvements... nice challenge!
-
ztank1013 almost 13 years@KitHo hey there, take a look at the enhanced version in my answer... and let me know of course!
-
Lri over 12 years@arnaud576875
find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 du -s | sort -n
if some of the found paths could contain spaces. -
Smylers about 11 years
du -h
requiressort -h
too, to ensure that, say981M
sorts before1.3G
; withsort -n
only the numbers would be taken into account and they'd be the wrong way round. -
rasmusx over 10 yearsYou could also write du -hs * | sort -hr. -s (summarize) is same as --max-depth=0
-
marsbard over 10 yearsThis is a great variant to get a human readable view of the biggest:
sudo du -smch * | sort -h | tail
-
Erik Trautman over 10 yearsThis doesn't list the size of the individual files within the current directory, only the size of its subdirectories and the total size of the current directory. How would you include individual files in the output as well (to answer OP's question)?
-
Franco about 10 years@ErikTrautman to list the files also you need to add
-a
and use--all
instead of--max-depth=1
like sodu -a -h --all | sort -h
-
Colby Blair over 9 yearsAwesome! I've been doing something lamer for a few years now. :)
-
djule5 almost 9 years
sort -h
only works on GNU's version / Linux, no luck with BSD / OS X. -
Benjamin Engwall almost 6 yearsThis kind of works, but ignores the units on the size of the file when sorting.
-
vhs over 4 yearsApparently, but not surprisingly.
-
Ben Butterworth over 3 yearsUnfortunately this does not show the files, but only the folder sizes.
-a
does not work with-d
either. -
Ben Butterworth over 3 yearsTo show files and folders, I combined 2 commands:
l -hp | grep -v / && du -h -d 1
, which shows the normal file size fromls
for files, but usesdu
for directories.