Shell file name segmentation, rename mobile batch files

Source: Internet
Author: User
Split shellfile names, rename mobile batch file 1.split file name and extension name file"sample.jpg & quot; name $ {file %. *} echo & quot; Filenameis: $ name & quot; output: Filenameis: $ {file % in sample. *} means: use the file content, split it from the right shell file name, rename and move the batch file 1. split the file name and extension file = "sample.jpg" name =$ {file %. *} echo "File name is: $ name" output: File name is: $ {file % in sample. *} indicates that the string is matched from the right side using the file content. here is. * indicates that '. 'End, and then take the remaining left part as the result and return extension =$ {file #*.} echo "File extension is: $ extension" output: F Ile extension is: jpg $ {file #*.} this is the opposite of the previous one. it matches the string from the left and meets '. 'and then truncate the remaining part as the result to return the extension: here, a % and # are short matches, that is, % and # can be used for long matching when the first meeting ends. rename and move batch files 1. when the names of files exported from the camera are inconsistent, you can change the names in a unified manner, such as specifying the names on a certain day #! /Bin/bash # Filename: renameJpgAndPng. sh # Description: Rename jpg and png files count = 1for img in *. png *. jpgdonew = "image-$ count. $ {img ##*.} "# to be like 'image-x.jpg 'MV" $ img "" $ new "2>/dev/null if [$? -Eq 0]; then echo "Renaming $ img to $ new. "let count ++ fidone: defines a digital variable and reads all files in the current folder ending with png and jpg. the new variable is in the new file format, first, add the image-prefix and $ count to get the file suffix. Then use the mv command to move the rename. if error 2 (stderr) is generated, the system redirects to/dev/null to recycle the garbage. $? The result returned by the previous operation. if the value is 0, the operation is successful, and a message indicating successful conversion is displayed.
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.