How to Rename a Directory on Linux


Linux laptop showing a bash prompt

Renaming a listing in Linux is simple, and there are many methods to go about it. From renaming a single listing to discovering and renaming many, right here’s how you can do it.

Your Information Is Protected

Renaming directories is one thing all of us have to do occasionally.

We’d create a listing and misspell its title, and we wish to put it proper. Typically, the aim of a listing modifications over time or by the lifetime of a venture, and also you wish to alter the title to replicate its new use. Maybe you’ve decompressed an archive file and it’s created a listing tree with the listing names in uppercase and also you’d like them in lowercase.

Regardless of the purpose. renaming a listing doesn’t do something to the information held inside it. It modifications the path to that knowledge, however the information and directories inside your renamed listing aren’t touched.

Don’t rename system directories. Altering the trail to system information and instructions goes to have a detrimental impact on the operating of your laptop, to say the least. If you have to use sudo to rename a listing—until you actually know what you’re doing—the possibilities are you shouldn’t be renaming it.

Utilizing the mv Command

In probably the most simple circumstances, all we actually want is the mv command. That is an integral a part of each Linux distribution, so there may be nothing to put in.

The mv command is over 50 years outdated on the time of writing. It hails from the daybreak of Unix, when brief and cryptic instructions have been in vogue, most likely to cut back the variety of characters that needed to cross alongside gradual serial strains from teletypes and dumb terminals to the precise laptop.

It really stands for “transfer”, and it may be used to maneuver information from listing to listing. If you happen to transfer a file to the identical location that it’s already in and provides it a brand new title, you’ve renamed the file. And we are able to do the identical with directories.

There are two subdirectories on this listing.


Listing two subdirectories

To rename a listing we use the mv command. We have to present the present title of the listing and the brand new title.

mv old-work archive-2

Renaming a directory with mv

If the listing you wish to rename isn’t in your present listing, present the trail in addition to the listing title.

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

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

Utilizing the File Browser

File browsers are capable of rename directories. The keystroke within the GNOME Information utility is F2. Highlighting a listing and tapping the F2 key opens the “Rename Folder” dialog.

Using the fie browser to rename a directory

Kind within the new title, and click on the inexperienced “Rename” button.

Providing the new directory name in the file browser

The listing is renamed for you.

The renamed directory in the file browser window

It’s so simple as that.

The rename Command

In case your wants are extra difficult than the simple renaming of a listing you may want to make use of the rename command. This lets you use Perl expressions to rename information and directories. It gives an altogether extra highly effective and versatile technique to rename directories.

We’re going to be speaking in regards to the Perl-based rename command. There’s one other, older command referred to as rename which is a part of the Linux core utilities. You’ll most likely want to put in the Perl rename command we wish to use.

To keep away from title clashes with the prevailing rename command, the Perl rename command is known as prename on Fedora, and perl-rename on Manjaro. On Ubuntu, the rename and prename instructions are each symbolic hyperlinks that resolve to a binary referred to as file-rename.

So, on Manjaro the command you’ll want to make use of perl-rename, and on Fedora it’s prename . On Ubuntu, you need to use rename or prename.

To put in Perl rename, on Ubuntu you have to sort:

sudo apt set up rename

Installing rename on Ubuntu

On Fedora, the command is:

sudo dnf set up prename

Installing prename on Fedora

On Manjaro the bundle is known as perl-rename.

sudo pacman -Sy perl-rename

Installing perl-rename on Manjaro

Be sure you use the suitable command on your distribution if you wish to work by the examples.

First Steps With rename

The rename command takes Perl common expressions and applies them to a file or listing, or group of information or directories.

In our listing, we’ve a group of different directories.


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

Their names are a combination of lowercase, uppercase, and combined case. We will convert all of them to lowercase with an appropriate expression.

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

Converting directories to lowercase names

All of the directories are actually in lowercase, whether or not they have been wholly uppercase beforehand, or contained the odd uppercase letter.

All of the magic is contained within the expression. The expression is wrapped in single quotes “'“. That is what the complete command means.

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

In different phrases, the command reads as “for all directories, swap any uppercase letters for the equal lowercase letter.”

Clearly, you’ll be able to rename a single listing with rename, though it does smack of overkill. You’ll be faster utilizing mv.

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

renaming a single directory with rename

The “s” on this expression means substitute. It checks every listing to see if its title is “gamma”. Whether it is, it replaces it with “epsilon-2.” Bear in mind although, that this might even have matched a listing referred to as “gamma-zeta”, for instance, renaming it to “epsilon-2-zeta.”

We will keep away from this by including the beginning of string “^” and finish of string “$” metacharacters to the primary clause of the expression.

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

Limiting a renaming action to entire directory names only

This leaves the listing “epsilon-2” untouched.

Utilizing rename With Different Instructions

We will use different instructions to find the directories we wish rename to work on. If we’ve a set of nested directories and we wish to rename any that finish in “-old” so that they finish in “-archive”, we are able to obtain that through the use of discover and xargs.

We have to use xargs as a result of rename doesn’t settle for piped enter. The xargs command overcomes that downside by accepting the piped enter and including to the command line of one other command as a command line parameter.

Our command appears to be like like this:

discover . -depth -type d -name "*-old" | xargs -r rename "s/outdated$/archive/"
  • .: We inform discover to start out looking out within the present listing. This could possibly be any path, after all.
  • -depth: Use a depth-first search. This implies the contents of deeper nested subdirectories are processed earlier than increased ones.
  • -type d: Seek for directories, not information.
  • -name “*-old”: The search clue. We’re on the lookout for directories with names ending in “-old.”
  • |: We’re piping the output from discover into the xargs command.
  • xargs -r: The -r (no run if empty) means don’t run the command if there aren’t any matching directories.
  • rename “s/outdated$/archive/”: The rename command to be run.

Our listing tree appears to be like like this earlier than the command.

The directory tree before our renaming command

We run our command:

Our renaming command using find, xargs, and rename

And we are able to see that the entire matching directories together with the nested ones have been renamed.

The directory tree after the renaming command

Horses for Programs

Renaming a listing doesn’t want something greater than mv. If you happen to favor GUI functions you need to use your file browser. If you happen to’ve acquired a whole lot of directories to rename, and particularly in the event that they’re scattered all through a listing tree, you’re going to wish the flexibleness of rename.

RELATED: Learn how to Handle Information from the Linux Terminal: 11 Instructions You Must Know

Leave a Reply

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