Linux Rename Uppercase First Letter

I am writing a Bash script to clean up in my music.

I wanted him to format all the file names and make them, and so with a little search on the Internet I wrote this line:

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

What I used to add file names to a text file and then sed, but then I did not know how to apply the new names to the files.

So, I started experimenting with renaming and was able to get the same result except for the bold parts, which should make every first letter in a word in uppercase.

 rename 's/[-_]/ /g' * && rename 's/\s+/ /g' * && **rename 's/\s\w{1}/*AZ*/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 the rename works (at least satisfactorily), finding only one letter after the SPACE character, but this is a replacement problem. I tried many different approaches, and all this left me with the result that the first letter in focus in this case is exchanged exactly for AZ.

The rename man page on the page says that you do lowercase in uppercase, which you do / a -z / AZ / g, but itโ€™s easy to understand that it only applies when it finds az AZ. So I need help.

The bonus would be if someone knows how to do this, as in the sed example, where \ <matches the beginning of each word, because at the moment my rename command will not apply to the first word and will not apply if for obvious reasons there are several discs similar to "Disc name [Disc 1]".

+7
source share
2 answers

This is a Perl question, since rename written in Perl, and the instructions for performing renaming are the Perl command.

In a s/// , for the substitution to know which letter to insert the version in upper case, it must "capture the letter from the input." The brackets in the template do this by storing the captured letter in the variable $1 . A \u in substitution does the next uppercase character.

So you can do:

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

Please note that the replacement part must insert a space before the uppercase letter, because the template contains a space, and therefore the space and original letter are replaced. You can avoid this by using \b , a zero-width statement that matches only the word boundary:

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

Also, you do not need {1} , because \w (like other characters in regular expressions) by default corresponds to one character.

Finally, the example in rename (1) is actually y/AZ/az/ , using the operator y/// rather than s/// . y/// - a completely different operator that replaces all occurrences of one set of letters with another; this is not useful for you here, where these are just some of the characters you want to make in uppercase.

+8
source
 rename -nv 's{ (\A|\s) (\w+) }{$1\u$2}xmsg' 

This looks for the beginning of a line \A or for a space \s followed by at least one or more characters of the word (az, 0-9, underscore) \w+ . It will contain the upper case of the first character of all word sequences.

+3
source

All Articles