Solved

TCSH script - add subfiles to a list

Posted on 2013-02-05
4
313 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 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 78

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sqlldr issue 1 35
HOw To Install Docker on VMware Workstation 19 617
PHP website on Linux - server DNS address could not be found. 18 112
Scripting 4 32
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.
Suggested Courses

737 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