How to Rename a File in Linux: Simple mv Command Guide
To rename a file in Linux, use the
mv command followed by the current filename and the new filename, like mv oldname.txt newname.txt. This moves the file to the new name, effectively renaming it.Syntax
The basic syntax to rename a file in Linux is:
mv: The command to move or rename files.source: The current name of the file you want to rename.destination: The new name you want to give the file.
bash
mv source_file_name destination_file_name
Example
This example renames a file named report.txt to summary.txt. It shows how the mv command changes the file name without creating a copy.
bash
touch report.txt ls mv report.txt summary.txt ls
Output
report.txt
summary.txt
Common Pitfalls
Common mistakes when renaming files include:
- Using the wrong filename or path, causing errors or renaming the wrong file.
- Not having write permission in the directory, which prevents renaming.
- Overwriting an existing file without warning if the new name already exists.
Always check filenames and permissions before renaming.
bash
mv oldname.txt newname.txt # Correct usage mv oldname.txt existingfile.txt # Overwrites existingfile.txt without prompt
Quick Reference
| Command | Description |
|---|---|
| mv oldname.txt newname.txt | Rename file from oldname.txt to newname.txt |
| mv file.txt /path/to/newname.txt | Rename and move file to a different directory |
| mv -i oldname.txt newname.txt | Rename with prompt if newname.txt exists |
| mv -n oldname.txt newname.txt | Rename without overwriting existing files |
Key Takeaways
Use the mv command with current and new filenames to rename files in Linux.
Check file permissions and paths to avoid errors when renaming.
Be careful not to overwrite existing files unless intended.
Use options like -i for interactive renaming to prevent accidental overwrites.