How to rename file or folder in Linux

In this article, we will see how to rename a file or folder in Linux. In Linux, mv command is used to rename a file or folder(directory).

Syntax:

mv [OPTION]... [-T] SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY mv [OPTION]... -t DIRECTORY SOURCE... mv old_file_name new_file_name

Examples to rename file or folder in Linux:

1. Rename a file in Linux by using mv. In this example, we are going to rename test.python file to test.py

mv test.python test.py

To verify run the command ls -l

2. Rename a file in Linux with verbose. -v option explains what is being done

mv -v test.python test.py

Output:

root@mongodb1:/home/r2schools/dump/rename# mv -v test.python test.py renamed 'test.python' -> 'test.py'

How to rename file or folder in Linux

3.Rename a file with -i option. If we want overwrite with existing file, we use -i option. In the below example, bad.py will be overwritten with abc.py.

root@mongodb1:/home/r2schools/dump/rename# mv -i bad.py abc.py mv: overwrite 'abc.py'? y root@mongodb1:/home/r2schools/dump/rename# ls abc.py bcd.txt dev production test.py xyz.txt

4. Rename a folder in Linux. In the below example we are going to rename folder dev to uat.
Before folder rename

root@mongodb1:/home/r2schools/dump/rename# ls abc.py bcd.txt dev production test.py xyz.txt

Command to rename folder.

root@mongodb1:/home/r2schools/dump/rename# mv dev uat

After folder rename

root@mongodb1:/home/r2schools/dump/rename# ls abc.py bcd.txt production test.py uat xyz.txt root@mongodb1:/home/r2schools/dump/rename#

So, in this article, we have covered how to rename a file or folder in Linux using mv command.