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

How to write Shell scripts to add extensions in batches

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to write Shell scripts to add extensions in batches". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Method 1:

The code is as follows:

For file in `ls`; do mv $file $file.txt; done

Method 2:

The code is as follows:

Find. -type f | xargs-I mv {}. Txt

There are also some unsuccessful experiments, which are recorded here first.

1. Using the rename command to modify the suffix is the simplest and most convenient way.

The code is as follows:

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 00:57 rename1.log

-rw-r-r- 1 root root 0 09-27 00:57 rename2.log

-rw-r-r- 1 root root 0 09-27 00:57 rename3.log

-rw-r-r- 1 root root 0 09-27 00:57 rename4.log

-rw-r-r- 1 root root 0 09-27 00:57 rename5.log

[root@demo test_rename] # rename log txt * .log # change * .log to * .txt

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 00:57 rename1.txt

-rw-r-r- 1 root root 0 09-27 00:57 rename2.txt

-rw-r-r- 1 root root 0 09-27 00:57 rename3.txt

-rw-r-r- 1 root root 0 09-27 00:57 rename4.txt

-rw-r-r- 1 root root 0 09-27 00:57 rename5.txt

[root@demo test_rename] #

two。 Modify the suffix with for, sed, and mv

The code is as follows:

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 01:51 rename1.log

-rw-r-r- 1 root root 0 09-27 01:21 rename2.log

-rw-r-r- 1 root root 0 09-27 01:21 rename3.log

-rw-r-r- 1 root root 0 09-27 01:21 rename4.log

-rw-r-r- 1 root root 0 09-27 01:21 rename5.log

[root@demo test_rename] # for i in $(ls.)

> do

> mv $I $(echo $I | sed's /\ .log /\ .txt /')

> done

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 01:51 rename1.txt

-rw-r-r- 1 root root 0 09-27 01:21 rename2.txt

-rw-r-r- 1 root root 0 09-27 01:21 rename3.txt

-rw-r-r- 1 root root 0 09-27 01:21 rename4.txt

-rw-r-r- 1 root root 0 09-27 01:21 rename5.txt

[root@demo test_rename] #

3. Add a suffix with find and xargs

The code is as follows:

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 02:20 rename1

-rw-r-r- 1 root root 0 09-27 02:20 rename2

-rw-r-r- 1 root root 0 09-27 02:20 rename3

-rw-r-r- 1 root root 0 09-27 02:20 rename4

-rw-r-r- 1 root root 0 09-27 02:20 rename5

[root@demo test_rename] # find. -type f | xargs-I mv {}. Txt

[root@demo test_rename] # ll

Total 20

-rw-r-r- 1 root root 0 09-27 02:20 rename1.txt

-rw-r-r- 1 root root 0 09-27 02:20 rename2.txt

-rw-r-r- 1 root root 0 09-27 02:20 rename3.txt

-rw-r-r- 1 root root 0 09-27 02:20 rename4.txt

-rw-r-r- 1 root root 0 09-27 02:20 rename5.txt

[root@demo test_rename] #

This is the end of "how to write Shell scripts to add extensions in batches". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report