batch rename files with ids intact

只谈情不闲聊 提交于 2019-12-20 03:49:12

问题


i have a directory listing like

seascaperecovered0088crop.jpg 
seascaperecovered0096crop.jpg
seascaperecovered0098crop.jpg
seascaperecovered0101crop.jpg
seascaperecovered0103crop.jpg
seascaperecovered0105crop.jpg
seascaperecovered0107crop.jpg
seascaperecovered0112crop.jpg
seascaperecovered0119crop.jpg
seascaperecovered0122crop.jpg

and i want to rename all files as seen here:

seascape_0122.jpg

i have tried something like this:

for f in `ls | egrep 'seascaperecovered.*\.jpg'`; 
do mv $f ${f/seascaperecovered/seascape}; 
done

i have read that you can do this with mv, rename, sed, awk, etc. can someone point me to the easiest (and clearest, hopefully) way of accomplishing this in UNIX? FWIW, I am ssh'd into a Linux machine and running a bash shell.

thanks, jml


回答1:


Very straightforward:

for i in seascaperecovered*.jpg; do A=${i/crop/}; mv $i ${A/recovered/_}; done

(Put echo before the mv first for a dry run.)




回答2:


With bash regular expressions

for file in *; do 
  [[ "$file" =~ [0-9]+ ]] && mv "$file" seascape_${BASH_REMATCH[0]}.jpg 
done


来源:https://stackoverflow.com/questions/7086513/batch-rename-files-with-ids-intact

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!