LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   CentOS (https://www.linuxquestions.org/questions/centos-111/)
-   -   Shell script to check and sort linux folder size (https://www.linuxquestions.org/questions/centos-111/shell-script-to-check-and-sort-linux-folder-size-4175598262/)

brianmagento 01-25-2017 06:51 PM

Shell script to check and sort linux folder size
 
Hello everyone,
I'm trying to figure out what directories occupy the most disk space on my Linux vps, is there a command that can check and sort linux folder size?

Thanks for any help

It worked: du -sh /folder/* | sort -rh | head -n 5

Thanks
Brian from magento explorer

tshikose 01-25-2017 07:37 PM

Hi,

Try
du /folder | sort -nr | head

Turbocapitalist 01-25-2017 10:50 PM

As suggested, you can pipe several together to produce the result you want. Here is another variation:

Code:

du -sh /folder/* | sort -rh | head -n 5
That will search the directory /folder/ and summarize how much each of its subdirectories use.
Then it sorts in descending order, taking into account units like K, M, and G
Then it shows just the top five offenders.

See the manual pages for each of them for details.

Code:

man du
man sort
man head


ondoho 01-26-2017 02:49 PM

i inherited this script from somewhere:
Code:

#!/bin/sh
du -ak -x "$@" | sort -n | tail -21 | \
awk -F '\t' -v OFS='\t' '{if ($1 > 1048576) $1 = sprintf("%.1fG",$1/1048576); else if ($1 > 1024) $1 = sprintf("%.1fM",$1/1024); else $1 = sprintf("%sK",$1)} 1'\
| sed 's/\t\.\//\t/g' | head -20


ccj4467 01-30-2017 01:07 PM

This is one I inherited

Code:

du -sh * | tee /tmp/prova.txt | grep T | sort -rn ; grep G /tmp/prova.txt | sort -rn ; grep M /tmp/prova.txt | sort -rn ; grep K /tmp/prova.txt | sort -rn ; rm /tmp/prova.txt
Ouput is human readable form.


All times are GMT -5. The time now is 03:15 AM.