Batch convert file names to uppercase/lowercase in Linux

Source: Internet
Author: User


In Linux, the file name is converted to uppercase and lowercase in batches. A buddy called from the scene and said that there is a directory with thousands of files under it. Now, the file name must be converted from lowercase to uppercase. First, you can't write scripts. In that case, you have to teach him to use vi to change permissions. Instead, you can only use commands. Character conversion, of course, comes to mind: tr: www.2cto.com for file in *; do mv $ file 'echo $ file | tr 'a-Z' 'a-z ''; after a while, the buddy called again and said, "Do not change the extension name to uppercase. Keep it in lowercase. In this case, the obvious segmentation will certainly be handed over to awk:
For file in *; do mv $ file 'echo $ file | awk-F. '{print toupper ($1 )". "$2}''; done can modify a file similar to a.txt, but there are no more extensions, such as B .tar.gz. Fortunately, awk supports variables: www.2cto.com for file in *; do mv $ file 'echo $ file | awk-F. '{for (I = 1; I <= NF; I ++) if (I = 1) result = toupper ($ I); else result + = ". "$ I; print $ result;}''; done, but the result is wrong. The file name has not changed. Check whether gawk supports toupper and tolower, it is not correct to change to gawk. In the end, there is no way to find a compromise. First, convert all to uppercase, and then convert the extension back to lowercase: for file in *; www.2cto.com do mv $ file 'echo $ file | sed-e s /\. TXT /\. txt/G'; done \. not converted ., it still represents any character, so if basename contains an extension, it will be tragic.
After trying a few times, we found that this is the escape: sed-e s/[.] TXT/. txt/G'; OK.

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.