Rename Files and Directories to Uppercase on Linux

Rename Files and Directories to Uppercase on Linux

Prateek Dagur Linux

Table of Contents

In previous post, we showed you how to Rename Files and Directories from Uppercase to Lowercase on Linux. In this guide, you'll see how to rename from lowercase to uppercase.

There are several different ways to handle this on Linux. One method will use native mv and tr utilities, 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 uppercase 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 lowercase to uppercase. 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 '[:lower:]' '[:upper:]' 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 '[:lower:]','[:upper:]'`
  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 uppercase and store the result in the variable newname.
  3. Rename each file with the new uppercase name stored in newname.

Using rename Command

The rename is much simpler command to change those names.

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 lowercase letters to uppercase.

mmv '*' '#u1'

Recursively from Lowercase to Uppercase

Recursively Change all Directories and Filenames

find . -depth | xargs -n 1 rename 's/(.*)\/([^\/]*)/$1\/\U$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\/\U$2/': This is a Perl expression which will be used to modify the filenames. The \U modifier in Perl is used for uppercase 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\/\U$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\/\U$2/' {} \;

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

Conclusion

Bulk renaming files from lowercase to uppercase letters 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.

Comments