How To Rename A Folder In Linux – Using Terminal Command Line

Renaming a folder in Linux can be done with a single command, but the syntax must be exact. If you are new to Linux, learning how to rename a folder in Linux is one of the first tasks you will need to master. The command line might seem intimidating, but renaming folders is straightforward once you understand the basic structure.

This guide covers everything from the simplest method to advanced techniques. You will learn the mv command, how to handle spaces, and even rename multiple folders at once. Let’s get started.

How To Rename A Folder In Linux Using The Mv Command

The most common way to rename a folder is with the mv command. The name mv stands for “move,” but it also works perfectly for renaming. In Linux, renaming a folder is essentially moving it to a new name in the same location.

The basic syntax is:

mv old-folder-name new-folder-name

Here is a simple example. Suppose you have a folder called project-backup and you want to rename it to project-archive. You would type:

mv project-backup project-archive

That is all it takes. The folder now has the new name. No extra flags or options are needed for basic renaming.

Important Notes About The Mv Command

  • The mv command does not ask for confirmation. It renames the folder immediately.
  • If a folder with the new name already exists, mv will move the source folder inside that existing folder. This can cause confusion.
  • You must have write permissions for the parent directory where the folder is located.

Always double-check the new name before pressing Enter. A typo can lead to unexpected results.

Renaming A Folder With Spaces In The Name

Folder names with spaces are common, but they require special handling in the terminal. If you try to run mv my folder new folder, Linux will think you are referring to multiple folders.

There are two ways to handle spaces:

  1. Use quotes: Enclose the folder name in single or double quotes. Example: mv "my folder" "new folder"
  2. Escape the space: Use a backslash before the space. Example: mv my\ folder new\ folder

Both methods work equally well. Quotes are often easier to read and type, especially if you have multiple spaces.

Example With Spaces

Imagine you have a folder named Summer Photos 2024 and you want to rename it to Summer Photos 2025. Using quotes, the command is:

mv "Summer Photos 2024" "Summer Photos 2025"

Using backslashes, it looks like:

mv Summer\ Photos\ 2024 Summer\ Photos\ 2025

Choose the method that feels most natural to you. Both are correct.

How To Rename A Folder In Linux Using The Gui

If you prefer a graphical interface, renaming a folder is even easier. Most Linux desktop environments like GNOME, KDE, or XFCE allow you to rename folders with a few clicks.

The steps are:

  1. Open your file manager (Files, Dolphin, Thunar, etc.).
  2. Navigate to the folder you want to rename.
  3. Right-click on the folder and select “Rename” from the context menu.
  4. Type the new name and press Enter.

This method is intuitive and requires no commands. However, it is slower if you need to rename many folders at once. For bulk operations, the terminal is more efficient.

Renaming Multiple Folders At Once

Sometimes you need to rename several folders at the same time. The mv command handles one folder at a time, but you can combine it with loops or other tools.

Using A For Loop In Bash

A simple for loop can rename multiple folders. For example, if you have folders named folder1, folder2, and folder3 and you want to add a prefix like old-, you can run:

for folder in folder1 folder2 folder3; do mv "$folder" "old-$folder"; done

This command renames each folder one by one. The $folder variable holds the current name, and old-$folder creates the new name.

Using The Rename Command

Some Linux distributions include the rename command, which uses Perl expressions to rename files and folders. It is more powerful but has a steeper learning curve.

To rename all folders that start with backup- to start with archive-, you would use:

rename 's/backup-/archive-/' backup-*/

Note that the rename command syntax varies between distributions. On Debian-based systems (like Ubuntu), it uses Perl. On Red Hat-based systems, it uses a different syntax. Check your system’s man page for details.

Renaming Folders With Special Characters

Special characters like !, @, #, $, and % can cause issues in the terminal. They often have special meanings in the shell.

To rename a folder with special characters, always use quotes. For example, to rename a folder called data$final to data-final, run:

mv "data$final" "data-final"

Without quotes, the shell might interpret $final as a variable, leading to errors. When in doubt, use quotes around folder names.

Common Mistakes When Renaming Folders

