Click to See Complete Forum and Search --> : bash : execute command for each file in directory


Nobody's Hero
11-30-2003, 02:25 PM
Just a quick question that I'm sure is very easy to answer.

I want to run the command "spamc -r <$file" in a for loop where $file is every file in a given directoy.

I plan on learning shell scripting soon, but for now I just need a quick solution... thanks..

Nobody's Hero
11-30-2003, 02:28 PM
nevermind, this seems to do it

#!/bin/bash

directory=/home/mmolino/Mail/.spam.directory/undetected/cur/

for file in $( ls $directory )
do
spamc -r <$directory$file
done


i had some quotes which were throwing things off.

bwkaz
11-30-2003, 03:03 PM
That'll work as long as none of the files have spaces in their name.

Better would be something like this:

find /home/mmolino/Mail/.spam.directory/undetected/cur -type f -maxdepth 1 -exec spamc -r <{} \;

which will work even if there are spaces in the filename.