Mac/linux Shell Batch Rename method overview

Source: Internet
Author: User

Overview of methods for batch renaming of Linux shells

0. Batch rename with graphics software like Gprename

1. Remove all the. Bak suffixes:

Rename ' s/\.bak$//' *.bak

2. Modify the. jpe file suffix to. jpg:

Rename ' s/\.jpe$/\.jpg/' *.jpe

3. Change the file name of all files to lowercase:

Rename ' y/a-z/a-z/' *

4. Rename the abcd.jpg to abcd_efg.jpg:

for Var in *.jpg; Do mv "$var" "${var%.jpg}_efg.jpg"; Done

5. Rename the abcd_efg.jpg to abcd_lmn.jpg:

for Var in *.jpg; Do mv "$var" "${var%_efg.jpg}_lmn.jpg"; Done

6. Change all lowercase letters in the file name to uppercase letters:

for var in ' ls '; Do mv-f "$var" ' Echo ' $var "|tr A-Z"; Done

7, the format *_? JPG files to *_0?. Jpg:

for var in ' ls *_?. JPG '; Do mv "$var" ' Echo ' $var "|awk-f" _ "{print" "_0" "$"; Done

8. Change the first three letters of the file name to Vzomik:

for var in ' ls '; Do mv-f "$var" ' Echo ' $var "|sed ' s/^.../vzomik/"; Done

9. Change the second four letters of the file name to Vzomik:

for var in ' ls '; Do mv-f "$var" ' Echo ' $var "|sed ' s/....$/vzomik/"; Done

10. Turn. txt into a. txt_bak suffix

LS *.txt|xargs-n1-i{} mv {} {}_bak

xargs-n1–i{} resembles a For loop,-n1 means an object to be disposed of,-i{} replaces the preceding object with {}, mv {} {}_bak equivalent to MV 1.txt 1.txt_bak

Find./*.txt-exec mv {} {}_bak \;

The command also replaces {} as a file in the previous find, followed by "\" for ";" , or the shell will use the semicolon as the end of the line command.

========================================================================

Now that you want to replace the file name in bulk, you must iterate through each file in the specified directory with a for loop. For each file, if the name of the file is Name.oldext, then we have to dig out the name in the original file name and then stitch it with the new file extension newext to form a new filename name.newext. According to this idea, the following script was born:

#!/bin/basholdext= "jpg" newext= "jpg" dir=$ (eval pwd) for file in $ (ls $dir | grep. $oldext) do name=$ (LS $file | cut-d.-f1) MV $file ${name}. $newext Doneecho "Change jpg=====>jpg done!"

The following is a brief description of the program:

1. Variables Oldext and Newext specify the old extension and the new extension, respectively. DIR Specifies the directory where the file resides;

2. "LS $dir | grep. $oldext "used to get all files with the old extension in the specified directory dir;

3. In the loop, first use the Cut command to "." In the file name. The previous string is clipped and assigned to the name variable, and the current file name is renamed to the new file name.

With this script, all the photos ' extensions were successfully modified. To make this script more general-purpose, we can add several read commands to implement the interaction between the script and the user. The modified version of the script is as follows:

#!/bin/bashread-p "old extension:" oldextread-p "New extension:" Newextread-p "the Directory:" Dircd $dirfor a file in $ ( LS $dir | grep. $oldext) do name=$ (ls $file | cut-d.-f1) MV $file ${name}. $newext echo "$name. $oldext ====> $name. $newext "Doneecho" All files have been modified. "

The modified script can modify any extension in bulk.

from:http://edsionte.com/techblog/archives/category/shell%e7%bc%96%e7%a8%8b


Mac/linux Shell Batch Rename method overview

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.