How to Rename a File or Directory in Ubuntu Using the mv Command

If you are coming to Linux from a graphical operating system like Windows or macOS, you are accustomed to right-clicking a file and simply selecting “Rename” from a menu. However, when you open the Ubuntu terminal and search for a rename command, you might be confused to find that the standard tool for renaming files is actually the mv (move) command.

In the UNIX philosophy that governs Linux, renaming a file and moving a file are technically the exact same operation at the file system level. You are simply changing the path and name associated with a specific chunk of data on the hard drive. Therefore, the same command handles both tasks.

This guide explains how to use the mv command to rename files and directories in the Ubuntu terminal.

The Basic Syntax

The mv command requires two pieces of information to function correctly: the source (what the file is currently named) and the destination (what you want the file to be named).

The basic syntax looks like this:

mv current_filename new_filename

Renaming a Single File

Assume you are working in your Documents directory and you have a file named budget_2023.txt. You realize this is a typo, and it should be 2024. To rename the file, you simply use the mv command and provide the old name followed by the new name.

mv budget_2023.txt budget_2024.txt

When you press Enter, the terminal will not print a success message. It will simply drop you back to a blank prompt. If you run the ls command to list the directory contents, you will see that budget_2023.txt is gone, replaced by budget_2024.txt.

Renaming a Directory

The exact same logic applies to directories (folders). You do not need a special command or flag to rename a folder. You just use mv.

If you have a folder named Old_Photos and you want to rename it to Archived_Photos, you run:

mv Old_Photos Archived_Photos

Important Warnings and Safety Flags

The mv command is incredibly powerful, but it is also completely silent and completely unforgiving. It assumes you know exactly what you are doing.

If you run the command mv report.txt invoice.txt, and a file named invoice.txt already exists in that directory, the mv command will instantly and permanently overwrite the existing invoice file with the report file. It will not ask for confirmation, and there is no “Recycle Bin” to recover the lost file from the terminal.

To prevent catastrophic data loss, especially when you are just learning Linux, you should get in the habit of using the interactive flag (-i).

mv -i report.txt invoice.txt

If you use the -i flag, and a conflict exists, the terminal will pause and ask you: mv: overwrite 'invoice.txt'? You must then type y for yes or n for no, providing a crucial safety net against typos.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.