The rename command modifies the file name with a Perl regular expression. Comprehensive use of Find, rename and mv.
1. The simplest way to rename an image file in the current directory with a specific format is to use the following script:
#!/bin/bash
#文件名: rename.sh
#用途: 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
Output Result:
$./rename.sh
Renaming back.jpg to Image-1.jpg
Renaming new.jpg to Image-2.jpg
Renaming Next.png to Image-3.jpg
The script renames all the. jpg and. png files in the current directory, the format of the new file name is Image-1.jpg, Image-2.jpg, and so on.
2. Working principle
Using the Find command to search,-O is used to specify multiple-iname options, which are used to perform case-insensitive matching. -maxdepth 1 refers to matching in the current directory.
To track the image number, initialize the Count=1, and the next step is to rename the file with the MV command. Therefore, a new file name needs to be constructed. ${img##*.} Resolves the file name that is in the current loop and obtains the filename extension.
Let count++ is used to increment the file number in each loop,
There are many other ways to perform a rename operation,
$ rename *. JPG *.jpg
Replace spaces in filenames with characters "_"
$ Rename ' s//_/g ' *
' s//_/g ' is used to replace a file name, and * is a wildcard used to match the target file, which can appear in *.txt or other styles
Convert file name to case
$rename ' y/a-z/a-z/' *
Move all. mp3 files into the given directory
$find path-type f-name "*.mp3"-exec mv {} target_dir \;
Replace spaces in all filenames with characters "_"
$find path-type f-exec Rename ' s//_/g ' {} \;
2.13 Batch Rename and move