holo gram
holo gram

Reputation: 75

Turning Only Specific Letters of the name of a file to Uppercase with bash script

I need to rename a file by turning only some of the letters to uppercase.

E.g:

filename: cookies.txt

letters to be changed: c,k

filename final: CooKies.txt

All this should be done with a bash script.

Upvotes: 0

Views: 113

Answers (1)

Shawn
Shawn

Reputation: 52354

It's trivial to do in bash with a bash-specific parameter expansion form:

filename=cookies.txt
echo "${filename^^[ck]}"
# To actually rename a file:
# mv "$filename" "${filename^^[ck]}"

will uppercase every c and k in the expansion of $filename.

You can also use ${variable^pattern} to only uppercase the first character that matches pattern, and commas instead of carets to do lowercasing. Leaving out the pattern will convert the entire string (Or first character).

Upvotes: 2

Related Questions