Reputation: 63369
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
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
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
Reputation: 184
Try the script below:
for file in `find . -name '*.a'`; do mv $file $file.b; done
Upvotes: 1
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