Rename Files and Directories to Lowercase on Linux

Rename Files and Directories to Lowercase on Linux

Prateek Dagur Linux

Table of Contents

You're probably already familiar with mv command to rename a file on Linux. But have you ever had to convert a bunch of filenames to lowercase on Linux? If you have, then you know it can be a bit of a pain.

There are several different ways to handle this on Linux. One method will use native mv utility and tr command, while the others will use rename and mmv tools.

In this guide, we are going to see several commands to rename folders and filenames to lowercase letters. Some of them will work only on files, some on directories and others would work recursively. Examine the various commands below to determine which one to use based on your requirements.

Prerequisites

This guide is useful for any Linux distro. Before you start, you should have administrative privileges to your Linux system either as a root or a non-root user with sudo privileges.

Installation

We're going to see three methods below to rename files from uppercase to lowercase. In case you select rename or mmv command to do this task, you first make sure whether they are installed on your system.

Note: mv belongs to the coreutils. That means you have mv already installed. If you see an error message like mv: command not found, your system is perhaps broken and needs troubleshooting.

Install rename or mmv

On Ubuntu, Debian, and Linux Mint:

# install rename
sudo apt install rename

# install mmv
sudo apt install mmv

On CentOS, Fedora, AlmaLinux, and Red Hat:

# install rename
sudo dnf install prename

# install mmv
sudo dnf install mmv

On Arch Linux and Manjaro:

# install rename
sudo pacman -S perl-rename

# install mmv
git clone https://aur.archlinux.org/mmv.git
cd mmv
makepkg -si

Renaming Files and Directories

Before converting filenames, make sure your are in correct working directory.

cd wherever/files/are

Using mv and tr Commands

for filename in *; do mv -i $filename `echo $filename | tr 'A-Z' 'a-z'`; done

You can also use tr '[:upper:]' '[:lower:]' instead. tr is used to translate or delete characters from standard input, writing to standard output. Use man tr for more details.

Let us expand above command to understand what's going on here. So, if you want to create a bash script, it will look like this:

for filename in *
do
  newname=`echo ${filename} | tr '[:upper:]','[:lower:]'`
  mv ${filename} ${newname}
done

Here's what the above code is doing:

  1. Go through each file in the current directory.
  2. Convert the name of each file to lowercase and store the result in the variable newname.
  3. Rename each file with the new lowercase name stored in newname.

Using rename Command

The rename is much simpler command for conversion.

rename 'y/A-Z/a-z/' *

In case above command does not work, try using -f flag:

rename -f 'y/A-Z/a-z/' *

Using mmv Command

This is perhaps the simplest method to rename uppercase letters to lowercase.

mmv '*' '#l1'

Recursively from Uppercase to Lowercase

Recursively Change all Directories and Filenames

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

Here's what the above code is doing:

  1. find . -depth: This will traverse the directory tree recursively and find files and directories.
  2. xargs -n 1: This flag tells xargs to treat each line of input as a single argument. Without this flag, xargs will create a single command with as many arguments as its limit. Use xargs --show-limits to list the existing limits on your system.
  3. rename 's/(.*)\/([^\/]*)/$1\/\L$2/': This is a Perl expression which will be used to modify the filenames. The \L modifier in Perl is used for lowercase characters.
  4. {} \;: This is a placeholder for the filenames that find finds. This tells the rename command to perform the action on each file found.

Recursively Change Filenames Only

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

Here, -type f  tells that file is of type regular file.

Recursively Change Directories Names Only

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

Here, -type d tells that file is of type directory.

Conclusion

Bulk renaming files from uppercase to lowercase characters on Linux can be done using several different methods. You can do this on both files and directories. The method you choose will depend on your specific needs and preferences.

If you want to rename files from lowercase to uppercase, check our guide on Rename Files and Directories to Uppercase on Linux.

Comments