Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a set of files on which I would like to apply the same command and the output should contain the same name as the processed file but with a different extension.

Currently I am doing rename /my/data/Andrew.doc to /my/data/Andrew.txt I would like to do this for all the .doc files from the /my/data/ folder and to preserve the name.

I tried several versions but I guess I have something wrong in the syntax as I an new to linux.

share|improve this question
add comment

2 Answers 2

up vote 10 down vote accepted

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 {}" \;

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
share|improve this answer
add comment
for i in *.doc ; do mv "$i" $(echo $i | sed s/doc/txt/) ; done
share|improve this answer
    
thank you Eric. –  Robert Jul 10 '13 at 12:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.