Solved

TCSH script - add subfiles to a list

Posted on 2013-02-05
4
309 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
Comment Utility
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 76

Expert Comment

by:arnold
Comment Utility
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
Comment Utility
Much thanks.  I was able to use your suggestion to arrive at a solution.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Glad to help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
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 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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now