How to Rename a File on Linux

The command to rename a file on Linux-based operating systems is:

mv old_name new_name

That's if the file is located in our current directory. Not in the current directory? Then we first have to "move" there with the cd command.

Here's a real example.

Let's say our files are in the following directory path: /home/alex/projects/project1/. First, we should switch to the project1 directory located at that path:

cd /home/alex/projects/project1/

Then we can list our directory contents with the ls command:

ls

Finally, to rename file5.html to index.html, we can run this command:

mv file5.html index.html

If we list our directory contents again, we can see the file was renamed:

This wraps up the most basic use case of the mv command.

Also want to know how to move, copy, or delete files and directories? Then check out our blog about how to manage files and directories on Linux.