Script to sort by size of directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to sort by size of directories
# 1  
Old 11-19-2013
Script to sort by size of directories

Hello,

I'm trying to find top 5(highest) directories by size. I did something like
Code:
du -sh * > $file

Where I can get all the size with respect to directories, but I need only top 5 directory from the file. How can I sort by size in the file and print top 5 sizes with the directory name???

Please suggest me to move further.

Output of the "file" is like below

Code:
4.0K    a
4.3M    dl
131G    he
8.0K    an
8.5G    ss
208K    be
4.0K    ng
5.1G    un
4.0K    e
65G     vm
6.9G    dch
4.0K   au
196K    iq
4.0K    jc
4.0K    es
27G     wr
4.0K   rs
5.7M    jk
52G     da
4.0K    jvk
4.8M    god
4.5G    is
12K    vs

# 2  
Old 11-19-2013
Try:
Code:
du -sk * | sort -nrk1 | head -5

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-20-2013
Hi.

See https://www.unix.com/shell-programming-and-scripting/182403-how-sort-numbers-symbol-like-alphabets-5m-52g.html for an alternate suggestion, namely a new option for "human-readable" data with recent versions of GNU sort.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 11-20-2013
Thanks alot guys, that helped a lot.

I appreciate your help.

---------- Post updated at 06:02 PM ---------- Previous update was at 10:26 AM ----------

I got this trouble with some other basic issue. But my poor scripting skills don't know how to fix this.

Code:
du -sk * | sort -nrk1 | head -5 > /kjor/space

awk '{ printf("%.3f GB\n", $1 / (1024*1024)); }' $1 cat /kjor/space

Output as

Code:
13.554 GB 
8.556 GB
20.889 GB
4.013 GB 
3.296 GB

Instead of

Code:
13.554 GB    an
8.556 GB    ss
20.889 GB    be
4.013 GB    ng
3.296 GB    gt

Can you suggest me how to print output without changing the actually alphabets ??
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort directories in 'D' on FTP server

Hello, There are 3 directories on FTP server with format yyyymmddhhmiss. Can someone please let tell me how do I sort these directories in descending order. I only have get the files from the latest directory on FTP server. ftp>ls -l 20150707011229 20150707061229 20150707131225 ... (2 Replies)
Discussion started by: pavan_test
2 Replies

2. Shell Programming and Scripting

Directories above particular size

Hi guys, i'm searching for a command, which gives me back all the directories which are greater than a particular size? thx for helping so far :b: guti (2 Replies)
Discussion started by: guti_rocks
2 Replies

3. UNIX for Dummies Questions & Answers

Finding size of all directories

Alright so I've tried a couple different things that at first glance, looked like they worked. find . -maxdepth 5 -type d -daystart -mtime 1 | xargs du -h Which seems to ignore the previous commands such as depth and modified time. find .. -maxdepth 2 -type d -daystart -ctime 1 | xargs... (8 Replies)
Discussion started by: Aussiemick
8 Replies

4. Shell Programming and Scripting

sort by size

Can some one help in sorting the attached file. I used cmd: sort -r jar1.txt -o sortedjar.txt , but it didnt work. Thanks for your help in Advance. (6 Replies)
Discussion started by: sawyer
6 Replies

5. Shell Programming and Scripting

How to list all the directories, sub directories in a mount along with size in ascending order?

Hi , I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In a particular mount, have to list all the directories and sub directories along with size of the directory and sub directory in ascending order. Please help me in this regard and many... (4 Replies)
Discussion started by: nmakkena
4 Replies

6. Shell Programming and Scripting

to find the size of directories

Hi, how to know the size of the files in the directory, exclusing the subfolder. ? i tried du -h du -sk du -k but its not giving.. (2 Replies)
Discussion started by: mail2sant
2 Replies

7. Shell Programming and Scripting

Command to sort directories after a recursive find

find -type d -name "TC_*" | sort That's what I have so far... it finds the appropriate directories and then sorts them. But, when it comes to nested subdirectories, it only sorts relative to the first subdirectory. I want it to sort based on the directory at the end of the path. Does anyone know... (3 Replies)
Discussion started by: crimsondarkn
3 Replies

8. UNIX for Dummies Questions & Answers

sort by size in Mb and Kb

Hi I am using the command below to list the 10 biggest directories and files in my present directory du -hs * | sort +0 | tail -10 the output is 8K disk-space 16K rish 32K WINDOWS 48K tests 104K imgvdEwLa.jpg 168K 020204_aerosmith_1024768.jdk 3.2M Acdc -... (4 Replies)
Discussion started by: the.noob
4 Replies

9. UNIX for Dummies Questions & Answers

Empty directories having different size

$ls -lrt mydir total 12 drwxrwxrwx 2 nobody nobody 512 Aug 8 11:51 tmp drwxrwxrwx 2 nobody nobody 4608 Jan 19 12:20 web.cache $ ls -lrt mydir/web.cache/ total 0 $ ls -lrt mydir/tmp/ total 0 Can anyone explain me the above results? I know the o/p of ls, but this... (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. HP-UX

Directories Size

Hi All I want to plan my backups, but 1st i would like to know what is the size of each directory. Is there any command which can show me the size of directories? Regards (2 Replies)
Discussion started by: cgege
2 Replies
Login or Register to Ask a Question