Rename a File
Rename a File
The teacher has finished a report, but its name still says draft.txt. We can change that name without opening an editor or rewriting the report. On Linux, the usual command is mv, which handles both renaming and moving. The destination you supply tells it which job you intend.
For a rename in the same folder, give a new filename as the destination. Before doing that, check whether the new name already belongs to another report. A successful rename is not helpful if it unexpectedly replaces the wrong file.
What you’ll learn
- Rename a report with mv and inspect its new name.
- Use an overwrite prompt when the destination may already exist.
The report keeps its contents under a new name
Before
The draft is stored under its original name.
draft.txt: DraftRename
mv changes the directory entry when both names are on the same filesystem.
draft.txt → report.txtAfter
Read the new name and confirm the original text remains.
report.txt: Draft
Rename a draft report and inspect its contents
Prepare the example
Use a fresh practice directory. The first line below creates a draft containing one word, so you can see whether its content changes during the rename. Make sure there is no existing report.txt in this folder before running this straightforward version.
Bash · Linux terminal
printf 'Draft\n' > draft.txt
mv draft.txt report.txt
cat report.txtExpected result
DraftThe printf line writes Draft into the starting file. mv draft.txt report.txt then gives that entry the destination name. For this same-filesystem rename, the report's content remains the same; the old name is no longer the path you use to reach it.
The final cat report.txt confirms that the text survived. For a real folder where the destination might already exist, use mv -i to request an overwrite confirmation. That prompt is a chance to inspect the situation, not something to answer automatically. Remember that a destination which is a directory means moving into it instead of simply renaming the file.
Names, paths and the two operands of mv
Linux does not have a separate command named “rename” for the basic case. The mv command changes a file from one pathname to another, so it can rename a file without moving it to a different directory.
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.”
mv syntax for renaming a file
mv old_filename new_filename
Let’s see it in action.
Rename a sample file and verify the new name
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.
Missing sources and existing destination reports
If the source filename does not 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.
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.
Rename files safely with mv
- Use
mv old_name new_nameto rename files - You can rename files in any directory by including the path
- Use
-ito prevent accidental overwrites
Try it, then check the worked answer
Your task: Rename draft.txt to report.txt while protecting an existing report.
Use a fresh practice directory. The first line below creates a draft containing one word, so you can see whether its content changes during the rename. Make sure there is no existing report.txt in this folder before running this straightforward version.
Bash · complete example for this question
printf 'Draft\n' > draft.txt
mv draft.txt report.txt
cat report.txtExpected result
DraftWorked answer
mv -i draft.txt report.txt
Choose mv -i draft.txt report.txt. The -i option requests confirmation before an overwrite. cp would leave the source behind, while mv -f works against the protection you wanted. Check both source and destination names before answering any prompt.
Check your decision
A rename changes the name, not the report text
Renaming changes how you refer to a file; it does not edit the report text. Give mv a deliberate destination and protect an existing report when necessary. If the target is a directory, pause and read the command as a move into that directory.
Reaching the recap and completing the quizzes are separate milestones.