18.20 Lowercase Filenames
20200915
There are times when we have a directory of files with some confused mixture of uppercase and lowercase names. On GNU/Linux the case of a filename is significant and thus we need to know how the precise capitalisation of a name in order to find a file. As a tradition filenames on Linux are typically all lowercase though today we also see mixed case filenames quite commonly. Non-the-less there are times when we may like to lowercase our filenames.
$ ls
Lemon_Squeeze_Track_01_Juice.abc
Lemon_Squeeze_Track_02_Justice-and-Penalty.abc
Lemon_Squeeze_Track_03_Junior-and-Petite.abc
Lemon_Squeeze_TRACK_04_Jump-and-Pan.abc
...
We use rename with the transliteration operator y
to lowercase the names of all files in the current directory. The
y
operator translates from one sequence of characters to
another. The -n
is short for --nono
and will
report what the command would do but not actually do it. The wildcard
for the filename argument (*
) will expand to all files in the
current directory but rename will only operate on those
files whose filename matches the transformation string.
$ rename -n 'y/A-Z/a-z/' *
rename(Lemon_Squeeze_Track_01_Juice.abc, lemon_squeeze_track_01_juice.abc)
rename(Lemon_Squeeze_Track_02_Justice-and-Penalty.abc, lemon_squeeze_track_02_j...
rename(Lemon_Squeeze_Track_03_Junior-and-Petite.abc, lemon_squeeze_track_03_jun...
rename(Lemon_Squeeze_TRACK_04_Jump-and-Pan.abc, lemon_squeeze_track_04_jump-and...
...
If the promise is correct then we can execute the command, using
-v
, short for --verbose
, in case a mistake is
made. If a mistake is made can copy the verbose output into a script
file and effectively create a script to undo the renaming.
$ rename -v 'y/A-Z/a-z/' *
Lemon_Squeeze_Track_01_Juice.abc renamed as lemon_squeeze_track_01_juice.abc
Lemon_Squeeze_Track_02_Justice-and-Penalty.abc renamed as lemon_squeeze_track_0...
Lemon_Squeeze_Track_03_Junior-and-Petite.abc renamed as lemon_squeeze_track_03_...
Lemon_Squeeze_TRACK_04_Jump-and-Pan.abc renamed as lemon_squeeze_track_04_jump-...
...
Our result is then a directory of files with all lowercase filenames.
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0