VOOZH about

URL: https://www.tecmint.com/rename-all-files-and-directory-names-to-lowercase-in-linux/

โ‡ฑ Rename All Files and Directory Names to Lowercase in Linux


Skip to content

In our previous article, we have described how to count the number of files and subdirectories inside a given directory. This guide will show you how to rename all files and directories names to lowercase in Linux.

Read Also: How to Find Out Top Directories and Files (Disk Space) in Linux

There are several ways to achieve this, but weโ€™ll explain two of the most efficient and reliable methods. For the purpose of this guide, we have used a directory named Files which has the following structure:

# find Files -depth
๐Ÿ‘ List Directory Structure
List Directory Structure

1. Using find, xargs and rename Commands Together

rename is a simple command line utility for renaming several files at once in Linux. You can use it together with find utility to rename all files or subdirectories in a particular directory to lowercase as follows:

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

Explanation of options used in the above command.

  • -depth โ€“ lists each directoryโ€™s contents before the directory itself.
  • -n 1 โ€“ instructs xargs to use at most one argument per command line from find output.

Sample output after renaming files and subdirectories to lowercase in Files directory.

๐Ÿ‘ Rename Files and Directory Names to Lowercase
Rename Files and Directory Names to Lowercase

Another alternative way using the find and mv commands in a script as explained below.

2. Using find and mv Commands in Shell Script

First create your script (you can name it anything you prefer):

$ cd ~/bin
$ vi rename-files.sh

Then add the code below in it.

#!/bin/bash
#print usage 
if [ -z $1 ];then
 echo "Usage :$(basename $0) parent-directory"
 exit 1
fi

#process all subdirectories and files in parent directory
all="$(find $1 -depth)"



for name in ${all}; do
 #set new name in lower case for files and directories
 new_name="$(dirname "${name}")/$(basename "${name}" | tr '[A-Z]' '[a-z]')"

 #check if new name already exists
 if [ "${name}" != "${new_name}" ]; then
 [ ! -e "${new_name}" ] && mv -T "${name}" "${new_name}"; echo "${name} was renamed to ${new_name}" || echo "${name} wasn't renamed!"
 fi
done

echo
echo
#list directories and file new names in lowercase
echo "Directories and files with new names in lowercase letters"
find $(echo $1 | tr 'A-Z' 'a-z') -depth

exit 0

Save and close the file, then make the script executable and run it:

$ chmod +x rename-files.sh
$ rename-files.sh Files #Specify Directory Name
๐Ÿ‘ Lowercase File Names Using Script
Lowercase File Names Using Script

You may also like to read these following related articles.

  1. Explanation of โ€œEverything is a Fileโ€ and Types of Files in Linux
  2. fswatch โ€“ Monitors Files and Directory Changes or Modifications in Linux
  3. Fasd โ€“ A Commandline Tool That Offers Quick Access to Files and Directories
  4. FSlint โ€“ How to Find and Remove Duplicate Files in Linux

In this guide, we expalined you how to rename all files and directories to lowercase in Linux. If get any errors, please hit us up via the feedback form below. You can as well offer us any other methods of doing the same.

If this article helped, share it with someone on your team.
TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
โ˜•
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
โ˜• Buy Me a Coffee
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

3 Comments

Leave a Reply
  1. Hi, you can execute your first option with a single command:

    find Files -depth -exec rename -v -f 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \+

    Reply
  2. Might be easier in a for loop using var substitution.

    $ for i in ./*; do echo $i ${i,,}; done
    

    Replace โ€œechoโ€ with โ€œmvโ€.

    Reply
  3. Does this command also work in CentOS 7?

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

    I donโ€™t get an error but it doesnโ€™t seem to work.

    Is it also possible to prepare this command so that spaces are replaced by a _ or โ€“

    Reply

Got Something to Say? Join the Discussion... Cancel reply

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Check your email for a magic link to get started.