thelinuxvault blog

Renaming All Files and Directory Names to Lowercase in Linux

In a Linux environment, having consistent naming conventions for files and directories can be very beneficial. One common convention is to use lowercase names. This blog post will guide you through the process of renaming all files and directory names to lowercase. We'll cover different methods, common practices, best practices, and provide example usage.

2026-05

Table of Contents#

  1. Using the rename Command (Perl-based rename)
  2. Using find and mv
  3. Common Practices
  4. Best Practices
  5. Example Usage
  6. References

Using the rename Command (Perl-based rename)#

Installation#

First, make sure you have the Perl-based rename utility installed. On Debian/Ubuntu systems, you can install it using:

sudo apt-get install rename

On Red Hat/CentOS systems, you might need to enable the EPEL repository (if not already enabled) and then install:

sudo yum install rename

Basic Syntax#

The basic syntax for using rename to convert names to lowercase is:

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

Here's what's happening:

  • rename is the command.
  • 'y/A-Z/a-z/' is a Perl substitution. The y/// operator is used for transliteration. It takes all characters in the range A-Z (uppercase) and converts them to the corresponding characters in the range a-z (lowercase).
  • * is a wildcard that matches all files and directories in the current directory.

Recursive Renaming#

If you want to rename files and directories recursively (in subdirectories as well), you can use:

find . -depth -execdir rename 'y/A-Z/a-z/' {} +
  • find . starts searching from the current directory (.).
  • -depth processes directories after their contents (so that when renaming a directory, its contents are already renamed).
  • -execdir rename 'y/A-Z/a-z/' {} + executes the rename command on each found file or directory from within the directory containing the file ({} is replaced with the actual name, and + is used for efficiency to pass multiple arguments at once). Using -execdir instead of -exec ensures the command runs in the directory containing the file, avoiding path-related issues.

Using find and mv#

Basic Concept#

This method involves using find to locate files and directories and then using mv (move/rename) to change their names to lowercase.

Example Command#

find . -depth -exec sh -c 'for f; do mv "$f" "$(dirname "$f")/$(basename "$f" | tr "[:upper:]" "[:lower:]")"; done' _ {} +
  • find. -depth as before.
  • -exec sh -c... runs a shell command.
  • for f; do... done loops through each file/directory found (f is the variable).
  • mv "$f" "$(dirname "$f")/$(basename "$f" | tr "[:upper:]" "[:lower:]")" - dirname "$f" gets the parent directory path, basename "$f" gets the name of the file/directory, tr "[:upper:]" "[:lower:]" converts the name to lowercase, and then mv renames the file/directory.

Common Practices#

Backup First#

Before performing any mass renaming operation, it's a good common practice to create a backup of the directory (or directories) you're working on. You can use commands like cp -r (for recursive copy) to create a backup. For example:

cp -r original_directory backup_directory

Test on a Small Subset#

If you're unsure about how the renaming commands will behave, test them on a small subset of files/directories first. For example, if you have a directory with a few test files, run the renaming command on that directory to see if it works as expected.

Best Practices#

Use Version Control (if applicable)#

If the files/directories are part of a version-controlled project (like a Git repository), commit any changes before running the renaming commands. This way, if something goes wrong, you can easily roll back.

If there are symbolic links in the directory structure, be aware that renaming the target of a symlink (if it's part of the renaming operation) might break the symlink. You may need to handle symlinks separately, for example, by checking if a file is a symlink using test -L and then dealing with it appropriately.

Example Usage#

Scenario#

Suppose you have a directory my_project with the following structure:

my_project/
├── UPPERCASE_FILE.txt
├── MixedCaseDirectory/
│   └── AnotherMixedFile.jpg
└── README.MD

Using rename (Perl-based)#

cd my_project
rename 'y/A-Z/a-z/' *
find . -depth -execdir rename 'y/A-Z/a-z/' {} +

After running these commands, the structure will be:

my_project/
├── uppercase_file.txt
├── mixedcasedirectory/
│   └── anothermixedfile.jpg
└── readme.md

Using find and mv#

cd my_project
find . -depth -exec sh -c 'for f; do mv "$f" "$(dirname "$f")/$(basename "$f" | tr "[:upper:]" "[:lower:]")"; done' _ {} +

This will also result in the same lowercase-named files and directories as above.

References#

By following the methods and practices outlined in this blog post, you can effectively rename all files and directory names to lowercase in your Linux environment.