This simple article shows how to rename a file on a Linux machine. For this, we are going to use CentOS, but you can run this command on any Unix distro.
In this article, we are going to use the `mv` command to rename a file. This command is not only used to rename a file but also to move a file to a different location on a Linux machine.
The “mv” command is part of the Coreutils package.
Linux Command to Rename a File
Now, let’s see how to rename a file using the Linux command ‘mv’.
For this tutorial, we are first creating a file called hello.txt and then will rename it to hello_world.txt. You can find the syntax of the mv command below to rename a file.
mv source_file new_name
[opc@cent tutorial]$ echo "Hello, world" > hello.txt [opc@cent tutorial]$ [opc@cent tutorial]$ mv hello.txt hello_world.txt
Does “mv” command overwrites a file while renaming?
Yes, that’s true!
If you want to rename a file to a name that is already present in the same directory, mv will replace the file without confirming.
To avoid overwriting a file while using the “mv” command on Linux system always use “-i” option like the below:
$ mv -i hello_linux.txt hello_world.txt mv: overwrite ‘hello_world.txt’? n
Note: You can also use the “-n” option, but it will not prompt anything and will not overwrite the file if already present.
This was a basic Linux command to rename a file. We have other tools too to rename a file like the “rename” command, but we will discuss that in our next tutorial. You can also check this guide on Linux Commands Cheat Sheet.
For any questions, please leave your valuable comments, and for more such basic Linux tutorials, subscribe to our free newsletter.