in reply to Run a script on every file in a directory

This is a shell question, but you didn't specify which shell you use. I answered for the two most common shells.

If your script takes multiple file names:

# bash script * # cmd perl -e"@ARGV = glob('*'); do 'script' or die $@||$!"

If you're script takes a single file name:

# bash for q in * ; do script "$q" ; done # cmd for %q in (*) do script "%q"

Replies are listed 'Best First'.
Re^2: Run a script on every file in a directory
by afoken (Chancellor) on Jun 08, 2010 at 20:07 UTC

    Not quite right. Both bash versions will leave out the "hidden" files with names starting with a ., and they will process every other directory entry, not just ordinary files, but also special files like symlinks, directories, device nodes, pipes. The cmd for loop also skips files with the hidden bit set.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      More like insufficient information in the question, so I answered the typical case. Should the OP answer me the following questions and specify the target shell, I'll provide.

      • Should directory "." be included? (I assumed no)
      • Should directory ".." be included? (I assumed no)
      • Should other directories be included? (I assumed yes)
      • Should other non-plain files be included? (I assumed yes)
      • Should other hidden files be included? (I assumed no)
      • Should subdirectories be visited recursively? (I assumed no)