Complex Query regarding folder size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complex Query regarding folder size
# 1  
Old 07-05-2014
Complex Query regarding folder size

Hi,

I provide the path to a folder.

I would like so drill in each folder inside the folder i gave and show me the size in human readable form du -sh <folder-name> of the top 8 folders with maximum sizes.

For example:

Path of the folder: "/opt/app/myapp" has these directories.
Code:
ls
logs
xmls
data
cache
tmp
config

My desired output:
Code:
2.6 GB logs/server3/jvmout
2.1 GB logs/jvm-tmp
1.8 GB xmls/server2/requestxml
1.6 GB xmls/server2/responsexml
1.3 GB xmls/server1/responsexml
998 MB data
49 MB cache/wl_ucp/tmp_xlr/dumps
41 MB logs/app_logs

Note: Its should not display "logs" folder in the desired output, but those sub-folders which have files that sum-up to be top 8 sizes.

My OS: SunOS sparc
Shell: C shell, Bash
# 2  
Old 07-05-2014
Human readable format is not great for sorting. Try something like this:
Code:
find /opt/app/myapp -type d -exec du -ksx {} + | sort -rn | head -n 8


Last edited by Scrutinizer; 07-06-2014 at 03:39 AM..
# 3  
Old 07-06-2014
I think Scrutinizer missed the note:
Quote:
Note: Its should not display "logs" folder in the desired output, but those sub-folders which have files that sum-up to be top 8 sizes.
If I understand what you mean by this correctly, as long as you can live without human readable output, you might try something like:
Code:
#!/bin/ksh
du -k /opt/app/myapp | sort -k1,1rn -k2,2 | awk '
# de(d): Delete Element d from arrays size[] and dir[].
function de(d,		i) {
	for(i = d; i < n; i++)  {
		size[i] = size[i + 1];
		dir[i] = dir[i + 1]
	}
	n--
}
{	size[++n] = $1
	dir[n] = $2
}
{	for(i = 1; i < n; i++)
		for(j = i + 1; j <= n; j++)
			if(index(dir[j], dir[i]) == 1)
				if(substr(dir[j], length(dir[i]) + 1, 1) == "/")
					de(i)
	if(n == 8) exit
}
END {	for(i = 1; i <= n; i++)
		printf("%-*s%s\n", length(size[1]) + 2, size[i], dir[i])
}'

This was written and tested using the Korn shell, but will also work with bash.

If du -h output is crucial, change the second line of the script to:
Code:
du -k /opt/app/myapp | sort -k1,1rn -k2,2 | awk -v sq="'" '

and the last three lines of the script to:
Code:
END {	for(i = 1; i <= n; i++)
		list = list " " sq dir[i] sq
	printf("du -sh %s\n", list)
}' | ksh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Complex string search query.

Hi, I wish to find the latest occurance of the below string in my log file. Once found, I need to search the below string after the above string and display this Request 331489 has passed or Request 331489 has failed I would like my query to be platform... (11 Replies)
Discussion started by: mohtashims
11 Replies

2. Shell Programming and Scripting

Getting folder more than 100K size

Hi , I am trying to get the folder details having size more than sme specified value and also the name of the folder should be like TEST. so 1. In the current directory search for all the folders having name like TEST 2. Print the list of the folder names having size more than 100... (3 Replies)
Discussion started by: Anupam_Halder
3 Replies

3. Shell Programming and Scripting

Complex query

A file whose location i am not aware of contains the below text <url>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(FAILOVER=on)(ADDRESS=(PROTOCOL=TCP) (HOST=ngm2sn1p2-vip.mybank.net)(PORT=4001))(ADDRESS=(PROTOCOL=TCP)(HOST=ngm2sn2p2-vip.mybank.net)... (1 Reply)
Discussion started by: mohtashims
1 Replies

4. Solaris

Need to know the users folder size

Hi Forum, I need to know the size of some user home folders. I've exported an account list from an Active Directory and wrote it into a file like: user_1 user_2 user... user_n and tried "du -sh < filename", which doesn't work. Get no results. I don't need the size of all home... (3 Replies)
Discussion started by: borsti007
3 Replies

5. Web Development

Complex MySQL Query(s)

Hi all, I have a bit of an issue, I am working on a bit of a CMDB for a friend, it's to do with real estate. Anyway, this is my situation. I have a table which contains all the properties (forsale, sold, etc) in the DB named "property". Now, this is what I want to achieve, I wish to... (5 Replies)
Discussion started by: STOIE
5 Replies

6. UNIX for Dummies Questions & Answers

I am trying to get the total size of a folder?

I am trying to get the total size of the folder using the below command but its not working. any ideas? du -bc <foldername>/|grep total|tr -s " "|cut -d" " -f1 the output i am getting is 78996 total but i just want it to be as 78996 please help (3 Replies)
Discussion started by: classic
3 Replies

7. UNIX for Dummies Questions & Answers

size of a folder ?

hi, is possible to calculate the size of a folder using ls ? ls -s works only for files.. thanks (2 Replies)
Discussion started by: aneuryzma
2 Replies

8. UNIX for Dummies Questions & Answers

Need help in finding Folder Size

Hi, I would like to find the size of a folder. When I run the command du -k It is going through all the sub-folder and files and taking really much time. Is there any command to get the complete directory size without showing the sub-folder and file size. Appreciate your response. ... (3 Replies)
Discussion started by: TonySolarisAdmi
3 Replies

9. Programming

Need help with complex SQL query (Sybase)

Hello, I have three tables. I need an SQL query (preferably Sybase) that will return all of the stringID values of table B where the following conditions exist: (1) B.intID = A.intID (2) B.intID != C.intID or (B.intID = C.intID and (C.v1 = 0 or C.v2... (2 Replies)
Discussion started by: chatieremerrill
2 Replies

10. UNIX for Dummies Questions & Answers

Limit Folder Size

Is there a way to limit a certain folder size(e.g. Documents, Desktop)? :) (2 Replies)
Discussion started by: tisdmin
2 Replies
Login or Register to Ask a Question