Hi there, In this tutorial, you will learn about the mv command in Linux with examples. As we all know that file management is one of the essential things for Linux users. Well, It is very easy to get the list of all the Linux command from somewhere but when we try to use we get some error. I am telling it because the mv command is a very popular Linux command almost every Linux user know that the mv command is used for renaming or moving files and directory but when they try to use it on their Linux terminal they face issues. They get errors because they only know there is an mv command but they do not know about the syntax.
In this guide, you will learn how to use the mv command for renaming files and directories in Linux as well as how to move a file or directory using the same command.
You may think, renaming and moving files and directories are two different things, Is it even possible to perform two operations using a single command?
Yes, It is possible.
- Rename files and directories.
- Move a file from one directory to another directory.
- Move one directory to another.
Table of Contents
Rename Files and Directories
The mv command is used for renaming files as directories in Linux. Only you need to know about the proper syntax which you are going to learn from this tutorial.
Rename a file
Syntax of mv command to rename files and directories–
mv [FileName] [NewFileName]
Here, mv is the command; Replace [FileName] with the name of the file you want to rename and put the new file name in the place of [NewFileName].
Let’s take a real example-
Suppose, we have a file, named string.py and we want to change the name of this file from the string.py to array. So, we will run the following command on our Linux terminal-
mv string.py array
Rename a directory
You have got to know how to rename a file in Linux but could you guess how can you rename a directory? We can guess, you are thinking that we will use the same syntax. If you are thinking so then you are absolutely correct.
The syntax will be the same as renaming files but we will use a directory name instead of a file name. Let’s have a look at the syntax-
mv [DirectoryName] [NewDirectoryName]
Let’s assume that we have a directory, named PythonScript and we want to change the name of this directory from PythonScript to PythonCode so we will run the following command-
mv PythonScript PythonCode
Move a File From One Directory to Another
The main purpose of the mv command is to move one or more files from one place to another place. So let’s see how we use the move command in Linux to change the file location.
Syntax of mv command-
mv [FileName] [Destination]
Let us explain-
Here, mv is the command for moving the file, And replace [FileName] with the name of the file you want to move and put the destination at the place of [Destination].
Let’s have an example-
First, have a look at the following image:
In the Image you can see there is a blur-me.git file in the home directory. And at the second step, you can see we have run
mv bluer-me.git Desktop command which simply means we are moving the blur-me.git file from the home directory to the Desktop.
To verify if the file is successfully moved or not we have run the
ls Desktop command. The ls command simply lists the files and directories.
Rename File While Moving From One Directory to Another
It is also possible to rename the file in the process of moving it. This mean, you do not have to rename the file name before or after moving it but it will get done automatically if you use the mv command to do so.
Let’s see the syntax and then example-
mv [FileName] [Destination/NewFileName]
Here, replace [FileName] with the file name and then put the location destination and a / and the new file name.
Let’s take an example-
We have a file named “SimpleTxt” we are have moved this file from the home directory to the Documents directory with a different name by running the following command-
mv SimpleTxt Documents/NewSimpleTxt
The SimpleTxt file was moved and renamed as NewSimpleTxt after running the command which is in the example.
Move Directory from One to Another Using mv Command
If you want to move one directory to another using the mv command then you can easily do it by running the following command-
mv [DirectoryName] [DestinationDirectory]
On your Linux terminal enter “mv command” then put the name of the directory you want to move and then put the name of the destination directory.
Have a look at the image-
In the image, you can see there is a directory named “PHPScript” which is in the home directory and we have moved it to another directory named “Script”.
Move Multiple Files at Once
The mv command allows us to move multiple files together from one place to another place. In order to do so. we use mv command followed by the list of files separated by spaces and passing the destination location where we want to move the file.
mv [File1] [File2] [File3] File4] [Destionation]
Let’s understand with an example-
First of all check the following Imag:
In the image, you can see there are five files with five different country names and also there is one directory named “Countries”. We have moved those five files with country names to the “Countries” directory. Using the following command-
mv us uk chaina russia india countries
We hope you understand.
In this tutorial, we have explained all about the mv Linux command. The mv command is used to move files from one location to another location. Even we can use it to move directories also. We can also use the mv command to rename the file or rename and move it to another location.