The mv command renames files and folders in Linux

The simplest way to rename a file or folder in Linux is the mv command. Type mv oldname newname in the terminal, and the file is renamed. The mv command works the same way whether you are renaming a single document, a photo, or a folder containing multiple files.

The mv command also moves files between folders at the same time. If you type mv oldname /path/to/folder/newname, the file moves to that folder and gets renamed in one step. This is useful when you want to organize files into the right location and fix their names at the same time.

You need to be in the correct folder or use the full path to the file. If you are in your Documents folder and the file is on your Desktop, you either change to the Desktop folder first or type the full path: mv ~/Desktop/oldname ~/Documents/newname.

Key Takeaways

  • The mv command renames a file by typing mv oldname newname in the terminal.
  • You can rename and move a file to a different folder in one command by including the path: mv oldname /path/to/folder/newname.
  • File extensions matter — if you rename a .txt file to remove the extension, the system may no longer recognize it as a text file.
  • The mv command overwrites files with the same name without warning, so double-check the new name before you press Enter.
  • Renaming many files at once requires a loop or a batch rename tool like rename or mmv, not the basic mv command.

How to rename a file in your current folder

Open the terminal and navigate to the folder where your file is. Type pwd to see which folder you are in. Then type ls to list all files in that folder so you can see the exact spelling and extension of the file you want to rename.

Once you can see the file, type mv oldname newname and press Enter. Replace oldname with the exact name of the file, including the extension. Replace newname with what you want to call it. For example: mv report.txt quarterly_report.txt. The file is renamed when ready.

If the file name has spaces, put the name in quotes: mv "old file name.txt" "new file name.txt". Without quotes, the terminal treats each word as a separate argument and the command fails.

How to rename a file in a different folder

You do not have to navigate to the file's folder. Instead, type the full path to the file. For example: mv ~/Documents/oldname.txt ~/Documents/newname.txt. The tilde (~) represents your home folder, so this command works from any location in the terminal.

If you want to rename the file and move it to a different folder at the same time, change the destination path: mv ~/Documents/oldname.txt ~/Pictures/newname.txt. This moves the file from Documents to Pictures and renames it in one step.

Use ls ~/Documents/ to see the contents of a folder without navigating to it. This helps you verify the exact file name before you type the mv command.

How to rename a folder

The mv command works the same way for folders as it does for files. Type mv oldfoldername newfoldername to rename a folder in your current location. For example: mv old_project new_project renames the folder and everything inside it stays in place.

You can also rename a folder and move it at the same time: mv ~/old_project ~/Archive/new_project. This moves the folder to the Archive directory and renames it in one command.

The folder does not have to be empty. All files and subfolders inside are renamed along with the parent folder. If you have a folder called "2023_photos" with hundreds of images inside, renaming the folder does not touch the individual files — only the folder name changes.

Why file extensions matter when renaming

A file extension is the part after the dot: .txt, .pdf, .jpg, .mp3. Linux uses the extension to know what type of file it is and which program should open it. If you rename a file and remove or change the extension, the system may not recognize it anymore.

For example, if you rename document.txt to document, a text editor may no longer open it by default. If you rename photo.jpg to photo.png, the system thinks it is a PNG image, but the file is still actually a JPG — this can cause problems when you try to open it.

Keep the extension the same unless you are intentionally changing the file type. If you want to convert a JPG to a PNG, use an image conversion tool, not just a rename command.

How to rename multiple files at once

The basic mv command renames one file at a time. To rename many files with a pattern, use the rename command or a loop. The rename command uses regular expressions to change file names in bulk.

For example, to rename all files that start with "photo_" and end with ".jpg" to start with "image_" instead, type: rename 's/photo_/image_/' *.jpg. This changes photo_1.jpg to image_1.jpg, photo_2.jpg to image_2.jpg, and so on.

If the rename command is not installed, you can use a for loop: for f in *.txt; do mv "$f" "${f%.txt}.doc"; done. This renames all .txt files in the current folder to .doc. Always test a rename command on a few files first before running it on a large batch.

What happens if the new name already exists

If you try to rename a file to a name that already exists, the mv command overwrites the existing file without asking. The original file is deleted and replaced with the file you are renaming. This happens silently — there is no warning or confirmation.

To prevent accidental overwrites, use the -i flag: mv -i oldname newname. This makes the command ask for confirmation before overwriting. Type y to confirm or n to cancel.

Some systems have mv set to use -i by default, but not all. If you are working with important files, always use the -i flag to be safe.

Frequently Asked Questions

Can I undo a rename if I made a mistake?

Linux does not have a built-in undo for the mv command. Once a file is renamed, it stays renamed. If you renamed the wrong file, you have to rename it back manually. This is why it is important to check the file name carefully before you press Enter, especially when using batch rename commands.

What if the file name has special characters?

Special characters like spaces, asterisks, or dollar signs can confuse the terminal. Put the file name in single or double quotes: mv 'old$file.txt' 'new$file.txt'. Single quotes treat everything literally, while double quotes allow some special characters to work. When in doubt, use single quotes.

Can I rename a file that is currently open?

Yes, you can rename a file even if a program has it open. The file is renamed when ready, but the program that opened it may not notice the change. Some programs may have trouble saving changes after a rename. Close the file in the program first, then rename it, to avoid confusion.

How do I rename a file with no extension?

Files without extensions work the same way as files with extensions. Type mv filename newname. If you want to add an extension, type mv filename filename.txt. If you want to remove an extension, type mv filename.txt filename. The system does not require an extension, but programs may not recognize the file type without one.

What is the difference between mv and cp for renaming?

The mv command moves or renames a file — the original is gone. The cp command copies a file — the original stays and a new copy is made. To rename, always use mv. Using cp creates a duplicate file with the new name, which wastes storage space and leaves the old file behind.