Renaming Directories on Linux: A Comprehensive Guide

Renaming Directories on Linux: A Comprehensive Guide

Learn how to effortlessly rename directories on Linux without risking data loss Discover various methods, including using the mv command, navigating through the file browser, and executing the rename command Master the art of renaming folders efficiently with our detailed guide

Some Noticeable Information

Renaming a directory in Linux doesn't harm the data inside it. It only changes the path to the data, leaving the files and directories intact.

The "mv" command is the most frequently used and straightforward way to rename directories in Linux. However, for more complex renaming tasks, the "rename" command with Perl expressions offers a versatile and robust alternative. Ensure that you install the suitable version according to your Linux distribution.

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.

Renaming a Folder Won't Harm Your Data

Occasionally, we find ourselves in the need to rename directories.

There are instances where we accidentally misspell a directory's name and would like to correct it. Furthermore, as projects progress, the purpose of a directory might change, leading to the desire of adjusting its name to better represent its new role. Additionally, when extracting an archive file, it is possible for the resulting directory tree to have uppercase directory names, which may prompt the wish to convert them to lowercase.

Renaming a directory does not affect the data stored inside it. It only modifies the path to access that data, while leaving the files and directories within untouched.

It is advised not to rename system directories, as it can significantly disrupt the functioning of your computer. Unless you are familiar with the process and understand its implications, it is recommended not to rename system directories, especially if it requires the use of sudo.

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, which dates back over 50 years, originated from the early days of Unix. During this time, concise and enigmatic commands were popular, likely to minimize the number of characters transmitted via slow serial lines from teletypes and dumb terminals to the computer.

Essentially an abbreviation for "move," the mv command enables the relocation of files between directories. By moving a file to its current location while assigning a new name, the file effectively undergoes renaming. This functionality also applies to directories.

There are two subdirectories in this directory.

ls

Renaming Directories on Linux: A Comprehensive Guide

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 Directories on Linux: A Comprehensive Guide

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

ls

Renaming Directories on Linux: A Comprehensive Guide

Using the File Browser

Renaming directories is possible using file browsers. In the GNOME Files application, you can press F2 to rename a directory. By selecting a directory and pressing the F2 key, the "Rename Folder" dialog will open.

Renaming Directories on Linux: A Comprehensive Guide

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

Renaming Directories on Linux: A Comprehensive Guide

The directory is renamed for you.

Renaming Directories on Linux: A Comprehensive Guide

It's as simple as that.

The rename Command

If you have more complex requirements for renaming a directory, the rename command can be used. This command enables the use of Perl expressions to rename files and directories, offering a much more robust and flexible solution for renaming directories.

We will discuss the Perl-based rename command, which is a newer version compared to the older Linux core utilities rename command. To use the Perl rename command, you may need to install it.

To prevent conflicts with the existing rename command, the Perl rename command is referred to as prename on Fedora, and perl-rename on Manjaro. On Ubuntu, both the rename and prename commands are symbolic links that point to a binary named 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

Renaming Directories on Linux: A Comprehensive Guide

On Fedora, the command is:

sudo dnf install prename

Renaming Directories on Linux: A Comprehensive Guide

On Manjaro the package is called perl-rename.

sudo pacman -Sy perl-rename

Renaming Directories on Linux: A Comprehensive Guide

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.

ls

Renaming Directories on Linux: A Comprehensive Guide

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/' *

ls

Renaming Directories on Linux: A Comprehensive Guide

All directories have been converted to lowercase, regardless of whether they were previously entirely uppercase or had occasional uppercase letters.

The expression within single quotes "'" contains all the magic. It represents the entirety of the command's purpose.

y: This command allows you to replace any character within the first range of characters with its corresponding character from the second range of characters.

/A-Z/a-z/: In this command, the first range includes all the letters from "A" to "Z", while the second range includes 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/' *

ls

Renaming Directories on Linux: A Comprehensive Guide

The "s" in this expression stands for substitute. It verifies each directory to determine if its name is "gamma". If it is, it will replace it with "epsilon-2." However, please note that this would also affect a directory named "gamma-zeta", for instance, resulting in it being renamed to "epsilon-2-zeta."

To avoid this, we can include the start of the string "^" and end of the string "$" metacharacters in the initial part of the expression.

ls

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

ls

Renaming Directories on Linux: A Comprehensive Guide

This leaves the directory "epsilon-2" untouched.

Using rename With Other Commands

To locate the directories we wish to rename, we can utilize alternative commands. If there is a series of nested directories and our intention is to rename those that conclude with "-old" to end with "-archive", the desired outcome can be achieved by employing find and xargs.

xargs becomes necessary due to rename's inability to accept piped input. However, the xargs command remedies this limitation by accepting the piped input and incorporating it as a command line parameter for another command.

Our command looks like this:

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

Start the search in the current directory, which can be any path.

-depth: Utilize a depth-first search algorithm, allowing for the processing of contents within nested subdirectories at deeper levels before those at higher levels.

-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.

Renaming Directories on Linux: A Comprehensive Guide

We run our command:

Renaming Directories on Linux: A Comprehensive Guide

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

Renaming Directories on Linux: A Comprehensive Guide

Horses for Courses

To rename a directory, simply use the "mv" command. If you prefer a graphical user interface, you can utilize your file browser. However, if you have numerous directories to rename, particularly if they are spread out within a directory tree, the versatility of the "rename" command will be necessary.

Editor's P/S

As a Gen Z netizen, I find the article on renaming directories on Linux to be informative and straightforward. The guide provides clear and concise instructions on how to rename directories using various methods, including the mv command, file browser, and rename command.

The article emphasizes the importance of being cautious when renaming system directories, as it can disrupt the functioning of the computer. It also highlights the flexibility of the rename command, which allows for more complex renaming tasks using Perl expressions.

Overall, I appreciate the comprehensive nature of the guide and believe it can be a valuable resource for anyone needing to rename directories on Linux, regardless of their level of expertise.