Convert nth character of all filenames in a directory to uppercase in bash

Viewed 64

For files like :

_aaa.txt _bbb.txt _ccc.txt

I want to convert them to :

_aAa.txt _bBb.txt

Any idea how to do this ?

3 Answers

In plain bash, using only shell parameter expansions to perform the conversion:

#!/bin/bash

n=3
for file in *; do
    [[ -f $file ]] || continue
    suffix=${file:n-1}
    mv -i "$file" "${file:0:n-1}${suffix^}"
done

Check the output of the following

#!/bin/bash

for filename in *; do    
newname=$(sed 's/./\U&/3' <<< "$filename")
echo "$filename --> $newname"
# mv $filename $newname
done

Then remove the #, if the filename printed is correct

If rename command is available, please try:

rename 's/^(..)(.)/$1\U$2/' *.txt
Related