Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change lowercase file names to uppercase with awk ,sed or bash

Tags:

bash

sed

awk

I would like to change lowercase filenames to uppercase with awk/sed/bash

your help would be appreciated

aaaa.txt
vvjv.txt
acfg.txt

desired output

AAAA.txt
VVJV.txt
ACFG.txt
like image 460
rebca Avatar asked Sep 17 '12 22:09

rebca


People also ask

How do you change from lowercase to uppercase in Bash?

To define uppercase, you can use [:upper:] or [A-Z] and to define lowercase you can define [:lower:] or [a-z]. The `tr` command can be used in the following way to convert any string from uppercase to lowercase. You can use `tr` command in the following way also to convert any string from lowercase to uppercase.

How do you change lowercase to awk?

Of course to convert into upper case, simply use the function toupper() instead of tolower().

How do I change a file name to uppercase?

Within Windows Explorer, right click on the file name, click on “Rename” and change the file extension to . bak and then again change it to uppercase/lowercase.


1 Answers

PREFACE:

If you don't care about the case of your extensions, simply use the 'tr' utility in a shell loop:

for i in *.txt; do mv "$i" "$(echo "$i" | tr '[a-z]' '[A-Z]')"; done

If you do care about the case of the extensions, then you should be aware that there is more than one way to do it (TIMTOWTDI). Personally, I believe the Perl solution, listed here, is probably the simplest and most flexible solution under Linux. If you have multiple file extensions, simply specify the number you wish to keep unchanged. The BASH4 solution is also a very good one, but you must be willing to write out the extension a few times, or alternatively, use another variable to store it. But if you need serious portability then I recommend the last solution in this answer which uses octals. Some flavours of Linux also ship with a tool called rename that may also be worth checking out. It's usage will vary from distro to distro, so type man rename for more info.

SOLUTIONS:

Using Perl:

# single extension
perl -e 's/\.[^\.]*$/rename $_, uc($`) . $&/e for @ARGV' *.txt

# multiple extensions
perl -e 's/(?:\.[^\.]*){2}$/rename $_, uc($`) . $&/e for @ARGV' *.tar.gz

Using BASH4:

# single extension
for i in *.txt; do j="${i%.txt}"; mv "$i" "${j^^}.txt"; done

# multiple extensions
for i in *.tar.gz; do j="${i%.tar.gz}"; mv "$i" "${j^^}.tar.gz"; done

# using a var to store the extension:
e='.tar.gz'; for i in *${e}; do j="${i%${e}}"; mv "$i" "${j^^}${e}"; done

Using GNU awk:

for i in *.txt; do

    mv "$i" $(echo "$i" | awk '{ sub(/.txt$/,""); print toupper($0) ".txt" }');
done

Using GNU sed:

for i in *.txt; do

    mv "$i" $(echo "$i" | sed -r -e 's/.*/\U&/' -e 's/\.TXT$/\u.txt/');
done

Using BASH3.2:

for i in *.txt; do

    stem="${i%.txt}";

    for ((j=0; j<"${#stem}"; j++)); do

        chr="${stem:$j:1}"

        if [[ "$chr" == [a-z] ]]; then

            chr=$(printf "%o" "'$chr")

            chr=$((chr - 40))

            chr=$(printf '\'"$chr")
        fi

        out+="$chr"
    done

    mv "$i" "$out.txt"

    out=
done
like image 61
Steve Avatar answered Nov 15 '22 06:11

Steve