If you are new to the Linux command line, one of the most confusing hurdles is trying to figure out how to rename a file. Unlike Windows or macOS, where you simply right-click a file and select “Rename,” Ubuntu does not have a dedicated rename command for standard, everyday use.
Instead, Linux uses the “move” command (mv). The philosophy behind this is simple: renaming a file is essentially just moving it from its old name to its new name in the exact same directory.
The Basic ‘mv’ (Move) Command Syntax
The mv command requires two pieces of information: the original file, and the destination (the new name).
- Open your terminal or SSH into your server.
- Use the
cdcommand to navigate to the folder containing the file you want to rename. (e.g.,cd /home/user/documents). - Type the
mvcommand, followed by the current name, and then the new name, and press Enter:
mv old-document.txt new-document.txt
The terminal will not output a confirmation message. It will simply return to a blank prompt. If you type ls to list the contents of the folder, you will see that old-document.txt has vanished, and new-document.txt has replaced it.
How to Rename Files with Spaces in the Name
The terminal uses spaces to separate commands. If your filename contains a space (e.g., “my budget 2024.csv”), the mv command will get confused and think you are trying to move three completely different files.
To safely rename a file with spaces, you must wrap the filename in quotation marks:
mv "my budget 2024.csv" "final budget 2024.csv"
Alternatively, you can use a backslash (\) right before the space to “escape” it, though quotation marks are generally easier for beginners to read and type.
A Warning About Overwriting Files
The mv command is notoriously aggressive. If you rename a file to invoice.pdf, but a completely different file named invoice.pdf already exists in that folder, Linux will silently and instantly crush the old file out of existence to make room for the new one.
To prevent catastrophic data loss, you should train yourself to always use the -i (interactive) flag when renaming or moving important data.
mv -i report.txt important-report.txt
If important-report.txt already exists, the terminal will freeze and ask: “overwrite important-report.txt?” You can safely type n (no) to cancel the rename, or y (yes) to destroy the old file and proceed.