The mv command is the standard way to rename files in Linux

To rename a file in Linux, you use the mv command, which stands for "move". The command works the same way whether you're renaming a file or moving it to a different folder — you tell Linux the current filename and the new filename you want, and it handles the rest. The basic structure is always: mv oldname newname

Open your terminal and navigate to the folder where your file lives. If you're not sure which folder you're in, type pwd (print working directory) and press Enter — it will show you your current location. Once you're in the right folder, type the mv command with your actual filenames and press Enter.

Linux is case-sensitive, which means document.txt and Document.txt are two different files. When you type the old filename, match the capitalization exactly or Linux won't find it and will show you an error message saying the file doesn't exist.

Key Takeaways

  • The mv command renames files by typing mv oldname newname in the terminal, then pressing Enter.
  • You must be in the same folder as the file, or include the full path to the file's location before the filename.
  • Linux treats uppercase and lowercase letters as different, so match the original filename's capitalization exactly.
  • The mv command overwrites files without warning if a file with the new name already exists in that folder.
  • You can rename multiple files at once using wildcards like *.txt to match groups of files with the same extension.

Renaming a file in your current folder

If the file you want to rename is in the folder you're already working in, the command is straightforward. Type mv report.txt report_final.txt and press Enter. The file named report.txt becomes report_final.txt. The terminal won't print a success message — it will just return to the command prompt, which means the rename worked.

To verify the rename actually happened, type ls (list) and press Enter. You'll see all the files in your current folder, and the old filename should be gone and replaced with the new one. If you still see the old filename, the rename didn't work — usually because you mistyped the original filename or the file isn't in the folder you thought it was.

Renaming a file in a different folder

If the file is in a different folder, you need to tell Linux where to find it. You can either navigate to that folder first using cd (change directory), or you can include the path in the command itself. For example, if you have a file called photo.jpg in a folder called Pictures inside your home folder, you can type mv ~/Pictures/photo.jpg ~/Pictures/vacation.jpg and press Enter.

The tilde symbol ~ is a shortcut for your home folder. If you use it, you don't have to type out the full path like /home/username/Pictures. The easier approach for most people is to navigate to the folder first: type cd Pictures, press Enter, then use the simple mv command with just the filenames.

Renaming multiple files with wildcards

If you have several files you want to rename in a similar way, you can use a wildcard pattern to match them all at once. A wildcard is a symbol that stands in for any characters. The asterisk * matches any number of characters, and the question mark ? matches exactly one character.

For example, if you have files named photo1.jpg, photo2.jpg, and photo3.jpg, and you want to change the extension from .jpg to .png, you can't do it with a single mv command — mv doesn't work that way with wildcards. Instead, you'll need to use a loop or a tool like rename or mmv (multiple move). The rename command is often installed by default and works like this: rename 's/\.jpg$/.png/' *.jpg — this changes all files ending in .jpg to end in .png instead.

If the rename command isn't available on your system, you can rename files one at a time, or use a bash loop: for f in *.jpg; do mv "$f" "${f%.jpg}.png"; done. This is more complex, but it works on any Linux system. Type it exactly as shown, and it will rename every .jpg file in your current folder to .png.

What happens if a file with the new name already exists

If you try to rename a file to a name that already exists in the same folder, the mv command will overwrite the existing file without asking for permission. This means the old file with that name will be deleted and replaced with the file you're renaming. There's no undo button — the original file is gone.

To protect yourself, you can use the -i flag, which stands for "interactive". Type mv -i oldname newname and press Enter. If a file with the new name already exists, Linux will ask you "overwrite newname?" — type y for yes or n for no. This gives you a chance to stop before you accidentally delete something important.

Common mistakes when renaming files

The most common mistake is mistyping the original filename. If you type mv report.txt report_final.txt but the actual file is named Report.txt (with a capital R), Linux will tell you "cannot stat 'report.txt': No such file or directory". Check the exact spelling and capitalization by typing ls first to see the real filename.

Another mistake is forgetting to include the file extension. If you type mv document.txt document, the file will lose its .txt extension and become a file with no extension. Linux doesn't require file extensions the way Windows does, but programs won't know it's a text file anymore. Always include the extension in the new name unless you specifically want to remove it.

A third mistake is trying to rename a file you don't have permission to change. If you get a "Permission denied" error, the file is owned by another user or protected. You may need to use sudo (which runs the command with administrator privileges), but be careful — sudo mv can cause problems if you're not sure what you're doing. Ask your system administrator for help if you're not sure whether you should have access to the file.

Using the rename command for pattern-based renaming

The rename command is more powerful than mv for changing multiple files at once, because it can find and replace patterns in filenames. It uses regular expressions, which are a way of describing text patterns. For example, rename 's/old/new/' *.txt will change the word "old" to "new" in every filename ending in .txt.

The pattern between the first two slashes is what to search for, and the pattern between the second and third slash is what to replace it with. So rename 's/report/summary/' *.txt will rename report_january.txt to summary_january.txt, and report_february.txt to summary_february.txt. The rename command won't overwrite existing files by default — it will skip any file that would create a duplicate name, which is safer than mv.

Frequently Asked Questions

Can I rename a file to a name with spaces in it?

Yes, but you need to put the new filename in quotes so Linux knows the whole thing is one filename. Type mv oldname "new name with spaces.txt" and press Enter. Without the quotes, Linux will think each word is a separate thing and the command will fail.

What if I accidentally renamed a file to the wrong name?

Just rename it again with the mv command. There's no "undo" in the terminal, but you can fix it by running mv a second time with the correct name. If you deleted the file instead of renaming it, you may be able to recover it with a tool like testdisk or photorec, but recovery isn't may provide.

Can I rename a folder the same way I rename a file?

Yes, the mv command works exactly the same for folders. Type mv oldfoldername newfoldername and press Enter. The folder and everything inside it will be renamed.

How do I rename a file that starts with a dash or special character?

Files starting with a dash can confuse the mv command because it thinks the dash is a flag. Use mv -- -oldname newname (with two dashes before the filename) or mv ./-oldname newname (with a dot and slash before the filename). For other special characters, put the filename in quotes: mv "file@name.txt" "newname.txt"

Does renaming a file change when it was created or modified?

No, renaming a file with mv doesn't change the modification date or any other file metadata. The file keeps the same creation date, modification date, and permissions it had before. Only the filename itself changes.