find amount of files using c shell script

August 29, 2010 at 20:23:52
Specs: Windows 7
I wish to define a variable as the amount of files in a folder that match a certain pattern.

My c shell script contains the line:

set n = (ls -1 $1/*x* | wc -l)

This just returns errors, however typing "ls -1 folder/*x* | wc -l" directly into the command line returns the correct number.

I think I might be needing to define my variable as:

@ n = (ls -1 $1/*x* | wc -l)

but this doesn't fix the issue. I suspect the reason is because 'ls' is trying to echo a value. How do I set a variable as this value?


See More: find amount of files using c shell script

Report •

August 30, 2010 at 07:34:54
In csh, command substitution uses the back tics (the shift tilde ~)

set n=`ls -1 $1/*x* | wc -l`

Report •

August 31, 2010 at 18:07:07
Thank you. The script now works.

I don't suppose you could tell me how to suppress the error message that ls generates when n ends up being zero?


Report •

August 31, 2010 at 21:40:19
This works but it sure is a kludge. We're forced to send standard output to a separate file from standard error, and then read the file:

(ls -1 $1/*x* | wc -l > /tmp/output.txt ) >& /dev/null
set n = `cat /tmp/output.txt`
echo $n

With the bourne shell, you could do this:

n=`ls -1 $1/*x* 2> /dev/null | wc -l`
echo $n

IMO, this is another reason not to use csh. Check out this link:

Table 1 compares using standard error and output with sh vs. csh.

Report •

Related Solutions

September 23, 2010 at 21:07:44
Thanks again, this code works as well.

Thanks also for the advice about shells, but my suite of programs/scripts are all in csh so it's not really viable to rewrite them all just for the sake of elegance.

One last question, just to satisfy my own curiosity now:
The purpose of this code is to test whether or not there are any files in the specified folder which match the desired pattern. The actual quantity is irrelevant, only a true-false answer is required. Also my chosen coding solution is inelegant in that if there are a lot of matching files, it may take a while to count them all, which is unnecessary. So my question is, is there a more elegant way to achieve this in c shell? Perhaps some way to abort the count once it reaches 1? Or something entirely different? Just ideas for solutions are required. You don't have to code it up.


Report •

September 24, 2010 at 09:22:45
Unix shells have an exit code which is set when the previous command executes successfully or not. If the command is successful, the exit code is 0 or non-zero if it failed. In sh/ksh/bash, the exit code is $? while in csh, it is $status.

In your case, you could elimate counting the number of objects the ls command returns and look at the exit status. If no objects exist, $status will be non-zero (typically 1) while if there are objects, $status equals 0


ls -1 $1/*x*
echo $status

You could write code controlling what happens if $status equals some value.

Report •

September 26, 2010 at 21:51:41
Thanks. This seems like a much more elegant solution to my problem. The code I am using is:

ls -1 $1/*x* >& /dev/null
echo $status

This is now a beautiful little one-liner. I love it!

Report •

Ask Question