du in Linux view the folder size and sort by size

  • 2020-05-27 07:58:23
  • OfStack

du in Linux - view the folder size and sort by size

One day, I want to check the use of 1 computer hard disk, as a command control, nonsense, let's start:

Use the df command to view current disk usage:


jack@jiaobuchong:~$ df -lh 
Filesystem   Size Used Avail Use% Mounted on 
/dev/sda3    18G 5.7G  11G 35% / 
udev      2.7G 4.0K 2.7G  1% /dev 
tmpfs      553M 916K 552M  1% /run 
none      5.0M   0 5.0M  0% /run/lock 
none      2.7G 488K 2.7G  1% /run/shm 
/dev/sda2    946M 128M 754M 15% /boot 
/dev/sda1    93G  87G 5.5G 95% /media/2AA64C7FA64C4D8F_

Here comes the du command:


jack@jiaobuchong:~$ pwd 
/home/jack 
jack@jiaobuchong:~$ du -sh 
1.9G  . 
jack@jiaobuchong:~$ cd .. 
jack@jiaobuchong:/home$ du -sh jack/ 
1.9G  jack/ 
jack@jiaobuchong:/home$ du -h --max-depth=0 jack/ 
1.9G  jack/ 

You can see the same result up here,

-s, --summarize display only a total each each argument, -s this parameter only shows the total, that is, the size of the current folder.


jack@jiaobuchong:~$ du -sh * 
170M  Desktop 
452K  Documents 
161M  Downloads 
12K examples.desktop 
833M  installed-software 
284K  learngit 
4.0K  Music 
4.7M  Pictures 
3.2M  program_pratice 
4.0K  Public 
112K  session 
4.0K  Templates 
4.0K  Videos 

* you can list the size of all files in the current directory. What about sorting the listed files from large to small?


jack@jiaobuchong:~$ du -sh * | sort -nr 
833M  installed-software 
452K  Documents 
284K  learngit 
170M  Desktop 
161M  Downloads 
112K  session 
12K examples.desktop 
4.7M  Pictures 
4.0K  Videos 
4.0K  Templates 
4.0K  Public 
4.0K  Music 
3.2M  program_pratice

Just ask sort for a favor. Ha ha! This is not a normal sort, it's all because of the -h parameter,


jack@jiaobuchong:~$ du -s * | sort -nr 
852756 installed-software 
173868 Desktop 
164768 Downloads 
4724  Pictures 
3236  program_pratice 
452 Documents 
284 learngit 
112 session 
12 examples.desktop 
4  Videos 
4  Templates 
4  Public 
4  Music 

So now we have a normal sort.

du-s * | sort-nr | head pick the top 10,

du-s * | sort-nr | tail pick the next 10.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: