Use shell batch to change file suffix names

Source: Internet
Author: User
Tags jpeg to jpg
Use shell batch to change file suffix names

There are times when you need to change the log files in the server to a different uniform format, and it's a good idea to use your scripts automatically. In this paper, the JPEG format file is changed to JPG format as an example.

Script to create 10 files, with JPEG as the suffix, the code is as follows:

#!/bin/sh for
((i=0;i<=10;i++)) doing touch
    ${i}.jpeg
Done

After executing the above script, you will see that 10 files are generated in the current directory, and the file name consists of Name.suffix

In order to change the files in the current directory in bulk, we need to traverse the current directory, get the name of the file that needs to be changed, and then splice with the new file suffix to form a new filename name.newsuffix.

The code is as follows:

#!/bin/sh
oldsuffix= "JPEG"
newsuffix= "jpg"
dir=$ (eval pwd) for

file in $ (ls $dir | grep. ${oldsuffix})
    do
        name=$ (ls ${file} | cut-d.-f1)
        mv $file ${name}.${newsuffix}
    did
echo "change jpeg to JPG successd! "
Oldsuffix is the file's old suffix, Newsuffix is the file's new suffix $ (cmd) is a command substitution that executes the cmd command when the statement is run, and then returns the execution result of the CMD command; the role of Eval is to perform command-line processing again (not two times for a command), That is, the arguments followed by the Eval are consolidated into the correct command line command execution. $ (eval pwd) The result returned after execution is the directory path where the script resides. In the loop statement, we use the Cut command to shear the matching file name. The cut command cuts bytes, characters, and fields from each line of the file and sends them to standard output. Parameter-D is used to customize the separator character, the default is tab, and the-D in the program. As a delimiter, parameter-f specifies which region to intercept, and-F1 represents the name of the filename that intercepts the first area.

Reference Articles link

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.