codedamn

How To Rename a Directory on Linux?

  • Arnab Sen's profile image
    Arnab Sen
    Author
How To Rename a Directory on Linux?

In Linux while creating a directory, we can use the mkdir command. But, what if we want to rename a directory? In this tutorial, we will learn how to rename a directory on Linux.

Most of the steps will remain the same, irrespective of which Linux distribution you are using. We can either use some commands on the terminal or use the GUI to rename a directory. Let’s see the GUI method first.

GUI (Graphical User Interface) Method

  1. Select the directory you want to rename.
  2. Right-click on the directory and select Rename.
  3. Enter the new name for the directory in the pop-up and press Enter.

Your directory will be renamed.

The dialog box for renaming a directory

Command Line Method

To use the command line method, open your terminal first (Ctrl+Alt+T).

Using the mv command

Even though the mv command is used to move files and directories, we can also use it to rename a directory. The syntax is as follows:

mv <old-name> <new-name>
Code language: Bash (bash)

Let’s rename the old-dir directory to new-dir using the mv command.

mv old-dir new-dir
Code language: Bash (bash)

It doesn’t matter if your directory contains some files or other directories. It will still be renamed and the files and directories inside it will remain intact.

Move command in the terminal

You can also specify the full path of the directory to rename it.

mv /home/username/old-dir /home/username/new-dir
Code language: Bash (bash)

Using the rename command

We also have a dedicated command to rename a directory. It is the rename command.

$ rename -h Usage: rename [options] <expression> <replacement> <file>... Rename files. Options: -v, --verbose explain what is being done -s, --symlink act on the target of symlinks -n, --no-act do not make any changes -a, --all replace all occurrences -l, --last replace only the last occurrence -o, --no-overwrite don't overwrite existing files -i, --interactive prompt before overwrite -h, --help display this help -V, --version display version For more details see rename(1).
Code language: Bash (bash)

In many Linux distributions, the rename command is not installed by default. So, you will have to install it first.

sudo apt install rename # For Ubuntu and Debian sudo yum install prename # For CentOS and Fedora sudo pacman -S rename # For Arch Linux
Code language: Bash (bash)

Using the rename commands you can rename multiple directories at once by specifying a pattern. Let’s say you have some directories that have space-separated names. And you want to replace the space with an underscore. You can do that using the rename command.

rename 's/ /_/g' *
Code language: Bash (bash)

If you are on Fedora (like me) the syntax will be slightly different.

rename " " "_" *
Code language: Bash (bash)

Using the -v flag, you can see the changes that will be made.

rename -v 's/ /_/g' *
Code language: Bash (bash)

You can also use the -n flag to see the changes that will be made without actually making them.

You can do other cool stuff with rename like, if you want to make all your directories uppercase, you can do that using the rename command.

$ rename 'y/a-z/A-Z/' * Folder_1 -> FOLDER_1 Folder_2 -> FOLDER_2 Folder_3 -> FOLDER_3 Folder_4 -> FOLDER_4
Code language: Bash (bash)

Conclusion

In this tutorial, we learned how to rename a directory on Linux. We saw both the GUI and command line methods. We also saw how to use the rename command to rename multiple directories at once.

I hope you enjoyed the article. Feel free to connect with me and follow me @ArnabSen1729.

Learn programming on codedamn

Codedamn is an interactive coding platform with tons of sweet programming courses that can help you land your first coding job. Here's how:

Programming is one of the most in-demand jobs today. Learning to program can change your future. All the best!