Joe Rud
asked on
TCSH script - add subfiles to a list
I'm working on an assignment, and I'm stuck on one point. I'm trying to return the sum of the size of all files in a directory (including sub directories). The du command is not allowed.
Consider the following directory structure:
testdir/
testdir/file1.txt
testdir/file2.txt
testdir/testsub/
testdir/testsub/file3.txt
testdir/testsub/file4.txt
My specific question is, how do I add the entire tree to a list in my script such that I can work with them the get the size. I can get the size once there.
So far I have the following:
When I search testdir I currently get the size of only that directory, of course.
I've tried setting filelist = `ls -R $mydir`which does add all the subfiles, but when I run my script is says "No such file or directory" for every item in the list.
Consider the following directory structure:
testdir/
testdir/file1.txt
testdir/file2.txt
testdir/testsub/
testdir/testsub/file3.txt
testdir/testsub/file4.txt
My specific question is, how do I add the entire tree to a list in my script such that I can work with them the get the size. I can get the size once there.
So far I have the following:
#!/bin/tcsh
...
echo "Enter Directory Name:"
set mydir = $<
echo "Calculating the total of the size of all files in the directory tree"
echo ""
set mysum = 0 #variable to hold sum
set filelist = $mydir
set mycursize = 0 #variable to hold current filesize
echo $filelist
foreach x ($filelist)
@ mycursize = `stat -c%s $x` #finds current filesize
echo "size of $x is $mycursize"
@ mysum = $mysum + $mycursize #accumulator
end
echo "The sum of the size of all files in this directory is: $mysum"
...
When I search testdir I currently get the size of only that directory, of course.
I've tried setting filelist = `ls -R $mydir`which does add all the subfiles, but when I run my script is says "No such file or directory" for every item in the list.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Much thanks. I was able to use your suggestion to arrive at a solution.
Glad to help
$x is a name, you need to use "$mydir/$x" which will include the explicit path/filename.
You can use ( test -f "$mydir/$x" ) regular file or use -d to test whether the item in the list is a directory.