Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

For loop in tcsh

  1. Jul 14, 2008 #1
    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
     
  2. jcsd
  3. Jul 14, 2008 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Yes. It's called foreach.

    This will invoke the command 'do_something_to' on each .c file in the current directory:
    Code (Text):
    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:
    Code (Text):
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: For loop in tcsh
  1. For loop in c (Replies: 3)

  2. Freal Loops? (Replies: 1)

  3. Loop in c (Replies: 1)

  4. Cool Loops (Replies: 12)

Loading...