If you are using Linux, you will probably encounter a situation where your hard drive is taking up space, but you do not know which files or folders are heaviest to find and delete. On Linux, there are commands available to help you find the largest File/Folder. With a combination of 3 commands du
, sort
and head
will help you quickly list out the data that is taking up hard drive space on your Linux or VPS.
Join the channel Telegram belong to AnonyViet ???? Link ???? |
- Command
du
: determine the amount of usage - Command
sort
: sort the lines of the text file or input data - Command
head
: output data from the top, for example take the first 10 lines for example
Check the size of the folder /home/
If you want to check the size of the root directory, change it to /root/
du -sh /home/
Use the command below to find the 10 largest files/folders. Replace /home
by the path you want
du -a /home/ | sort -n -r | head -n 10
Output example:
589700 /home 589696 /home/anonyviet.com 526988 /home/anonyviet.com/public_html 506628 /home/anonyviet.com/public_html/wp-content 385096 /home/anonyviet.com/public_html/wp-content/uploads 179988 /home/anonyviet.com/public_html/wp-content/uploads/2014 117072 /home/anonyviet.com/public_html/wp-content/uploads/2013 74212 /home/anonyviet.com/public_html/wp-content/plugins 47296 /home/anonyviet.com/public_html/wp-content/uploads/2012 43100 /home/anonyviet.com/public_html/wp-content/cache
If you want to change the capacity bytes wall MB, KB billionthen use the following command:
cd /home du -hsx * | sort -rh | head -10
The above commands can only be used when Linux has the package installed sort
otherwise you can use the command below:
for i in G M K; do du -ah | grep [0-9]$i | sort -nr -k 1; done | head -n 11
Output example:
179M . 84M ./uploads 57M ./images 51M ./images/a 49M ./images/abc/2013 47M ./uploads/def 37M ./videos/gha/2013/12 37M ./videos/gha/2013 37M ./videos/gha 36M ./videos 35M ./uploads/gha
Command to find the largest files in a folder:
Find the largest file in Folder /home
find /home -printf '%s %p\n'| sort -nr | head -10
Find the largest file in the current folder
find . -printf '%s %p\n'| sort -nr | head -10
Output example:
5700875 ./images/faq/2023/11/iftop-outputs.gif 5459671 ./videos/faq/2023/12/glances/glances.webm 5091119 ./videos/faq/2023/12/glances/glances.ogv 4706278 ./images/faq/2023/09/cyberciti.biz.linux.wallpapers_r0x1.tar.gz 3911341 ./videos/faq/2023/12/vim-exit/vim-exit.ogv 3640181 ./videos/faq/2023/12/python-subprocess/python-subprocess.webm 3571712 ./images/faq/2023/12/glances-demo-large.gif 3222684 ./videos/faq/2023/12/vim-exit/vim-exit.mp4 3198164 ./videos/faq/2023/12/python-subprocess/python-subprocess.ogv 3056537 ./images/faq/2023/08/debian-as-parent-distribution.png.bak
To display only files, not folders, use the command:
find /path -type f -printf '%s %p\n'| sort -nr | head -10
or
find /path -type f -iname "*.mp4" -printf '%s %p\n'| sort -nr | head -10
- Find the largest directory in Linux
- Find the heaviest directory in Linux
- Find the largest folder in Linux
- Find the heaviest folder in Linux
- Find the largest file in Linux
- Find the heaviest file in Linux
Reference: hocvps.com