LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 01-25-2017, 06:51 PM   #1
brianmagento
LQ Newbie
 
Registered: Jan 2017
Posts: 1

Rep: Reputation: Disabled
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

Last edited by brianmagento; 02-08-2017 at 02:50 AM. Reason: solution worked, thanks tshikose
 
Old 01-25-2017, 07:37 PM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Hi,

Try
du /folder | sort -nr | head
 
Old 01-25-2017, 10:50 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,318
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
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
 
Old 01-26-2017, 02:49 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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
 
1 members found this post helpful.
Old 01-30-2017, 01:07 PM   #5
ccj4467
Member
 
Registered: Jan 2009
Posts: 34

Rep: Reputation: 5
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script which will check the target file and folder exists and copy it agent_mach Linux - Newbie 5 11-18-2014 01:33 AM
Shell script which will check the target file and folder exists and copy it agent_mach Programming 2 11-15-2014 05:36 AM
how to check folder if it has files in it using Bash Shell script vincent.dang Linux - Newbie 7 08-05-2010 07:58 PM
shell script - how to check file size noir911 Programming 3 01-24-2009 09:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 10:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration