Shell script to add the extension in batches two ways to share the _linux shell

Source: Internet
Author: User

Method 1:

Copy Code code as follows:

For file in ' ls '; Do MV $file $file. txt; Done

Method 2:

Copy Code code as follows:

Find. -type f |xargs-i mv {} {}.txt

There are some unsuccessful trials, first recorded here.

1. Use the Rename command to modify the suffix name, this is the simplest and easiest way

Copy Code code 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 #把 *.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]#

2. Modify suffix name with for, SED and MV

Copy Code code 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 suffix names with Find and Xargs

Copy Code code 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]#

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.