简体   繁体   中英

bash shell: how to rename files

我只想将所有* .a文件重命名为当前目录和子目录中的* .ab,如何在shell脚本中执行此操作?

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"

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

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

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

请尝试以下脚本:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM