How to Rename a File or Directory in Linux Terminal

In a graphical interface like Windows or macOS, renaming a file is as simple as right-clicking and typing a new name. In the Linux terminal, however, things work a bit differently. Surprisingly, there is no dedicated “rename” command for basic file changes. Instead, you use the mv (move) command.

Why Use the ‘mv’ Command?

The mv command is primarily used to move files from one directory to another. However, if you “move” a file to the exact same directory but give it a different name, the system effectively renames it. This is the standard method for renaming files and directories in Linux.

How to Rename a File

The basic syntax for the mv command is mv [old_name] [new_name].

For example, if you have a file named report.txt and you want to rename it to final_report.txt, you would type the following command in your terminal:

mv report.txt final_report.txt

Press Enter. The file is instantly renamed. If you type ls to list the directory contents, you will see the new name.

How to Rename a Directory

Renaming a folder (directory) uses the exact same command. If you have a folder called photos and want to change it to vacation_photos, the command is:

mv photos vacation_photos

A Word of Caution

Be careful when using the mv command. If a file with your chosen [new_name] already exists in that directory, Linux will silently overwrite the existing file with your renamed file, and the old data will be lost forever.

To prevent this, you can add the -i (interactive) flag to the command. This tells Linux to ask for confirmation before overwriting anything:

mv -i report.txt final_report.txt

Get the best tech tips delivered straight to your inbox.

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