Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TCSH script - add subfiles to a list

Posted on 2013-02-05
4
Medium Priority
?
317 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 80

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

636 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