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