The quickest way to rename a directory in Linux

The mv command renames directories in Linux. Type mv old-name new-name in the terminal, and the folder changes names when ready. The command works the same way whether you are renaming a folder in your home directory or deep inside a project folder — you just need to tell Linux where the folder is and what to call it instead.

If the directory is in your current location, the command takes one line. If it is somewhere else on your system, you either navigate to its parent folder first or include the full path. Linux does not ask for confirmation, so the rename happens as soon you press Enter.

Key Takeaways

  • The mv command renames directories: type mv old-name new-name and press Enter.
  • You must be in the directory's parent folder or include the full path for the command to work.
  • Linux does not confirm the rename, so double-check the old name and new name before pressing Enter.
  • Renaming a directory does not affect the files inside it — they stay exactly as they are.
  • If a directory with the new name already exists, mv will move your directory into it instead of renaming.

Renaming a directory in your current folder

If you are already inside the parent directory, the command is simplest. Open your terminal and type:

mv old-folder-name new-folder-name

Replace old-folder-name with the actual name of the folder you want to rename, and new-folder-name with what you want to call it. Press Enter, and the rename is done. You can verify it worked by typing ls to list the contents of your current directory — you should see the new name in the list.

For example, if you have a folder called project-files and want to rename it to archived-project, you would type:

mv project-files archived-project

Renaming a directory when you are not in its parent folder

If the directory you want to rename is somewhere else on your system, you have two options: navigate to its parent folder first, or include the full path in the command.

To navigate first, use cd to move to the parent directory, then use mv as normal. For example, if your folder is at /home/username/Documents/old-name, type:

cd /home/username/Documents

Then:

mv old-name new-name

Alternatively, include the full path in a single command. Type:

mv /home/username/Documents/old-name /home/username/Documents/new-name

Both approaches work. The first is easier to read; the second is faster if you do not plan to work in that folder again.

What happens to files inside the renamed directory

Renaming a directory does not touch the files inside it. All the files keep their names, their contents, and their locations. Shortcuts or links that pointed to the old directory path will break, because the path no longer exists — but the actual files are unchanged.

If you have scripts or other programs that reference the old directory path, you will need to update those references. For example, if a backup script was set to back up /home/username/old-folder, it will fail after you rename the folder. Update the script to point to the new path instead.

Avoiding mistakes when renaming

Linux does not ask "Are you sure?" before renaming. The command executes when ready, so check your typing before you press Enter. Common mistakes include misspelling the old name (which causes an error saying the directory does not exist) or accidentally creating a new directory instead of renaming.

If you are not certain about the directory name, use ls first to see exactly what it is called. Type ls and look at the output, then copy the exact name into your mv command. This takes an extra second but prevents typos.

If you accidentally rename a directory to the wrong name, you can rename it again using the same command. There is no undo in the terminal, but because you are just changing a name (not deleting anything), you can always rename it back.

When mv moves instead of renames

If a directory with the new name already exists, mv will move your directory into it instead of renaming it. For example, if you type mv folder1 folder2 and folder2 already exists, Linux will place folder1 inside folder2 rather than renaming folder1.

To prevent this, check that the new name does not already exist before you run the command. Use ls to list all directories in the current location and confirm the new name is not in the list. If it is, choose a different new name or delete the existing directory first (if you no longer need it).

Renaming multiple directories at once

If you need to rename many directories, you can use a loop in the terminal to rename them all with a pattern. For example, if you have folders named backup-2024-01, backup-2024-02, and backup-2024-03, and you want to change them all to archive-2024-01, archive-2024-02, and archive-2024-03, you can write a short script.

A straightforward approach uses the for loop. Type:

for dir in backup-2024-*; do mv "$dir" "${dir/backup/archive}"; done

This command finds all directories matching the pattern backup-2024-*, and for each one, renames it by replacing backup with archive. The quotes around $dir protect directory names that contain spaces.

If you are not comfortable with loops, renaming directories one at a time with mv is always safe and clear.

Frequently Asked Questions

What if the directory name has spaces in it?

Include the name in quotes. Type mv "old folder name" "new folder name". The quotes tell Linux to treat the entire text as a single name, even though it contains spaces. Without quotes, Linux would think you are trying to rename multiple items.

Can I rename a directory I do not own?

Only if you have write permission on the parent directory. If you do not, the command will fail with a "Permission denied" error. You may need to use sudo (which runs the command as the system administrator), but only do this if you are certain you should have access to that directory.

Does renaming a directory affect file paths in my programs?

Yes. Any program or script that references the old directory path will fail or look in the wrong place. You must update those references manually. Search your configuration files and scripts for the old path and replace it with the new one.

What if I rename a directory by accident?

Rename it again using the correct name. There is no undo in the terminal, but because you are only changing the name (not deleting anything), you can always rename it back to what it was. Just run mv again with the wrong name and the correct name.

Can I rename a directory to a name that already exists?

No. If the new name already exists as a directory, mv will move your directory into it instead. If the new name exists as a file, the command will fail. Always check that the new name is not already in use before you run the command.