massive rename on terminal and sequentially numbered

旧时模样 提交于 2021-01-27 17:38:50

问题


I want to rename a lot of files (some jpg and a lot of png files) I need them sequentially numbered on each folder have this structure:

.../folder01
    file.png
    file.jpg
.../folder02
    file.png
    file.png
  ..........
.../folder05
    file.png
    file.png

and I want something like this:

.../folder01
    0001.jpg
    0002.png
.../folder02
    0003.png
    0004.png
  ..........
.../folder05
    0012.png
    0013.png      

how can I make it using bash?


回答1:


Here's one way:

find . \( -name '*.jpg' -o -name '*.png' \) -print  | (i=0; while read f; do 
    let i+=1; mv "$f" "${f%/*}/$(printf %04d "$i").${f##*.}"; 
done)



回答2:


Here is another way using the automated tools of StringSolver:

mv folder01/file.jpg folder01/0001.jpg
mv folder01/file.png folder01/0002.png
mv

The second example is needed because else it would think about renaming all files using the number in the folder's name. The last two lines can also be abbreviated in just one, which performs the moves and immediately generalizes it:

mv -a folder01/file.png folder01/0002.png

DISCLAIMER: I am a co-author of this work for academic purposes, and working on a bash script renderer. But you can already test the system as it.



来源:https://stackoverflow.com/questions/14224166/massive-rename-on-terminal-and-sequentially-numbered

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