2.13 Batch Rename and move

Source: Internet
Author: User
Tags perl regular expression

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.