How to Rename a Directory on Linux

Linux laptop showing a bash prompt
fatmawati achmad zaenuri/

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

Your Data Is Safe

Renaming directories is something we all need to do from time to time.

We might create a directory and misspell its name, and we want to put it right. Often, the purpose of a directory changes over time or through the life of a project, and you want to adjust the name to reflect its new use. Perhaps you’ve decompressed an archive file and it’s created a directory tree with the directory names in uppercase and you’d like them in lowercase.

Whatever the reason. renaming a directory doesn’t do anything to the data held inside it. It changes the path to that data, but the files and directories inside your renamed directory aren’t touched.

Don’t rename system directories. Changing the path to system files and commands is going to have a detrimental effect on the running 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—the chances are you shouldn’t be renaming it.

Using the mv Command

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

The mv command is over 50 years old at the time of writing. It hails from the dawn of Unix, when short and cryptic commands were in vogue, 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 stands for “move”, and it can be used to move files from directory to directory. If you move a file to the same location that it is already in and give it a new name, you’ve renamed the file. And we can do the same with directories.

There are two subdirectories in this directory.


Listing two subdirectories

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

mv old-work archive-2

Renaming a directory with mv

If the directory you want to rename is not in your current directory, provide the path as well as the directory name.

mv ~/htg/old-work ~/htg/archive-2

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

Using the File Browser

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

Using the fie browser to rename a directory

Type in the new name, and click the green “Rename” button.

Providing the new directory name in the file browser

The directory is renamed for you.

The renamed directory in the file browser window

It’s as simple as that.

The rename Command

If your needs are more complicated than the straightforward renaming of a directory you might need to use the rename command. This allows you to use Perl expressions to rename files and directories. It provides an altogether more powerful and flexible way to rename directories.

We’re going to be talking about the Perl-based rename command. There is another, older command called rename which is part of the Linux core utilities. You’ll probably need to install the Perl rename command we want to use.

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

So, on Manjaro the command you’ll need to use perl-rename, and on Fedora it is prename . On Ubuntu, you can use rename or prename.

To install Perl rename, on Ubuntu you need to type:

sudo apt install rename

Installing rename on Ubuntu

On Fedora, the command is:

sudo dnf install prename

Installing prename on Fedora

On Manjaro the package is called perl-rename.

sudo pacman -Sy perl-rename

Installing perl-rename on Manjaro

Make sure you use the appropriate command for your distribution if you want to work through the examples.

First Steps With rename

The rename command takes Perl regular expressions and applies them to a file or directory, or group of files or directories.

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


A collection of directories in a mixture of uppercase, lowercase, and mixed case

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

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

Converting directories to lowercase names

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

All the magic is contained in the expression. The expression is wrapped in single quotes “'“. This is what the entire command means.

  • y: This means search for any character in the first range of characters, and substitute it for the corresponding character from the second range of characters.
  • /A-Z/a-z/: The first range is all the letters from “A” to “Z”, and the second range is all the characters from “a” to “z.”
  • *: The asterisk wildcard means apply this to all directories.

In other words, the command reads as “for all directories, swap any uppercase letters for the equivalent lowercase letter.”

Obviously, you can rename a single directory with rename, although it does smack of overkill. You’ll be quicker using mv.

rename 's/gamma/epsilon-2/' *

renaming a single directory with rename

The “s” in this expression means substitute. It checks each directory to see if its name is “gamma”. If it is, it replaces it with “epsilon-2.” Be aware though, that this would also have matched a directory called “gamma-zeta”, for example, renaming it to “epsilon-2-zeta.”

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

rename 's/^gamma$/epsilon-2/' *

Limiting a renaming action to entire directory names only

This leaves the directory “epsilon-2” untouched.

Using rename With Other Commands

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

We need to use xargs because rename doesn’t accept piped input. The xargs command overcomes that problem by accepting the piped input and adding to the command line of another command 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 in the current directory. This could be any path, of course.
  • -depth: Use a depth-first search. This means the contents of deeper nested subdirectories are processed before higher ones.
  • -type d: Search for directories, not files.
  • -name “*-old”: The search clue. We’re looking for directories with names ending in “-old.”
  • |: We’re piping the output from find into the xargs command.
  • xargs -r: The -r (no run if empty) means don’t run the command if there are no matching directories.
  • rename “s/old$/archive/”: The rename command to be run.

Our directory tree looks like this before the command.

The directory tree before our renaming command

We run our command:

Our renaming command using find, xargs, and rename

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

The directory tree after the renaming command

Horses for Courses

Renaming a directory doesn’t need anything more than mv. If you prefer GUI applications you can use your file browser. If you’ve got a lot of directories to rename, and especially if they’re scattered throughout a directory tree, you’re going to need the flexibility of rename.

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

Source link

Leave a Reply

Your email address will not be published.