calculate the space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculate the space
# 1  
Old 02-09-2008
calculate the space

Hi everyone,

I need to write a script to calculate the space for sub-folders under /home:

Here is the scanrio:

cd /home
drwxr-xr-x 57 root root 8192 Jan 22 16:13 home_1
drwxrwxrwx 69 root root 8192 Jan 29 10:36 home_2
drwxr-xr-x 97 root root 8192 Nov 21 14:40 home_3
drwxr-xr-x 80 root root 8192 Jan 30 13:38 home_4
dr-xr-xr-x 1 root root 1 Feb 9 13:14 home_5
dr-xr-xr-x 1 root root 1 Feb 9 13:14 home_6
dr-xr-xr-x 1 root root 1 Feb 9 13:14 home_6
dr-xr-xr-x 1 root root 1 Feb 9 13:14 home_7
.
.
.
goes upto

dr-xr-xr-x 1 root root 1 Feb 9 13:14 home_23
I need to cd to each home dir and check for all folders where owner is a number and calculate it's size using "du -sk" command.

# cd home_1

ls -l
drwxr-xr-x 16 16848 eaq 4096 Sep 10 08:22 khanbx0c
drwxr-xr-x 21 17341 eao 12288 Oct 29 2006 labatlx
drwxr-xr-x 6 lasisiog eal 4096 Feb 4 07:25 lasisiog
drwxr-xr-x 14 lecailpx eau 4096 May 2 2007 lecailpx
drwxr-xr-x 14 16460 eao 4096 Oct 21 13:03 littleax
drwxr-xr-x 28 logtrak eaq 8192 Jul 2 2007 logtrak
drwxr-xr-x 2 17480 eap 4096 Feb 17 2007 megrinan
drwxrwxrwx 64 navaraam eaq 12288 Feb 6 15:47 navaraam
drwxr-xr-x 31 17501 eac 8192 Jul 2 2007 olaqiaa
drwxrwxr-x 104 owback easdmm 94208 Feb 6 13:25 owback
drwxr-xr-x 14 16490 ehe 4096 Nov 30 2004 patvalza
drwxr-xr-x 33 sagazt qcd 8192 Jan 30 17:04 sagazt
drwxr-xr-x 9 sanniml ehh 4096 Oct 22 14:27 sanniml
drwxrwxr-x 2 root root 4096 Jul 21 2004 seismic
drwxr-xr-x 9 shehriih eqc 4096 Nov 21 2006 shehriih
drwxr-xr-x 2 ssglusr ssd_ssg 4096 Oct 4 2005 ssglusr
drwxr-xr-x 2 17602 ehh 4096 Jun 3 2007 suicmevs
drwxr-xr-x 6 17436 eap 4096 Apr 7 2007 suwaiiah
drwxr-sr-x 77 wasimx eaf 24576 Feb 6 15:28 wasimx
drwxr-xr-x 12 16504 ehd 4096 Oct 21 13:03 xiaojx
drwxr-xr-x 3 17309 eap 4096 Dec 5 2006 youngsj
drwxr-xr-x 37 17406 qst 12288 Apr 3 2007 zambraga
drwxr-xr-x 47 zarmm eag 12288 Feb 5 10:47 zarmm
drwxrwxr-x 49 16531 eao 16384 Jun 14 2007 zegdra0a

I need to capture home directories where owner is just a number:

example:
drwxr-xr-x 37 17406 qst 12288 Apr 3 2007 zambraga

than run du -sk on those one by one by using echo:

zambraga 131040

Any tips would be appreciated.
# 2  
Old 02-10-2008
Java

Code:
#!/bin/sh
for directory in `ls -ld home_*/* | awk '{ print $3":"$9 }'`
do
    if echo $directory | cut -d ':' -f 1 | grep -iv [a-z] > /dev/null
    then
        du -sk `echo $directory | cut -d ':' -f 2`
    fi
done

Untested
# 3  
Old 02-11-2008
Hi Smiling Dragon,

Thanks for your help .. it works for me !!! Appreciate that.
# 4  
Old 05-03-2008
Hi guys, need a quick help on this ... how can I get the following output into different columns like:

16862 868K /home/home_1/abbama0z
16791 8.9M /home/home_1/ahmaha0d
.... and so on ..

instead of the following:

16862
868K /home/home_1/abbama0z
16791
8.9M /home/home_1/ahmaha0d
16841
32K /home/hom_2/ajajaa0j
16915
89M /home/home_2/aljabeaa
17176
32K /home/home_2/alsaeeaj
17013
32K /home/home_3/alzairam
16646
1.7M /home/home_3/amairam
16847
842M /home/home_3/apontejc
16512
1.4M /home//home_3argaudmj
17005
1.4M /home/home_3/asiriks
16984
32K /home/home_4/bajaas0a

Appreciate your help ....
# 5  
Old 05-03-2008
This should give the desired output:

Code:
awk '{ORS=NR%2?" ";"\n"}1' file

Start a new thread for a new question next time instead of hijack your own thread.

Regards
# 6  
Old 05-03-2008
Thanks for the input, but how can I get this from running the above mentioned script in this thread.

I did not hijack the thread, this question is related to the same thread and the script which generates this output is also there so I wanted to make it easy if someone needs to see the script.

I hope this clarifies your concern.
# 7  
Old 05-03-2008
Pipe the output of your script to the awk command:

Code:
scriptname | awk '{ORS=NR%2?" ";"\n"}1'

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

3. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Fedora

Need to incrwase PHYSICAL VOLUME space on hard drive with free space on it

Hi, I run Fedora 17. I created a physical volume of 30GB on a disk with 60GB of space so there is 30GB of free space. On the physical volume, I created my volume group and logical volumes. I assigned all the space in the physical volume to my volume group. I need to add the 30GB of free space... (1 Reply)
Discussion started by: mojoman
1 Replies

5. Emergency UNIX and Linux Support

Command to calculate space for all subdirs under a dir

du -hs command calculates the space for all the subdirs under a dir ...but it is very slow if the dir is huge....is there any quick way ...I am using Sun OS. Thanks, Ajay (19 Replies)
Discussion started by: ajaypatil_am
19 Replies

6. Linux

How to reclaim the space which i used to increse the swap space on Xen,

Hi, i have done a blunder here, i increased the swap space on Xen5.6 server machine using below steps :- 1056 dd if=/dev/zero of=/root/myswapfile bs=1M count=1024 1057 ls -l /root/myswapfile 1058 chmod 600 /root/myswapfile 1059 mkswap /root/myswapfile 1060 swapon /root/myswapfile ... (1 Reply)
Discussion started by: apm
1 Replies

7. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

8. Shell Programming and Scripting

calculate swap space usage in Solaris and converts it in Percentage

Hi Folks, I am looking for a way to write a script to calculate swap usage in Solaris so that the current usage will be shown in Percentage value. Thanks!! Based on 'swap -s' command # swap -s total: 1378936k bytes allocated + 1236880k reserved = 2615816k used, 2725104k available (2 Replies)
Discussion started by: jaapar
2 Replies

9. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

10. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies
Login or Register to Ask a Question