Sum of file size in directory / subdirectory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of file size in directory / subdirectory
# 1  
Old 09-09-2010
Sum of file size in directory / subdirectory

Hi ,

I am trying to write something to find the size of particular type of files in a directory & it's subdirectory and sum the size .. These types of file are found at directory level or its subdirectories level ..

Code:
#!/bin/ksh
FNAME='.pdf'
S_PATH=/abc/def/xyz
find $S_PATH   -exec ls -lad {} \; 	> temp1
cat temp1 | grep -i $FNAME	             > temp2 
awk 'NR>5{+$5}END{ print "Total " i}' temp2

Seems my awk is not working.. I am doing something stupid with awk.. Any simple way would be appreciated.
Input is
Code:
-rw-r--r--    1 owner abcdef        1031 Sep 07 12:57 /abc/def/xyz/PAY_WFM/unique.pdf
-rw-r--r--    1 owner abcdef        6279 Sep 07 12:57 /abc/def/xyz/SA1/unique.pdf
-rw-r--r--    1 owner abcdef         987 Sep 07 11:59 /abc/def/xyz/T1/unique.pdf
-rw-r--r--    1 owner abcdef        1650 Sep 07 12:57 /abc/def/xyz/A1/unique.pdf
-rw-r--r--    1 owner abcdef       83477 Sep 07 12:57 /abc/def/xyz/DM/unique.pdf
-rw-r--r--    1 owner abcdef        5845 Sep 07 12:57 /abc/def/xyz/TH/unique.pdf
-rw-r--r--    1 owner abcdef        3266 Sep 07 12:57 /abc/def/xyz/MR/unique.pdf
-rw-r--r--    1 owner abcdef       37295 Sep 07 12:57 /abc/def/xyz/PPO/unique.pdf
-rw-r--r--    1 owner abcdef      242529 Sep 07 12:57 /abc/def/xyz/P/unique.pdf
-rw-r--r--    1 owner abcdef       43072 Sep 07 12:57 /abc/def/xyz/P2/unique.pdf
-rw-r--r--    1 owner abcdef        5485 Sep 07 12:57 /abc/def/xyz/P/unique.pdf
-rw-r--r--    1 owner abcdef        4849 Sep 07 12:05 /abc/def/xyz/TP/skvunique.txt
-rw-r--r--    1 owner abcdef       58730 Sep 07 12:57 /abc/def/xyz/M1/unique.pdf

Output wanted is --- > 494495 (which is sum of values in 5th column)


Thanks in advance.
Vaddadi

Last edited by Scott; 09-09-2010 at 02:13 PM.. Reason: Code tags, please...
# 2  
Old 09-09-2010
Hi.

Why did you skip the first 5 rows?

Code:
$ awk '{T+=$5} END {print T}' inputfile
494495

Find also has an ls option, so no need for exec, really.

Your find also doesn't exclude directories (i.e. maybe you want -type f in there).
# 3  
Old 09-09-2010
I was having some header line in the file so I wanted to skip them..

Anyway.. I removed the header and then your instruction worked like champ..

Can we merge find | cat | awk command in single line ..

Thanks .. Vaddadi
# 4  
Old 09-09-2010
Hi.

I think you need at least two command.

You could use something like:

Code:
find $S_PATH -type f -name "*.pdf" -ls 2> /dev/null | awk '{T+=$7} END {print T}'

(the size is $7 for my find -ls)
# 5  
Old 09-09-2010
Thanks it worked like a champ ..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching File in Directory and all Subdirectory and Delete

Hi All, My directory structure is like Directory1 SubDirectory1 SubDirectory2 SubDirectory3 I have main directories and subdirectories underneath. I want to write a shell script where I will be passing file name as a parameter, Now I want to find all the files in Directory1... (19 Replies)
Discussion started by: John William
19 Replies

2. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

3. Shell Programming and Scripting

Move all files not in a directory into a subdirectory named for each given file

Hi Everyone! Looking for some help with a script that will take all files in any given root folder (which are not already in a folder) and put them into separate folders with the name of each given file. Any ideas? Thank you! (1 Reply)
Discussion started by: DanTheMan
1 Replies

4. UNIX for Dummies Questions & Answers

How to get the set of files size as a sum in a directory.

Hi, Can someone help me to get the complete files size (sum) over a perod time (1 day,2days)in a directory. eg: Directory :ABC I have a1,a2,a3 files are created in last 24 hours so I need to get the some of all these files. I am using the below find command but its giving me the... (1 Reply)
Discussion started by: gaddamja
1 Replies

5. Shell Programming and Scripting

How can i find a word inside a file from a directory and it's subdirectory ?

Suppose i have a word "mail". I have to search this word in all files inside a directory and it's sub-directories. It will also search in all hidden directory and sub-directories. If it finds this word in any file it will list that file. How can i do this with perl/ruby/awk/sed/bash or... (9 Replies)
Discussion started by: cola
9 Replies

6. Shell Programming and Scripting

Search for file and size subdirectory as well

Hi I made this code to search in directory for file and size How can I remodel it to seach in the sub direcotry as well Thanks #!/bin/bash echo -n "Enter: " read var if then echo "Directory exists: ${var}" size=`du -hs "${var}"` echo The size of the current folder is... (4 Replies)
Discussion started by: lio123
4 Replies

7. Shell Programming and Scripting

How to traverse directory structure and sum size of files?

How do I write a bash or ruby or perl or groovy script to print all the files in my directory tree that are one-to-two years old, the size of each file, and the sum of file sizes and then delete them? I was using find . -atime +365 -exec rm '{}' \; but the problem was that I could not... (5 Replies)
Discussion started by: siegfried
5 Replies

8. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

9. UNIX for Dummies Questions & Answers

Create Year directory, date subdirectory and archive the file

Hi, After checking all the UNIX threads, I am able to come up with a solution so far. I am working on a shell script where it moves the files to a certain directory. The conditions to check are 1) Check if the file exists in the current directory. 2) Check if the destination directory... (2 Replies)
Discussion started by: madhunk
2 Replies

10. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies
Login or Register to Ask a Question