mv command allows you to move files and directories, rename files. And also to overwrite the file and directories.
mv | move command in linux | move directories and files
![]() |
linux command mv |
Hello, guys welcome to the next session on Linux command line tutorial. In this session, we will see how we can use "mv command" in Linux.
Let's get started I will just press "ctrl + alt + T" to open my terminal.
Syntax :
$mv [option] [source] [destination]
So first you use "mv" as a command then you can use options like "-v","-i" or "-f"
Then comes your source file. It can be one or many files or directories.
And then comes the destination (file or directory)
Rename a file using mv command
![]() |
Rename the file using mv |
$mv [fileName] [newfile Name]
First of all, I have this "file1.txt" and I want to use the "mv" command to change the name of this file.
So I will just use "mv" and then the name of the file and then I will just give some other name to the file. For example "file2.txt" and press ENTER.
And you will see here this file1 becomes file2.
And the content of the file will remain the same.
move a file to the directory using mv command
![]() |
moving a file to the directory |
$mv [file name] [folder name]
Now for example I want to move this "file2.txt" to "folder1".
I can just use "mv" and then the file name and then my directory name and press ENTER.
So in this way you can transfer the file or move the file to our directory using the move command.
To avoid overwrite use an interactive flag with mv
![]() |
Interactive mode |
$mv -i [file name] [folder name]
I want to create a new file. For example "file2.txt" on the desktop.
And I want to move this "file2.txt" to folder1 which already have "file2".
So if you want to move the file which already exists then it will overwrite the file.
At this time what you are going to do is just use a flag (-i) with this command "mv -i file2.txt folder1".
"i" stands for interactive mode.
So it will ask you if you want to overwrite the file. Which was already present in the folder1 with the same name.
When I press "Y" it's going to overwrite the file or if I have press "N" and it's not going to overwrite the file.
move a directory using mv command
![]() |
move directory in linux |
$mv [directory name] [newDirectory name]
Let's create a directory called folder2
Now for example I want to transfer the "folder1" inside the "folder2".
I can use the Move command. So I can just write "mv folder1 folder2" and press ENTER. And it's going to move folder1 inside folder2.
So if the "folder2" doesn't exist it's going to create a directory and then move the content of the folder1 inside folder2.
Conclusion
I hope you understood this basic concept which I want to convey here.
To learn more about "mv" just write "man mv" and it's going to view the whole description of using the mv command.
So just use this and read these descriptions and you will be good to go.
I hope you have enjoyed this tutorial and bye for now.
HAVE A NICE DAY 💗