LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-24-2013, 11:26 AM   #1
tjerich
LQ Newbie
 
Registered: Sep 2013
Distribution: SL6
Posts: 1

Rep: Reputation: Disabled
BASH : Sum size of same name directories


EDITED: Added more information

First of all, I am a bash noob, so please be gentle

I am trying to sum the size of folders that are in different places but have the same name. It looks like this :

root
--- directory 1

------ folder 1
--------subfolder 1
--------subfolder 2
------ folder 2
--------subfolder 3
--------subfolder 4
------ folder 3
--------subfolder 5
--------subfolder 6

--- directory 2

------ folder 1
--------subfolder 1
--------subfolder 2
------ folder 2
--------subfolder 3
--------subfolder 4
------ folder 3
--------subfolder 5
--------subfolder 6

I am trying to sum the size of subdirectories 1 to 6 and output that to a .csv

At the moment I am simply outputting sizes of subdirectories in two seperate CSV files. One for directory 1 and one for directory 2

At the moment I have this to output sizes of subfodlers that I run where I need them :

du -h --max-depth=1 --block-size=GB * | grep "[\/]" | sort -n -r > ~/lists/disks/RC_job.csv

The output look like this :

40GB folder1/subfolder1

15GB folder1/subfolder2

10GB folder2/subfolder 3
...

I have one output for directory 1 and one for directory 2. I would like to sum the size of subfolders from directory one and two and have an output that looks like this

60GB subfolder1

25GB subfolder2

10GB subfolder3

Where subfolder1 is directory1/folder1/subfolder1 + directory2/folder1/subfolder1

This is my first post here I do not know if this enough info. I would be pleased to provide more if necessary. I am pretty sure this can be done with awk, but I haven't really used that yet.

Cheers !

Last edited by tjerich; 09-24-2013 at 11:59 AM.
 
Old 09-24-2013, 12:27 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Something like?:
Code:
for DIRECTORYNAME in name0 name1 name2; do
 RETVAL_BT=$(echo "scale=0;$(find ${DIRECTORYNAME} -type f -printf "%s+"|sed -e "s|\+$||g")"|bc -l)
 RETVAL_GB=$(echo "scale=2;$RETVAL_BT/1024/1024/1024"|bc -l)
 echo "${RETVAL_GB:=ERROR}";${DIRECTORYNAME};
done
 
Old 09-24-2013, 12:38 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i wouldnt use spaces in filenames...
quick-and-dirty:
Code:
[schneidz@hyper ~]$ awk '{print $2}' dir1.lst dir2.lst | while read dir
do
 size=`cat dir1.lst dir2.lst | grep -w $dir | awk '{size = size + $1} END {print size}'`
 echo $size"gb" $dir
done
 
Old 09-24-2013, 06:16 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Untested, but how about something like:
Code:
find dir1 dir2 -type d -maxdepth 2 -mindepth 2 -exec du -sBG {} \; | awk -F"[ /]*" '{_[$NF]+=gensub(/G/,"","g",$1)}END{for(i in _)print i,_[i]}'
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Output of ls -lh (size of directories) gacanepa Linux - Newbie 4 06-24-2013 11:20 PM
bash script to add integer values & get the sum demet8 Linux - Newbie 5 10-20-2012 01:46 PM
Getting SUM in Bash thewebbie Programming 1 05-20-2009 11:42 AM
script to sum all partition size bostonthunder777 Linux - Server 4 12-26-2007 03:26 PM
directories size is always 4K Milosevic Linux - Newbie 7 12-10-2005 05:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:32 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