How to Rename a Directory on Linux – How-To Geek

Linux laptop showing a bash indicator
fatmawati achmad zaenuri/Shutterstock.com

Renaming a directory in Linux is easy, and there are many ways to do it. From renaming a single directory to finding and renaming many, here’s how.

Your data is safe

Renaming directories is something we should all do from time to time

.

We could create a directory and misspell its name, and we want to correct that. Often, the purpose of a directory changes over time or over the life of a project, and you want to adjust the name to reflect its new use. You may have unzipped an archive file and created a directory tree with uppercase directory names, and you want them lowercase.

Whatever the reason, renaming a directory does nothing to the data it contains. You change the path to that data, but the files and directories within your renamed directory are not touched.

Do not rename system directories. Changing the path to system files and commands will have a detrimental effect on the functioning of your computer, to say the least. If you need to use sudo to rename a directory, unless you really know what you’re doing, chances are you shouldn’t rename it.

Using

the mv command

In the simplest cases, all we really need is the mv command. This is an integral part of every Linux distribution, so there’s nothing to install.

The mv command is over 50 years old at the time of writing. It comes from the dawn of Unix, when short, cryptic commands were all the rage, probably to reduce the number of characters that had to pass along slow serial lines from teletypes and dumb terminals to the actual computer.

It actually means “move”, and can be used to move files from one directory to another. If you move a file to the same location it is already in and rename it, you have renamed the file. And we can do the same with directories.

There are two subdirectories in this directory.

ls

To rename a directory we use the mv command. We need to provide the current directory name and the new name.

mv old-work archive-2 If the directory

you want to rename is not in the current directory, provide

the path and name of the directory. mv ~/htg/old-work ~/htg/archive-2 ls

Rename a directory in a different directory with mv by specifying the path on the command line

Using File Explorer

File browsers can rename directories. The keystroke in the GNOME Files application is F2. Highlighting a directory and tapping the F2 key opens the “Rename Folder” dialog box.

<img src="https://www.howtogeek.com/wp-content/uploads/2022/05/8-7.png" alt="

Using the fie browser to rename

a directory” /> Type the new name

and click the green “Rename” button

.

Provide the new directory name in File Explorer

The directory is renamed.

The directory renamed in the file explorer window

It’s as simple as that

. The rename command

If

your needs are more complicated than directly renaming a directory, you may need to use the rename command. This allows you to use Perl expressions to rename files and directories. It provides a much more powerful and flexible way to rename directories.

Let’s talk about the Perl-based rename command. There is another older command called rename that is part of the main Linux utilities. You’ll probably need to install the Perl rename command we want to use.

To avoid name conflicts with the existing rename command, the Perl rename command is called prename in Fedora and perl-rename in Manjaro. In Ubuntu, the rename and prename commands are symbolic links that resolve to a binary called file-rename.

So, in

Manjaro the command you will need to use perl-rename, and in Fedora it is prename. In Ubuntu, you can use rename or prename.

To install Perl rename,

in Ubuntu you need to type

: sudo apt install rename In Fedora, the command is

:

sudo dnf install prename

In Manjaro the package is called

perl-rename. sudo pacman -Sy perl-rename

Be sure to use the appropriate command for your distribution if you want to work with the samples

.

Getting started with

rename

The rename command takes regular expressions from Perl and applies them to a file

or directory, or group of files or directories.

In our directory, we have a collection of other directories.

ls

Their names are a mixture of lowercase, uppercase and lowercase. We can convert them all to lowercase with a suitable expression.

rename ‘y/A-Z/a-z/’ * ls

Directory conversion to lowercase names

All directories are now lowercase, whether they were completely uppercase previously, or contained the odd uppercase letter

.

All magic is contained in expression. The expression is enclosed in single quotation marks “‘”. This is what the whole command means.

y: This means searching for any character in the first character range and replacing it with the corresponding character in the second character range. /A-Z/

  • a-z/: The first range is all letters from “A” to “Z”, and the second range is all characters from “a” to “z
  • “.

  • *: The asterisk wildcard means apply this to all directories.

In other words, the command says “for all directories, change the uppercase letters to the equivalent lowercase letter.”

Obviously, you can rename a single directory with renaming, although it smacks of exaggeration. You will be faster using mv.

rename ‘s/gamma/epsilon-2/’ * ls

renaming a single directory with rename

The “s” in this expression stands for substitute. Check each directory to see if its name is “gamma”. If so, replace it with “epsilon-2”. However, note that this would also have coincided with a directory called “gamma-zeta”, for example, renaming it to “epsilon-2-zeta”.

We can avoid this by adding the beginning of the string “^” and the end of the string “$” metacharacters to the first clause of the expression.

ls rename ‘s/^gamma$/epsilon-2/’ * ls

Limit a rename action to fully qualified directory names only

This leaves the “epsilon-2” directory intact

.

Using renaming with

other commands

We can use other commands to locate the directories where we want the rename to work. If we have a set of nested directories and we want to rename any that end in “-old” to end in “-archive”, we can achieve this by using find and xargs.

We need to use xargs because rename does not accept pipelined input. The xargs command overcomes that problem by accepting pipelined input and adding another command to the command line as a command-line parameter.

Our command looks like this:

find . -depth -type d -name “*-old” | xargs -r rename “s/old$/archive/” .

  • : We tell find to start searching the current directory. This could be any path, of course.
  • -depth: Use a depth search first. This means that the contents of the deeper nested subdirectories are processed before the higher ones.
  • -type d: Searches directories, not files.
  • -name “*-old”: The search track. We are looking for directories with names ending in “-old”.
  • |: We are piping the output of find to the xargs command.
  • xargs -r: -r (no run if empty) means not to run the command if there are no matching directories.
  • rename “s/old$/archive/”: The rename command to run.

Our directory tree

looks like this

before the command. The directory tree before our rename command

We execute our command:

And we can see that all matching directories, including nested ones, have been renamed.

The directory tree after the rename command

Horses for courses

Renaming a directory needs nothing more than mv. If you prefer GUI applications, you can use your file browser. If you have many directories to rename, and especially if they are scattered across a directory tree, you will need the flexibility to rename.

RELATED: How to Manage Files from Linux Terminal: 11 Commands You Need to Know