bash command for each file in a folder

Solution 1:

There are at least a hundred thousand million different ways of approaching this but here are the top contenders:

The Bash for loop

for f in ./*.doc; do
    # do some stuff here with "$f"
    # remember to quote it or spaces may misbehave
done

Using find

The find command has a lovely little exec command that's great for running things (with some caveats). Find is better than basic globbing because you can really filter down on the files you're selecting. Be careful of the odd syntax.

find . -iname '*.doc' -exec echo "File is {}" \;

Note that find is recursive so you might want to use -maxdepth 1 to keep find in current working directory. -type f can be used to filter out regular files.

If we're just renaming doc to txt...

The rename command is sed-like in searching. Obviously this won't do anything to convert the format.

rename 's/doc$/txt/' *.doc

Solution 2:

for i in *.doc ; do mv "$i" $(echo $i | sed s/doc/txt/) ; done