How to calculate file's size in directory and subdirectory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate file's size in directory and subdirectory
# 1  
Old 09-12-2003
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 subdirectory right after the parent directory, but not more than 2 subdirectories. What i meant is:

parent A --> Sub I --> Sub I1 --> sub I2

The above will works fine as long as all other sub directories are inside Sub I

But if I have another subdirectory as same level as Sub I's, my script wont work correctly:

parent A
--> Sub I --> Sub I1 --> sub I2
--> Sub II --> Sub II2 ......

my script would't work, it can still calculate for space taken by Sub I, but it can't do for SubII. It doesn't recognize SubII as a subdirectory.


Here is the code that I have written:

Thanks a lot
Quote:
#!/bin/bash

export total=0
function search {
if [ "$1" != "" ];then
cd "$1"
fi

for f in $(ls); do
echo "$f"
if [ -d $f ]
then {
echo "Ignoring directory $f"
search $f // do recursive
}
else {
size=`wc -c <"$f"`
echo "Size of file: $f is: $size"
total=`expr $total + $size`
}
fi
done
}
search $1
echo "There are $fileNum files."
echo "Total bytes is $total."
# 2  
Old 09-12-2003
method looks faulty

I think I spot 2 general problems with your algorithm

The recursion is greedily depth first, that is, it never unwinds from traversing the first (and only) directory it encounters.

Secondly the 'total' value you use to keep a running count is useless because it is limited in scope to the call to search.

You need to

1) deal with breadth, enumerate all dirs at one level on each descent and keep them somewhere to explore later

2) pass back your total as a return value and deal with it

Knuth deals with all the standard well known tree traversal methods, see if you can research from there to get a better idea.

personal opinion:
Although recursion is 'divine' , its usually a lot less assache to go at it with a good ole sledgehammer.
# 3  
Old 09-12-2003
KLL, please click on the rules link at the bottom of the page and read our rules. Notice what rule 6 says about classwork.

This looks like a homework assignment. If it's not, just do this:

du -sk /some/directory


Edit...

Some followups were posted discussing my above post. I split them off into a new thread which is here.

Last edited by Perderabo; 09-12-2003 at 10:39 PM..
# 4  
Old 09-13-2003
Thanks Andy.
# 5  
Old 10-16-2003
Hi,

I know that it is a very long time since, but I found this thread and I think it might interest you.

Regs David

dirinfo.pl
#----------------------------------------------------------------------------------

#!/opt/perl/bin/perl

use File::Find;

# Determine wether argument is given or not
# -----------------------------------------
if (@ARGV) {
@dirm = @ARGV;
foreach $dirm(@dirm) {
if ($dirm eq "\." ) {
$dirm = `pwd`;
$dirm =~ s/\n//g;
}
$dirm =~ s/\/$//g;
push(@directory, $dirm);
}
}
else {
usage();
}
# -----------------------------------------

# Define global vars and hashes
# -----------------------------------------
%seen = ();
$total = ();
$nr = 0;
$mbnr = 0;
# -----------------------------------------

# Searching starting at your given directory
foreach $directory (@directory) {
finddepth(\&wanted, $directory);
}


# If this is dir is unique, start getting total disk usage inclusive sub-dirs
# -----------------------------------------
sub wanted {
$dir = "$File::Find::dir";

unless ($seen{$dir}++) {
next if ( $dir =~ lost+found );
next if ( $dir eq $directory );
$dm=`du -ks $dir 2>/dev/null`;
($kb, $path) = split /\s+/, $dm ;
$mb = int( $kb / 1024 );

$output{$path} = $mb;

}
}
# -----------------------------------------

# Determine most characters of path and store it in $nr
# -----------------------------------------
@long = keys %output;
foreach $item(@long) {
if ( $nr < length($item)) {
$nr = length($item);
}
}
# -----------------------------------------


# Determine most characters of size and store it in $mbnr
# -----------------------------------------
@long = values %output;
foreach $item(@long) {
if ( $mbnr < length($item)) {
$mbnr = length($item);
}
}
# -----------------------------------------

# Add a space as long as the the current path is smaller then the longest one.
# -----------------------------------------
print "\n\n\n";
while (($key, $value) = each %output) {
$numb = ( $nr - length($key) );
$mbnumb = ( $mbnr - length($value) );
$dd = " Directory $key has";
while ( $numb > 0 ) {
$dd = $dd . " ";
$numb--;
}
while ( $mbnumb > 0 ) {
$dd = $dd . " ";
$mbnumb--;
}
push(@newarray, "$dd $value Mb\n");
}
# -----------------------------------------

# Print total directory size
# -----------------------------------------
foreach $line(sort(@newarray)) {
print $line;
}

print "\n\n";

foreach $directory(@directory) {
$total=`du -ks $directory 2>/dev/null`;
($total_size, $useless) = split /\s+/, $total;
$total_size_mb = int( $total_size / 1024 );
print "Total usage of $directory is $total_size_mb Mb\n";
}
print "\n\n";
# -----------------------------------------


sub usage() {
print "\ndirinfo.pl: Error incorrect usage \n\n";
print "Usage: /usr/local/bin/dirinfo.pl <directory> \n";
print "\t <directory> Is directory start point to check \n\n\n";
exit 1;
}
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. Programming

[c] How to calculate size of the file from size of the buffer?

Hi, Can I find size of the file from size of the buffer written? nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie); Thank You :) (1 Reply)
Discussion started by: ezee
1 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

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

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

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 .. #!/bin/ksh FNAME='.pdf' S_PATH=/abc/def/xyz find $S_PATH -exec ls -lad... (4 Replies)
Discussion started by: Vaddadi
4 Replies

8. 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

9. UNIX for Dummies Questions & Answers

calculate directory size by year of file

I need to calcualte the size of a directory by the year the files in that directory were created . For example the script will sum up, by year, the number of blocks for that directory and its' subdirectories for files created / accessed in that year. I need a report that would look like... (11 Replies)
Discussion started by: igidttam
11 Replies

10. UNIX for Dummies Questions & Answers

how to calculate the file size?

HI, Can somebody please tell me how many bytes make a KB & MB. ThANKS. Rooh.:( (3 Replies)
Discussion started by: rooh
3 Replies
Login or Register to Ask a Question