cergun
March 25, 2010, 11:41pm
1
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?
instead of using ls you can use du.
An example
du -sk /export/home/amit/*.dta | awk '{c+=$1} END {printf "%s KB\n", c}'
cergun
March 26, 2010, 12:49am
3
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?
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.
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
cergun
March 26, 2010, 1:44am
5
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:
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?
Soory i got it wrong .
You can try this it will work
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
cergun
March 26, 2010, 2:17am
7
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.
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....
I have modified it a bit and tried it in my system and it worked . Try it and let me know.
for dirname in `du /DirectoryPath/ | awk '{ print $NF }' `;
do
echo $dirname
ls -l $dirname/*.dta | awk '{print $5}' |awk '{c+=$1} END {printf "%s \n", c}'
done
cergun
March 26, 2010, 5:13pm
9
final code is:
for dirname in `du /DirectoryPath/ | awk '{ print $NF }' `;
do
echo -n $dirname
ls -l $dirname/*.dta 2>/dev/null | awk '{c+=$5} END {printf " %20.0f \n", c}';
done
I used ls instead of find because find was also adding sizef of the files in subdirectories.
Now I am trying to put it in shell script and trying to get the output to a text file.
Any suggestions?
cergun
March 29, 2010, 1:31pm
10
As I am throwing these to excel, I decided to put the size in the beginning so I seperate columns with "/". Therefore the change of number of columns in each line does not cause any problems with the size. The code is now like that:
for dirname in `du /dIRECTORYpATH/ | awk '{ print $NF }' `;
do
ls -l $dirname/*.dta 2>/dev/null | awk '{c+=$5} END {printf "%20.0f", c}' | tee -a /users/cergun/dta_files.txt
echo $dirname | tee -a /users/cergun/dta_files.txt
done