Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of batch renaming based on Linux Shell

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "Linux Shell to achieve batch renaming method", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Linux Shell to achieve batch renaming method" bar!

0. Batch renaming with graphics software like GPRename

1. Delete all .bak suffixes:

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

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

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

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

Rename'yUnix Amurazapa Muhamzubo'*

4. Rename abcd.jpg to abcd_efg.jpg:

For var in * .jpg; do mv "$var"${var%.jpg} _ efg.jpg"; done

5. Rename 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 amurz A- Z`; done

7. Change the file format * _? .jpg to * _ 0?.jpg:

For var in `ls * _? .jpg`; do mv "$var" `echo "$var" | awk-F'_'{print $1 "_ 0" $2}'`; 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 last four letters of the file name to vzomik:

For var in `ls`; do mv-f "$var" `echo "$var" | sed's done.

10. Change .txt to the suffix of .txt _ bak

Ls * .txt | xargs-N1-I {} mv {} {} _ bak

Xargs-N1-I {} is similar to the for loop.-N1 means to deal with an object.-I {} replaces the previous object with {}. Mv {} {} _ bak is equivalent to mv 1.txt 1.txt_bak.

Find. / * .txt-exec mv {} {} _ bak\

This command also uses {} as a substitute for the file from the previous find, followed by the "\" for ";", otherwise shell will use the semicolon as the end of the line command.

=

Since you want to replace the filenames in batches, you must use a for loop to iterate through each file in the specified directory in turn. For each file, if the name of the file is name.oldext, then we must dig out the name from the original file name and splice it with the new file extension newext to form a new file name name.newext. Following this line of thinking, the following script is born:

#! / bin/bash

Oldext= "JPG"

Newext= "jpg"

Dir=$ (eval pwd)

For file in $(ls $dir | grep. $oldext)

Do

Name=$ (ls $file | cut-d. -F1)

Mv $file ${name}. $newext

Done

Echo "change JPG= > jpg done!"

Here is a brief explanation for this program:

1. The variables oldext and newext specify the old and new extensions, respectively. Dir specifies the directory where the file is located

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

3. In the loop, first use the cut command to put the file name in the "." The previous string is cut out and assigned to the name variable; the current file name is then renamed to the new file name.

Through this script, the extensions of all photos have been successfully modified. To make this script more generic, we can add a few read commands to enable interaction between the script and the user. The improved script is as follows:

#! / bin/bash

Read-p "old extension:" oldext

Read-p "new extension:" newext

Read-p "The directory:" dir

Cd $dir

For file in $(ls $dir | grep. $oldext)

Do

Name=$ (ls $file | cut-d. -F1)

Mv $file ${name}. $newext

Echo "$name.$oldext = = > $name.$newext"

Done

Echo "all files has been modified."

The modified script can modify any extension in batch.

At this point, I believe you have a deeper understanding of "Linux Shell to achieve batch renaming method", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report