A feature I love with the csh / tcsh

The foreach command which I never remember how to do, is a very powerful tool in the csh, which is missed in bash.

For those who don’t know it, foreach can be used in the command line interface (cli) in csh, unlike some other shells.

With foreach you can do some very simple directory work without having to first make a shell script.

Below is a very simple way of using the command.

First lets prepare something to work with, create a test dir that can be easily be removed afterwards

# mkdir test && cd test

Prepare some directories. these can also be files if you want to work with this.

# mkdir 1 2 3 4 5 6 7 8 9

The few commands to populate each directory from 1-9 with a file called test

# foreach file (*)
# cd $file && touch test && cd ..
# end

Now go and take a look in each directory, and you’ll see a file called test.

Leave a Reply

Your email address will not be published. Required fields are marked *