If you are new to the Linux terminal, your first instinct when you need to change a file’s name is to search for a rename command. While some Linux distributions do include a complex batch-renaming tool by that name, the standard, universal, and most efficient way to rename a single file in Linux is actually by using the mv (move) command.
This sounds counterintuitive until you understand how Linux handles file paths. When you use the mv command, you are technically picking the file up, “moving” it back into the exact same folder it was already in, but assigning it a completely different name during the transit.
The Basic Syntax for Renaming
The syntax for the move command requires two pieces of information: the current name of the file, and the new name you want to give it.
If you have a file named old_report.txt and you want to rename it to final_report.txt, you would type:
mv old_report.txt final_report.txt
Press Enter. The terminal will simply return a blank prompt, but the file has been instantly renamed.
The Danger of Overwriting Files
The mv command is incredibly fast, but it is also merciless. If you type mv old_report.txt final_report.txt, and a file named final_report.txt already exists in that folder, Linux will instantly destroy the existing file and overwrite it with your renamed file, completely without warning.
To protect yourself from accidental data loss, you should always use the interactive flag (-i) when renaming files.
mv -i old_report.txt final_report.txt
With the -i flag active, if Linux detects that final_report.txt already exists, it will pause the command and ask: overwrite final_report.txt? (y/n). You can then safely press n (no) to cancel the rename operation, saving your data from destruction.