How to Rename a File in Linux
In this Linux tutorial, you'll learn how to rename a file using the terminal.
Alright, let’s get started!
The Command You Need: mv
In Linux, there's no separate rename command for everyday use. Instead, we use the mv command — short for "move" — to rename files. It's like telling Linux: “Hey, move this file from its old name to a new name... in the same place.”
Basic Syntax
mv old_filename new_filename
Let’s see it in action.
Step-by-Step Example
Suppose we have a file called notes.txt and we want to rename it to project_notes.txt.
ls
notes.txt
mv notes.txt project_notes.txt
ls
project_notes.txt
Awesome! You've just renamed a file.
Renaming Inside a Directory
If the file is inside another folder, like documents/, you can still rename it using the path:
mv documents/oldname.txt documents/newname.txt
This keeps the file in the same folder but changes its name.
What If the File Doesn’t Exist?
If you try to rename a file that doesn't exist, you'll get an error like this:
mv hello.txt goodbye.txt
mv: cannot stat 'hello.txt': No such file or directory
So always make sure the file you're trying to rename actually exists. Use ls to list files in the current directory if you're unsure.
Pro Tip: No Confirmation by Default
Linux won’t ask for confirmation if new_filename already exists — it will simply overwrite it. To avoid that, use the -i (interactive) option:
mv -i file1.txt file2.txt
mv: overwrite 'file2.txt'?
This gives you the chance to stop the action if needed.
Summary
- Use
mv old_name new_nameto rename files - You can rename files in any directory by including the path
- Use
-ito prevent accidental overwrites
That’s it! You’ve now learned how to rename a file in Linux like a pro. Keep experimenting and try renaming a few files in your system.
