Bin Chen
Bin Chen

Reputation: 63369

bash shell: how to rename files

I just want to rename all the *.a file to *.a.b in current directory and subdirs, how to do it in shell script?

Upvotes: 1

Views: 2177

Answers (4)

kurumi
kurumi

Reputation: 25609

Ruby(1.9+)

$ ruby -e 'Dir["**/*.a"].each{|x|File.file?x && File.rename(x,"#{x}.b")}'

In a shell script (at least Bash 4)

shopt -s globstar
shopt -s nullglob
for file in **/*.a
do
 echo mv "${file}" "${file}.b"
done

Upvotes: 2

George
George

Reputation: 2057

find . -type f -name '*.a' -print0 | xargs -0 -IZZ mv ZZ ZZ.b

This should handle filenames with spaces and / or newlines. It also doesn't rename directories (the other solution doing find would). If you want it to be case-insensitive, use "-iname" instead of "-name"

Upvotes: 7

Shumin Guo
Shumin Guo

Reputation: 184

Try the script below:

for file in `find . -name '*.a'`; do mv $file $file.b; done

Upvotes: 1

zebediah49
zebediah49

Reputation: 7611

To rename <files> with that, rename 's/\.a$/.a.b/' <files>. Doing so recursively will just take a bit of looping.

(or use *, */*, */*/*, */*/*/*, etc. for the files)

Upvotes: 1

Related Questions