Moving files and directories in Linux is very easy for most Linux GUI users because using file manager you can easily do the task. But in case you are not CLI based Linux distro instead of Linux GUI, then you must use mv commands to move files or directories.
In this tutorial, we will explain how to move files and directories using the mv command in the Linux terminal.
The mv command in Linux terminal
The mv command or move command is used for moving files and directories from one destination to other. However, using the same command you can also rename the file or directory while moving them to another destination. So, the syntax for the mv command is as follows:
mv [options] SOURCE DESTINATION
mv -f /home/linux/download/data.pdf /home/linux/document/data.pdf
As you can see the example of an mv syntax here is clearly defined the option, source of the file or directory and the destination where needs to be moved the file or directory.
Here you can use different options to complete the moving task, however, the SOURCE can be one, or more files or directories and the DESTINATION can be a single directory, where you going to put the source file(s).
Move (mv) command options
|–backup||make a backup of each existing destination file|
|-b||like –backup but does not accept an argument|
|-f, –force||do not prompt before overwriting|
|-I, –interactive||prompt before overwriting|
|-n, –no-clobber||do not overwrite an existing file|
|-s, –suffix||override the usual backup suffix|
|-t, –target-directory||move all SOURCE arguments into DIRECTORY|
|-T, –no-target-directory||treat DEST as a normal file|
|-u, –update||move only when the SOURCE file is newer than the destination file or when the destination file is missing|
|-v, –verbose||explain what is being done|
|-z, –context||set SELinux security context of the destination file to default type|
Need more details about the mv command’s options, read GNU software manuals.
How to use the mv command in the Linux terminal
Above we have mentioned the mv syntax and related options, using one of these options you can complete the files or directories moving task. In case you want to take backup while moving or want to rename files or directories, you can do it easily.
So, for some examples, we are going to see how you can interpret the mv command in Linux.
Moving files and directory one directory to another
As we have already mentioned, you can move multiple files at a time using the mv command in the Linux terminal, and for that, you must interpret correct arguments.
mv file.zip /tmp
Here we have mentioned the file.zip, and it could be any type of file, we have just taken an example. After the file needs to be a space then the destination directory name, as you can see /tmp for an example.
Similarly, you can move a directory inside another directory using the following command.
mv dir1 dir2
Here dir1 is the directory we are moving inside the dir2 directory. If the dir2 directory doesn’t exist then the dir1 directory will be renamed as dir2.
Rename file name or directory name usnig mv command
Using the mv command we can rename the directory name and even file name, for that use the following commands:
mv movie.mp4 movie.mkv
Here the source file is the movie.mp4 and using the mv command we can rename the file. For example, we renamed the file from the movie.mp4 to the movie.mkv and you can do the same.
Similarly, you can do the same with directories. Make sure, when you rename the directory or file that doesn’t exist already.
mv dir1 dir2
Checkout ls command for list files and directories.
Moving Multiple Files and Directories
You can move multiple files, and you have to mention the source files names and then the space and the destination directory.
mv file1.zip file2.pdf /tmp
If you want to move a specific file type files, then you can add a wild-card pattern to move all the specific files to another directory.
mv *.zip /documents
The mv command in Linux is useful for moving files and directories and while renaming the same. And various options of this command also can be useful for certain conditions. So, if you want to know more about it, then execute mv –help in your Linux terminal.
For new Linux users command line could be hard, but you can also move files using GUI file manager in any Linux distribution.