Convert uppercase to lowercase of file names

Try find (Linux command)

On many occasions it is preferable to have the lowercase file and directory names. Going one by one renaming is very hard work. Especially when there are hundreds or thousands of files, the job becomes more complicated and difficult to pass by hand. But that does not mean that it is impossible or that there are no methods to do it in a totally fast and automated way.

But beyond the pure aesthetics or the preferences of each user, sometimes it is about technical issues, since some apps cannot work with certain names in capital letters and they need to be converted. When you come across one of these apps, perhaps you have been accumulating files with names of all kinds for a long time, and going back becomes complicated. But don't worry, it has a simple solution as I show you in this tutorial ...

Convert uppercase to lowercase

Command to switch from uppercase to lowercase

The first thing you should know is that the following command will convert all uppercase to lowercase, including if there are subdirectories within that directory. So if you don't want all of them to be lowercase, just do it in the specific directory or it will convert everything for you. This seems important to me so that later you do not have problems and names that you did not want to be converted to lowercase are converted.

On the other hand, you need have the rename program installed. If you don't have it installed, use your distro's package manager to install it easily. However, in general you should have it installed, so it will not be necessary to install it in all cases. This tool will serve you to be able to modify several names at the same time, instead of using the typical mv to modify the name one by one ...

Another thing you need to know before getting down to business is that if when converting from uppercase to lowercase it comes across the same name, then it won't convert. You already know that Linux has case-sensitive in your FS, so it is case sensitive. That means that if you have a file called Hello2 and HELLO2, the system differentiates them. But, of course…, when they go to lowercase they would both be called hello2 and that's not possible. Therefore, it will throw an error message and it does not transform it.

After that you have everything you need to know and we can start to transform the names from uppercase to lowercase. The generic command It would be the following, and you can modify it to point to the directory you want:

<br data-mce-bogus="1">

<em>find &lt;nombre_directorio&gt; -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;</em>

You replace by the name of the directory or the path of the directory that you want to make lowercase. For example, like the example that I show you in the image that is Test, but it could also be any other. For newbies, say that by path I mean the path, when it is not directly within the directory where you are currently. For example, if you are in ~ / but want to act on / home / user / Downloads.

Convert it to script

To facilitate the uppercase and lowercase conversion, you can create a script let it do it for you and you won't have to enter the above command every time you need a conversion. This is very useful for those who are constantly passing names. Also, if you put it inside any of the paths of the $ PATH environment variable, you can execute it just by invoking its name, without having to have it in the same directory where you are working or specify the full path ...

The steps you must follow to create the converter script are:

  • The first is go to / bin directory to include the script there and thus use it like any other command, simply invoking its name from the terminal.
<br data-mce-bogus="1">

cd ~/bin<br data-mce-bogus="1">

  • Then create with nano, or with your favorite text editor, the file with the script and call it whatever you prefer. I'm going to call it mayutominu:
sudo nano mayutominu.sh

  • Inside nano you must paste the following text for the code of this script from bash:
</pre><pre>#!/bin/bash
if [ -z $1 ];then
echo "Uso :$(basename $0) parent-directory"
exit 1
fi

all="$(find $1 -depth)"

for name in ${all}; do
new_name="$(dirname "${name}")/$(basename "${name}" | tr '[A-Z]' '[a-z]')"
if [ "${name}" != "${new_name}" ]; then
[ ! -e "${new_name}" ] &amp;&amp; mv -T "${name}" "${new_name}"; echo "${name} was renamed to ${new_name}" || echo "${name} wasn't renamed!"
fi

done
exit 0</pre><pre>
  • Now save the file using the key combination Ctrl + O and exit with Ctrl + X. You already have your mayutominu.sh file created with the script, the following is give permission of execution:
sudo chmod +x mayutominu.sh
  • You finally got it ready to use. How is it done? Well, continuing with the same example in the previous section, if you want to change the names of the Test directory from uppercase to lowercase, you can do the following:
mayutominu Prueba
  • You know, if you need to specify the full path, or path, you can also, if the directory is not in the current position. For example:
mayutominu /home/usuario/Descargas

I hope it has helped you, you know that any questions or suggestions, you can leave your comments...


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   yo said

    In debian it tells me this:
    -bash: syntax error near unexpected token `newline

    find -depth | xargs -n 2 rename -v 's /(.*)\/(((^\/)*)/$ 1 \ / \ L $ 1 /' {} \;