?
Solved

TCSH script - add subfiles to a list

Posted on 2013-02-05
4
Medium Priority
?
316 Views
Last Modified: 2013-02-07
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:

#!/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"
...

Open in new window


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.
0
Comment
Question by:Geisrud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 1000 total points
ID: 38858088
If you are allowed to use the find command that would do it.

find $mydir -type f

This would find only, not directories or other types.

To get in a list you can do like
set fls = ( `find files -type f` )

Whether you do stat on each item in the list or ls -l, it is up to you.
0
 
LVL 79

Expert Comment

by:arnold
ID: 38858093
Do you need to recursively go through every directory in the file?
$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.
0
 
LVL 14

Author Closing Comment

by:Geisrud
ID: 38862593
Much thanks.  I was able to use your suggestion to arrive at a solution.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38863645
Glad to help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question