Solved

TCSH script - add subfiles to a list

Posted on 2013-02-05
4
311 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
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 250 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 77

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ifconfig 4 69
How to enable sync between two yum repo? 1 39
awk file 6 78
Setup Ubuntu 16.04 to use LDAP for user login and authentication 2 81
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…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

828 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