Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Directory "$d" contains a few thousand e-mail files with the .txt extension. To open them in my e-mail client, I need to rename them to .eml

Will this command rename them correctly:

find "${d}" -type f -name '*.txt' | while read f; do mv -vn "${f}" "${f%.*}".eml; done

or is there a better, more robust way to do this?

I could not think of an elegant way of doing this using:

-exec ...{}... \;
share|improve this question

3 Answers

up vote 1 down vote accepted

Your solution is generally ok, but it will break on newlines. Here is a slightly more robust bash4+ solution:

shopt -s globstar nullglob
for file in **/*.txt; do
    mv "$file" "${file%.*}.eml"
done
share|improve this answer
Awesome... thanks! – Robottinosino Jun 11 '12 at 20:32

I think you should be fine with

find "$d" -name \*.txt -exec rename .txt .eml {} \;

or even

for f in *.txt; do rename .txt .eml "$f"; done

if all the files are in the same directory.

share|improve this answer
The system I am on does not have "rename". – Robottinosino Jun 11 '12 at 18:25

Yes, your command will work, assuming you're using bash or a shell with similar syntax. In the future when you're contemplating a big command like this, remember that you can use echo to preview the resulting command lines. I.e. you could put echo in front of mv, run the pipeline and see what the commands are going to be. If they look OK, remove echo and run the command for real.

share|improve this answer

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.