Quantcast for loop in tcsh Text - Physics Forums Library

PDA

View Full Version : for loop in tcsh


ehrenfest
Jul14-08, 04:13 PM
Is it possible to do a for loop at the command line over all files in a given directory using the tcsh shell? I want to loop over all files and apply some commands (e.g. sed) to each one.

EDIT: I called it a "for" loop because I saw a "for" command when I googled this problem but of course I just want to loop over all the files

D H
Jul14-08, 04:51 PM
Yes. It's called foreach.

This will invoke the command 'do_something_to' on each .c file in the current directory:
foreach f (*.c)
do_something_to $f
end

I often use foreach in conjunction with the backtick operator. This will invoke the command 'do_something_to' on every .c file anywhere in the directory tree headed by the current directory:
foreach f (`find . -name '*.c'`)
do_something_to $f
end
The find command can of course do the above directly by using find's -exec option.