In the tcsh shell on Unix, I would like to print the folder name together with the number of files that I searched, then feed it to a file.

However I just manage to find the number of count then write to a file, not sure how to attach the folder name along.

my_folder.txt contains a list of directory names such as:

foreach a (`cat my_folder.txt`)
find ./netlists/spice/${a}.sp -newermt '8/16/2022 0:00:00' | wc -l  >check_latest.txt

Now that above code will output just the number of files, such as:


But I look to print out something like this:

dir_1   10
dir_2   5
dir_3   4

May I know how to achieve this?

  • I've never tried writing a tcsh script for the common reasons so idk but isn't foreach a (`cat my_folder.txt`) an antipattern in tcsh just like for a `cat my_folder.txt` or similar would be in a bourne derived shell like bash?
    – Ed Morton
    Commented Aug 17, 2022 at 16:02

3 Answers 3


I'm not entirely sure what you're doing, but this might be what you want:

foreach a (`cat my_folder.txt`) echo "$a " $(find ./netlists/spice/${a}.sp -newermt '8/16/2022 0:00:00' | wc -l)  >check_latest.txt; end

Personally, I'd split it onto multiple lines to make it more readable:

foreach a (`cat my_folder.txt`)
  count=$(find ./netlists/spice/${a}.sp -newermt '8/16/2022 0:00:00' | wc -l)
  echo "$a $count"
done >check_latest.txt
  • What shell are you writing this for? Note that you dropped end from the first command in the later loop and instead introduced do ... done and a sh-style command substitution using $(...), while keeping foreach which is not from standard sh. You also moved the redirection out of the loop without remarking on it.
    – Kusalananda
    Commented Aug 17, 2022 at 12:13
  • @Kusalananda, that code would work in zsh. Commented Aug 17, 2022 at 12:41
  • @StéphaneChazelas Which one, the first or the last or both? It would be good if the original user could clarify what shell they are using.
    – Kusalananda
    Commented Aug 17, 2022 at 12:49
  • @Kusalananda, second. The first would need to have the ;end removed unless the cshjunkieloops option is enabled (and from a functionality (as opposed to syntax) PoV, the redirection moved out of the loop at least). Commented Aug 17, 2022 at 12:56
  • Thanks Steve, and everyone's help this far. I try with Steve'code, however it doesn't works for me. I get 'Illegal variable name.' for Steve code #1. For code #2, my environment seem doesn't allow 'do' cmd.
    – Grace
    Commented Aug 17, 2022 at 13:44

Here's how I would do this in tcsh:

foreach a (`cat my_folder.txt`)
  set count=`find ./netlists/spice/${a}.sp -newermt '8/16/2022 0:00:00' | wc -l`
  echo "$a $count"

I'm not sure what you intend the redirection to do. If you actually want the results to end up in a file you need to do the redirection at the end of the loop:

foreach a (`cat my_folder.txt`)
  set count=`find ./netlists/spice/${a}.sp -newermt '8/16/2022 0:00:00' | wc -l`
  echo "$a $count"
end > check_latest.txt
import os
for i in m:
    print "{0} {1}".format(i.strip(),co_file)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .