How to Rename All Files to Lowercase or Uppercase in Linux

Sometimes you may need to rename files to lowercase or uppercase in Linux. In this article, we will learn how to do this. There are several ways to rename files & folders. We will look at one of them using find, rename and xargs function. It is simple and easy to use.


How to Rename All Files to Lowercase or Uppercase in Linux

We will look at simple way to rename all files to lowercase in Linux. Let us say you have the following folder /data

file1.txt
/Files/File2.txt
FILE3.txt


Rename Files to Lowercase using find, rename and xargs

We will rename function for renaming files as rename function allows you to rename multiple files & folders at once. It also has shortcuts to directly rename files & folders as lowercase or uppercase. First, we will use find function to get a list of all files & subfolders in our target folder.

$ find -depth /data
file1.txt
FILE3.txt
/Files/File2.txt
/Files
data

We use -depth option to get an exhaustive list of all files & folders in our target location. Also, it lists each folder & subfolder’s contents before it lists the folder or subfolder itself.

We pipe the above output to xargs command which will construct separate rename command for each file and folder in our target folder.

$ find /data -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
file1.txt renamed as file1.txt
FILE3.txt renames as file3.txt
/Files/File2.txt renamed as /Files/file2.txt
/Files renamed as /files

As you can see above, it renames the files in each folder before renaming the folder itself. In the above command, -n1 is used to instruct xargs to use at most 1 argument per line of input. We also use $1 to indicate existing filename and L$2 to specify to rename existing filename to lowercase.

If you only want to rename folders and not files, you can add -type d option in find command.

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

If you only want to rename files and not folders use -type f option in find command.

$ find /data -depth -type f | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
file1.txt renamed as file1.txt
FILE3.txt renames as file3.txt
/Files/File2.txt renamed as /Files/file2.txt


Rename to Uppercase using find, rename & xargs

Similarly, you can rename files & folders to uppercase, by simply replacing L$2 with U$2 (L for lowercase and U for uppercase) in the above commands.

$ find /data -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' {} \;
file1.txt renamed as FILE1.txt
FILE3.txt renames as FILE3.txt
/Files/File2.txt renamed as /Files/FILE2.txt
/Files renamed as /FILES

If you only want to rename folders and not files, you can add -type d option to find command.

$ find /data -depth -type d | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' {} \;
/Files renamed as /FILES

If you only want to rename files and not folders use -type f option in find command.

$ find /data -depth -type f | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' {} \;
file1.txt renamed as FILE1.txt
FILE3.txt renames as FILE3.txt
/Files/File2.txt renamed as /Files/FILE2.txt

In this article, we have learnt how to rename to lowercase or uppercase in Linux.

Also read:

How to Find Index of Item in List in Python
How to Redirect Using JavaScript
How to Show All Users in MySQL
How to Upgrade All Python packages with Pip
How to Create Python Function With Optional Arguments

Leave a Reply

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