- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 124字
- 2021-07-09 19:46:23
How to do it...
The following script uses find to locate PNG and JPEG files, then uses the ## operator and mv to rename them as image-1.EXT, image-2.EXT, and so on. This changes the file's name, but not its extension:
#!/bin/bash #Filename: rename.sh #Desc: Rename jpg and png files count=1; for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1` do new=image-$count.${img##*.} echo "Renaming $img to $new" mv "$img" "$new" let count++ done
The output is as follows:
$ ./rename.sh Renaming hack.jpg to image-1.jpg Renaming new.jpg to image-2.jpg Renaming next.png to image-3.png
The preceding script renames all the .jpg and .png files in the current directory to new filenames in the format image-1.jpg, image-2.jpg, image-3.png, image-4.png, and so on.