Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Rename command uppercase first letter

I'm writing a Bash script for cleaning up in my music.

I wanted it to format all the file names and making them and so with a little internet search I wrote this line:

sed -i -e 's/[-_]/ /g' -e 's/ \+/ /g' -e **'s/\<[a-z]/\U&/g'** -e "s/$artist //g" -e "s/$album //g"

Which I used to add the file names to a text file and then sed it, but then I didn't know how to apply the new names to the files.

So then I started experimenting with rename and managed to get the exact same result except for the bolded parts, which is supposed to make every first letter in a word uppercase.

rename 's/[-_]/ /g' * && rename 's/\s+/ /g' * && **rename 's/\s\w{1}/*A-Z*/g' *** && rename 's/^\d+[[:punct:]]\s//g' * && rename "s/$artist\s//g" * && rename "s/$album\s//g" * && rename "s/($ext)//g" *

Now, the code in rename is working (satisfactorily at least), finding only one letter after a SPACE character, but it's the replacement that is problematic. I've tried numerous different approaches, all leaving me with the result that the first letter in focus get exchanged to exactly A-Z in this case.

In the rename manual page it says to make lower case uppercase you do 's/a-z/A-Z/g' but it's easy to figure that it only applies when it finds a-z A-Z. So this is what I need help with.

A bonus would be if someone knows how to do it like in the sed example, where the \< matches the beginning of each word, because at the moment, my rename command won't apply to the very first word and neither will it apply if there are multiple discs looking like "Disc name [Disc 1]" for obvious reasons.

like image 476
Jompa Avatar asked Jan 09 '13 16:01

Jompa


People also ask

How do I make the first letter capital in Linux?

'^' symbol is used to convert the first character of any string to uppercase and '^^' symbol is used to convert the whole string to the uppercase. ',' symbol is used to convert the first character of the string to lowercase and ',,' symbol is used to convert the whole string to the lowercase.

How do I change capitals to lowercase in Linux?

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.

Is there a rename command in Linux?

You can use the built-in Linux command mv to rename files. Here are some of the options that can come in handy with the mv command: -v , --verbose : Explains what is being done. -i , --interactive : Prompts before renaming the file.

Which command changes string to uppercase in Linux?

The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace.


1 Answers

This is sort-of a Perl question, since rename is written in Perl, and instructions for how to perform the renaming are a Perl command.

In a s/// in order for the substitution to know which letter to insert the upper-case version of, it has to ‘capture’ the letter from the input. Parentheses in the pattern do this, storing the captured letter in the variable $1. And \u in a substitution makes the next character upper-case.

So you can do:

$ rename 's/\s(\w)/ \u$1/g' *

Note that the replacement part has to insert a space before the upper-case letter, because the pattern includes a space and so both the space and the original letter are being replaced. You can avoid this by using \b, a zero-width assertion which only matches at a word boundary:

$ rename 's/\b(\w)/\u$1/g' *

Also you don't need the {1} in there, because \w (like other symbols in regexs) matches a single character by default.

Finally, the example in rename(1) is actually y/A-Z/a-z/, using the y/// operator, not s///. y/// is a completely different operator, which replaces all occurrences of one set of letters with another; that isn't of use to you here, where it's only some characters you want making upper-case.

like image 163
Smylers Avatar answered Sep 24 '22 23:09

Smylers