Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

List sub-directories recursively and order them by size

Posted on 2011-10-18
6
Medium Priority
?
262 Views
Last Modified: 2012-05-12
Hi All,
I often get alerts from our disk monitoring system for space usage. We have thousands of directories used by number of applications and often finding culprit takes time.
I have used du to locate details but I think there is a better way to do this job.

$ du -smc * | sort -n
1116    agent11g
6464    10.2
7580    total

Above command returns size just 1  level down. I need something to traverse n levels.

Please note this needs to be done recursively & then ordered by size. I need to have last node of directory->sub-directory tree  & not top node

Thanks

0
Comment
Question by:crazywolf2010
6 Comments
 
LVL 6

Expert Comment

by:Jelcin
ID: 36986526
Hi, would this work for you?

du --max-depth=100 -mc * | sort -n

Open in new window

0
 

Author Comment

by:crazywolf2010
ID: 36986727
Actually NO

The above command will return results as below :

239     oracle/product/10.2/crs_1/lib32
247     oracle/product/10.2/db_1/lib32
361     oracle/product/10.2/db_1/lib
394     oracle/product/10.2/db_1/oc4j/j2ee
399     oracle/product/10.2/crs_1/log
399     oracle/product/10.2/crs_1/log/long
408     oracle/product/10.2/db_1/oc4j
428     oracle/product/10.2/crs_1/.patch_storage
428     oracle/product/10.2/crs_1/.patch_storage/6160398
442     oracle/product/10.2/crs_1/evm/log
449     oracle/product/10.2/crs_1/evm
522     oracle/product/10.2/crs_1/install
522     oracle/product/10.2/crs_1/install/prepatch10203
700     oracle/product/10.2/db_1/rdbms/log
771     oracle/product/10.2/db_1/rdbms
2693    oracle/product/10.2/crs_1
3775    oracle/product/10.2/db_1

I don't need listing of all directories and their parnet nodes. I just need the last directory which hosts big files.  For example the results should be like

522     oracle/product/10.2/crs_1/install/prepatch10203
442     oracle/product/10.2/crs_1/evm/log
428     oracle/product/10.2/crs_1/.patch_storage/6160398
408     oracle/product/10.2/db_1/oc4j399
399     oracle/product/10.2/crs_1/log/long
247     oracle/product/10.2/db_1/lib32
239     oracle/product/10.2/crs_1/lib32

Thanks
0
 
LVL 6

Expert Comment

by:mohansahu
ID: 36991940
Hi,
To find largest top 10 files OR directories in Linux.
Use the below command

#du -a /var | sort -n -r | head -n 10
Here a-include all files,
         n-numeric sort & r-recursive

MS
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:crazywolf2010
ID: 37025384
Hi There,
I don't need listing of all directories and their parnet nodes. I just need the last directory which hosts big files.  For example the results should be like

522     oracle/product/10.2/crs_1/install/prepatch10203
442     oracle/product/10.2/crs_1/evm/log
428     oracle/product/10.2/crs_1/.patch_storage/6160398
408     oracle/product/10.2/db_1/oc4j399
399     oracle/product/10.2/crs_1/log/long
247     oracle/product/10.2/db_1/lib32
239     oracle/product/10.2/crs_1/lib32

How can I get these?

Thanks
0
 

Author Comment

by:crazywolf2010
ID: 37069993
Hi Gents,
Any bash guru here who can solve this?
0
 
LVL 7

Accepted Solution

by:
stephenhoekstra earned 2000 total points
ID: 37444153
if you wish to include subdirectories, look at --max-depth=n, like so:

du --max-depth=2 -m /etc | sort -nr | awk '{ if (a[$1]++ == 0) print $0; }' "$@" | head -5

the awk statement will check the first field (size) and remove if duplicated, so it will turn ouput like this:
13      /etc/selinux/targeted
13      /etc/selinux

into this:
13      /etc/selinux/targeted


full test output:
[root@boogiemonster ~]# du --max-depth=2 -m /etc | sort -nr | head -5
27      /etc
13      /etc/selinux/targeted
13      /etc/selinux
4      /etc/brltty
2      /etc/pki/tls

[root@boogiemonster ~]# du --max-depth=2 -m /etc | sort -nr | awk '{ if (a[$1]++ == 0) print $0; }' "$@" | head -5
27      /etc
13      /etc/selinux/targeted
4      /etc/brltty
2      /etc/pki/tls
1      /etc/yum/vars


this can of course go deeper by changing the --max-depth:
[root@boogiemonster ~]# du --max-depth=4 -m /etc | sort -nr | awk '{ if (a[$1]++ == 0) print $0; }' "$@" | head -5
27      /etc
13      /etc/selinux/targeted
9      /etc/selinux/targeted/modules/active
4      /etc/selinux/targeted/policy
2      /etc/pki/tls/certs


hope that helps..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question