Getting the total file size for certain files per directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the total file size for certain files per directory
# 1  
Old 03-26-2010
Getting the total file size for certain files per directory

Hi,

I am trying to get the total file size for certain files per directory.

I am using

find /DirectoryPath -name '*.dta' -exec ls -l {} \; | awk '{ print $NF ": " $5 }' > /users/cergun/My\ Documents/dtafiles.txt

but this lists all the files in the directories.

I need the total per directory for all dta files recursively.

path/directory1 : total size of dta files for directory1
path/directory2 : total size of dta files for directory2

Any suggestions?
# 2  
Old 03-26-2010
instead of using ls you can use du.

An example

Code:
du -sk /export/home/amit/*.dta | awk '{c+=$1} END {printf "%s KB\n", c}'

# 3  
Old 03-26-2010
amitranjansahu thank you for your answer but it did not work.

There are directories under the starting search directory.

I need the total size of dta files for each sub directory separately.

like:

/export/home/amit/a 587987
/export/home/amit/b 456445
/export/home/amit/c 87987
/export/home/amit/c/d 98797

a b c d are directories and numbers are total file size of dta files in those directories.

What do you recommend?
# 4  
Old 03-26-2010
I have one solution but it has one restriction.

It will go ahead one dir above and list the files size recursively.

Like under the mentioned path it will list the directories and search the *.dta files in these dirs recursively and give the size.


Code:
for dirname in `ls -F /export/home/amit/ | grep /`;
do
echo $dirname
find /export/home/amit/$dirname -name '*.dta' -exec du -sk {} \; | awk '{c+=$1} END {printf "%s KB\n", c}'
done

# 5  
Old 03-26-2010
amitranjansahu unfortunately this did not work either it listed a b c but not d

---------- Post updated at 12:44 AM ---------- Previous update was at 12:26 AM ----------

However it is about to work I guess,

I changed the code but I am not sure how I can print the directory variable with awk:

Code:
for dirname in `du /DirectoryPath/ | awk '{ print $NF }' `;
do
find $dirname -name '*.dta' -exec du -sk {} \; | awk '{c+=$1} END {printf " %s %s \n", dirname,c}'
done

dirname in awk did not work, where am I making the mistake?
# 6  
Old 03-26-2010
Soory i got it wrong .

You can try this it will work

Code:
for dirname in `find /export/home/amit -type dir`;
do
echo $dirname
find $dirname -name '*.dta' -exec du -sk {} \; | awk '{c+=$1} END {printf "%s KB\n", c}'
done

# 7  
Old 03-26-2010
it says:

find: invalid argument `dir' to `-type'

How can I write the dirname and total size in one line instead of 2 lines?

---------- Post updated at 01:17 AM ---------- Previous update was at 12:53 AM ----------

amitranjansahu I can't thank you enough.

Below is the code I am using. It seems like it is working.

I will appreciate if you can take a look.

Code:
for dirname in `du /DirectoryPath/ | awk '{ print $NF }' `;
do 
echo -n $dirname
find $dirname -name '*.dta' -exec du -sb {} \; | awk '{c+=$1} END {printf " %12.0f \n", c}';
done

Thanks a lot....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check total files size in hdfs directory?

Is there a way to calculate the total file size of HDFS file directory in GB or MB? I dont want to use du/df command. Without that is there a way HDFS Directory - /test/my_dir (1 Reply)
Discussion started by: rohit_shinez
1 Replies

2. Solaris

Find the total size of multiple files

If I have a number of files in a directory, for example, test.1 test.2 test.3 abc.1 abc.2 abc.3 and I need to find the total file size of all of the test.* files, I can use du -bc test.* in Linux. However, in Solaris, du does not have the -c option. What can I do in Solaris to get... (11 Replies)
Discussion started by: learnix
11 Replies

3. Shell Programming and Scripting

Find a particular directory in multiple file systems and calculate total size

Hello : I need some help in writing a ksh script which will find a particular directory in all the file systems in a server and finally report the total size of the direcotry in all the file systems. Some thing like this.. find /u*/app/oracle -type d -name "product" -prune and then... (1 Reply)
Discussion started by: Sam1974
1 Replies

4. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

5. Shell Programming and Scripting

Checking the total size of all files from a particular date

Hi I have some set of files for a particular date. What is the command that I need to put in for finding the total size of all the files for that particular date. The following command is fetching me the size of all individual files seperately du -h *20101010* 16M file1.20101010 120K... (10 Replies)
Discussion started by: bobby1015
10 Replies

6. Shell Programming and Scripting

Use Awk and Array to get total size of files

Hello all, I need to do scripts total up the size in selected extension file for example motion.mov and segmentation.avi is in Label Media. For file info.doc and calc.xls in Label Document. I need output will be like this: count 1 Media,,2 GB count 2 Document,,4 GB My problem is,... (16 Replies)
Discussion started by: sheikh76
16 Replies

7. UNIX for Dummies Questions & Answers

get total size of files

as we use du - sh *.frm In This command It will show the list of files and size But I want the Total size that these files in directory with extension .frm How can we do This (6 Replies)
Discussion started by: kaushik02018
6 Replies

8. UNIX for Dummies Questions & Answers

Find total size for some files?

Hi, I'm newbie to Unix. I'd like to count the total size of those files in my directory by date. For example, files on this period 05/01/08 - 05/31/08. If possible can we count by byte instead of kb. if I use $ du - ks , it will add up all files in the dir. thanks, Helen (5 Replies)
Discussion started by: helen008
5 Replies

9. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

10. HP-UX

total size taken by directory

HI ALL, i am a beginner in unix world. can anyone please tell me the way to find total size taken by each directory (including size of all subdirectories and files) present under /var. e.g what is space occupied by /var/adm , /var/admin etc. its a simple question but still i dont know the... (2 Replies)
Discussion started by: jyoti
2 Replies
Login or Register to Ask a Question