Even experienced users make mistakes. Here are the most common pitfalls:

  • Forgetting the path: If you are not in the same directory as the folder, you must include the full or relative path. Example: mv /home/user/docs/oldname /home/user/docs/newname.
  • Using a trailing slash: Do not add a trailing slash to the folder name. mv oldname/ newname/ is incorrect. Use mv oldname newname.
  • Overwriting existing folders: If a folder with the new name already exists, mv will move the source folder inside it. To avoid this, check for existing folders first.
  • Case sensitivity: Linux folder names are case-sensitive. Data and data are different folders. Be precise.

How To Rename A Folder In Linux With Full Path

You do not always have to be in the same directory as the folder. You can specify the full path to rename a folder anywhere on the system.

For example, to rename /var/www/html/old-site to /var/www/html/new-site, use:

mv /var/www/html/old-site /var/www/html/new-site

This is useful when you are working in a different directory and do not want to change your current location.

Renaming Folders With The Find Command

The find command can locate folders and rename them in one step. This is helpful for batch renaming based on criteria like age or name pattern.

For instance, to rename all folders in the current directory that contain the word “temp” to “backup-temp”, you could use:

find . -type d -name "*temp*" -exec mv {} backup-{} \;

This command finds all directories (-type d) with “temp” in the name and renames them by adding the prefix “backup-“. The {} placeholder represents the found folder name.

Using Tab Completion To Avoid Typos

Tab completion is a lifesaver in the terminal. When typing a folder name, press the Tab key to auto-complete it. This reduces the chance of typos.

For example, if you type mv pro and press Tab, the shell will complete the name to project-backup if that is the only match. If multiple matches exist, press Tab twice to see a list.

Always use tab completion for folder names, especially when they are long or contain spaces.

How To Rename A Folder In Linux Using Symlinks

Sometimes you might want to create a new name for a folder without actually renaming it. This is done with symbolic links (symlinks). A symlink acts as a shortcut to the original folder.

To create a symlink, use the ln -s command:

ln -s original-folder new-link-name

This creates a link called new-link-name that points to original-folder. The original folder remains unchanged. This is not a true rename, but it can serve a similar purpose in some workflows.

Renaming Folders In Scripts

If you need to rename folders programmatically, you can write a bash script. This is useful for automation or when dealing with hundreds of folders.

Here is a simple script that renames all folders in the current directory by adding a date prefix:

#!/bin/bash
for folder in */; do
  mv "$folder" "$(date +%Y%m%d)-$folder"
done

This script loops through every folder (*/) and prepends the current date. Save it as a file, make it executable with chmod +x script.sh, and run it.

Understanding Permissions For Renaming

To rename a folder, you need write permission on the parent directory. The folder itself does not need to be writable. This is a common point of confusion.

Check permissions with the ls -ld command. For example, ls -ld /home/user/docs shows the permissions of the docs directory. If you do not have write permission, you will see an error like “Permission denied.”

Use sudo if you need to rename folders owned by root or other users. Example: sudo mv /root/oldname /root/newname.

Frequently Asked Questions

Can I Rename A Folder Using The Cp Command?

No, the cp command copies folders, not renames them. To rename, you must use mv or a graphical file manager.

What Happens If I Rename A Folder That Is Currently In Use?

If a program has the folder open, renaming it might cause issues. It is best to close any programs using the folder before renaming it.

How Do I Rename A Folder With A Hyphen At The Beginning?

Folders starting with a hyphen can be mistaken for command options. Use mv -- -oldname newname or specify the full path to avoid confusion.

Is There A Way To Undo A Folder Rename?

There is no built-in undo. You must manually rename it back using the same mv command. Be careful before renaming.

Can I Rename A Folder Without Using The Terminal?

Yes, most Linux desktop environments allow renaming via the file manager’s right-click menu. This is the easiest method for beginners.

Final Tips For Renaming Folders In Linux

Renaming folders in Linux is a fundamental skill. Start with the mv command for single folders. Use quotes for spaces and special characters. For bulk operations, explore loops or the rename command.

Always double-check your command before pressing Enter. A small mistake can rename the wrong folder or overwrite existing data. Practice in a test directory until you feel confident.

With these techniques, you can handle any folder renaming task efficiently. The command line gives you speed and flexibility, while the GUI offers simplicity. Choose the method that fits your workflow.

Now you know how to rename a folder in Linux like a pro. Go ahead and try it out on your own system.