The mv command renames files in Linux

To rename a file in Linux, use the mv command followed by the current filename and the new filename. Open a terminal, navigate to the folder where the file is, and type mv oldname.txt newname.txt, then press Enter. The file is renamed when ready — there is no confirmation step.

The mv command works the same way whether you are renaming a document, image, video, or any other file type. You do not need special permissions unless the file belongs to another user or sits in a protected system folder.

If the filename contains spaces, put the whole name in quotes: mv "old file name.txt" "new file name.txt". Without quotes, Linux treats each word as a separate item and the command fails.

Key Takeaways

  • The mv command is the standard way to rename files in Linux, and it works from any terminal window.
  • Type the current filename exactly as it appears, including the file extension, or the command will not find the file.
  • Filenames with spaces must be wrapped in quotes, or Linux will misread them as multiple separate names.
  • You can rename a file and move it to a different folder in one command by including the path in the new filename.

Navigate to the folder containing the file first

Before you can rename a file, you need to tell the terminal where to find it. Use the cd command to change to the folder. For example, if your file is in your Documents folder, type cd Documents and press Enter.

If you are not sure where you are, type pwd (print working directory) and press Enter — the terminal shows the full path of your current location. If you need to go up one level to the parent folder, type cd .. and press Enter.

Once you are in the correct folder, type ls and press Enter to see all files in that location. Find the exact filename you want to rename — pay attention to capitalization and the file extension, because Linux treats these as part of the name.

Type the mv command with the old and new filenames

The basic structure is always the same: mv [old filename] [new filename]. Replace [old filename] with the current name of the file, exactly as it appears when you ran ls. Replace [new filename] with what you want to call it.

For example, if you have a file called report.pdf and want to rename it to final_report.pdf, type mv report.pdf final_report.pdf and press Enter. The file is renamed when ready. Type ls again to confirm the new name appears in the folder.

The file extension (the part after the dot) is part of the filename. If you rename photo.jpg to vacation, the file loses its .jpg extension and may not open in image programs anymore. Keep the extension unless you have a specific reason to change it.

Handle filenames with spaces and special characters

If the filename contains spaces, the terminal needs to know the whole thing is one name. Wrap it in double quotes: mv "old file name.txt" "new file name.txt". Without the quotes, Linux reads each word as a separate item and the command fails with an error.

Special characters like asterisks (*), question marks (?), and dollar signs ($) can confuse the terminal because they have special meanings in Linux. If your filename contains these, put the whole name in single quotes instead: mv 'file$name.txt' 'newname.txt'. Single quotes tell Linux to treat everything inside as literal text.

Hyphens and underscores are safe and do not need quotes. Many people use underscores instead of spaces for this reason — my_file_name.txt is easier to type and less error-prone than "my file name.txt".

Rename and move a file in one command

The mv command can rename a file and move it to a different folder at the same time. Include the path to the destination folder in the new filename. For example, mv report.pdf ~/Documents/final_report.pdf renames report.pdf to final_report.pdf and moves it into the Documents folder in your home directory.

The tilde (~) is shorthand for your home directory — the main folder where your personal files live. If you want to move the file to a subfolder, include that in the path: mv report.pdf ~/Documents/Archive/final_report.pdf moves it into the Archive subfolder inside Documents and renames it at the same time.

If the destination folder does not exist, the command fails. Create the folder first using mkdir if you need to, or double-check the path is correct by navigating there with cd and running ls.

Undo a rename if you made a mistake

Linux does not have an undo button for the mv command. Once you rename a file, the old name is gone. However, if you renamed a file by accident and remember the old name, you can straightforward rename it back: mv newname.txt oldname.txt.

If you cannot remember the old name, look at the file's properties or check your terminal history. In most terminals, press the up arrow key to scroll through recent commands — you may see the original filename there. Some file managers also show recently renamed files in a history or undo menu.

To avoid mistakes, type the command carefully before pressing Enter. Take a moment to check that the old filename matches exactly what you see in ls, and that the new filename is what you actually want.

Rename multiple files at once

To rename several files that follow a pattern, use a wildcard. For example, if you have files named photo1.jpg, photo2.jpg, and photo3.jpg, and you want to rename them all to vacation1.jpg, vacation2.jpg, and vacation3.jpg, you can use a loop. Type for f in photo*.jpg; do mv "$f" "${f/photo/vacation}"; done and press Enter.

This command finds all files matching photo*.jpg, then replaces the word "photo" with "vacation" in each name. The syntax is more complex than a single rename, but it saves time when you have many files to change.

If you are not comfortable with loops, rename files one at a time using the basic mv command. There is no penalty for doing it slowly — it is safer than running a command that could rename the wrong files.

Frequently Asked Questions

What happens if I rename a file to a name that already exists?

The mv command overwrites the existing file without warning. The old file with that name is deleted and replaced with the file you are renaming. To prevent this, check what files are in the folder by running ls before you rename anything, or use a slightly different name if you are unsure.

Can I rename a file I do not own?

No, unless you have write permission on the file or you are the root user. If you try to rename a file owned by another user, you get a "Permission denied" error. Ask the file owner to rename it, or contact your system administrator if you need access.

Does renaming a file change its contents?

No. Renaming only changes the name displayed in the folder — the data inside the file stays exactly the same. The file still opens in the same programs and works the same way.

What if the filename is very long or hard to type?

Use tab completion to avoid typing the whole name. Type the first few letters of the filename, then press the Tab key — the terminal fills in the rest automatically. This works for both the old and new filenames and prevents typos.

Can I rename a file to have no extension?

Yes, but the file may not open in the correct program afterward. Most programs rely on the extension to know what type of file it is. If you remove the extension, you may have to right-click the file and choose "Open with" to pick a program manually.