How to use mv command to move a directory in Linux Guide 2021

mv-command-to-move-a-directory-in-Linux-complte-guide-image

mv command is very basic command in linux, used to transfer files and move a directory in linux from one location to another location.

I will explain everything about mv command so you will learn how move folder in linux without any headache.

You may like the following tutorial:

Article Contents

Introduction of mv command

The mv command is short form of move. It is gererally used to move files and directories from one location to another.

You can use mv command in Linux to rename files as well as folders. So you can use mv command for two purposes one is move content and another is renaming the files and directories.

You can also use the cp command to rename the files and folders.

mv command is comes with every linux system pre-installed, it is a core system command.

It is simple to use, open terminal by pressing (Ctrl+Shift+T) and start typing mv command in Linux. Let’s know the basic syntax and more options about it.

The basic syntax of mv command in Linux

The basic syntax for the mv command is as follows:

mv [OPTIONS] SOURCE DESTINATION

The SOURCE can be one, or more files or directories, which you want to move, and DESTINATION can be a single file/directory, where you want to move/rename.

Some basic conditions are below:

  • When you use multiple files or directories are given as a SOURCE, then the DESTINATION must be a directory, multiple files/directory contents can’t be move-in a single file. So the destination must be a folder. In this case, the SOURCE files will be moved to the target directory.
  • If your source is a single file/directory, and an existing directory is a destination, then the file/directory will be moved to the destination directory.
  • When your source and destination files/directory’s location are the same, but destination name differs from source then file and folder are renaming.
  • When the SOURCE is a directory and DESTINATION doesn’t exist, SOURCE will be renamed to DESTINATION. Otherwise, if the DESTINATION exists, it is moved inside the DESTINATION directory.
  • To move a file or directory, you need to have write permissions on both SOURCE and DESTINATION. Otherwise, you will receive a permission denied error.

For example, to move the file file1 from the current working directory to the /tmp directory you would run:

$mv file1 /tmp

[vijay@localhost ~]$ touch file1
[vijay@localhost ~]$ ls
data     Documents  file1  Pictures  Templates
Desktop  Downloads  Music  Public    Videos
[vijay@localhost ~]$ mv file1 /tmp/
[vijay@localhost ~]$ ls /tmp/
file1
systemd-private-160843acac93410f9342bc607e19610c-bolt.service-czZFwo
systemd-private-160843acac93410f9342bc607e19610c-colord.service-l8ha1b
systemd-private-160843acac93410f9342bc607e19610c-fwupd.service-yr8f8s
systemd-private-160843acac93410f9342bc607e19610c-geoclue.service-vYXe0m
systemd-private-160843acac93410f9342bc607e19610c-ModemManager.service-AgVw8X
systemd-private-160843acac93410f9342bc607e19610c-rtkit-daemon.service-pB5oKn
Temp-4d8d6efc-7c08-4186-9a96-cd7e56e9da3e
Temp-97fcac93-4121-4f23-813b-40bfe2bd6c2f
tracker-extract-files.1000
vboxguest-Module.symvers
[vijay@localhost ~]$ 

move a directory in Linux from source to target

I want to move directory from one location to another location, Then possible conditions are two.

Condition 1: Move the directory from Current Location to the desired destination. Then the syntax will be as below:

$mv Dorectory_name /path/to/destination

Condition 2: Move the directory from the desired location to the desired destination. You can say from one location to another location Then the syntax will be as below:

$mv /path/to/source /path/to/destination

In the following example, I will use mkdir command to create a directory into the current working directory named dir1. You can use pwd command to check the current working directory.

I have created a new directory named dir1 now I will move a directory from the current location to /home/vijay/Documents,

Second I am going to move dir1 from /home/vijay/Documents to /home/vijay/Downloads. You can see the complete task in below commands in the terminal

[vijay@localhost ~]$ mkdir dir1
[vijay@localhost ~]$ ls 
data     dir1       Downloads  Pictures  Templates
Desktop  Documents  Music      Public    Videos
[vijay@localhost ~]$ mv dir1/ /home/vijay/Documents/
[vijay@localhost ~]$ ls
data  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[vijay@localhost ~]$ ls /home/vijay/Documents/
data  dir1
[vijay@localhost ~]$
[vijay@localhost ~]$ mv /home/vijay/Documents/dir1 /home/vijay/Downloads/
[vijay@localhost ~]$ ls /home/vijay/Downloads/
dir1
[vijay@localhost ~]$ 

Move multiple directories in Linux

You can move multiple directories from source to destination, simple specify multiple directories one by one.

For Example, I am willing to copy 3 directories respectively named dir2, dir3, and dir4 from current location to destination /home/vijay/Documents. The syntax is as follows:

$mv dir2 dir3 dir4 /home/vijay/Documents/

[vijay@localhost ~]$ mv /home/vijay/Documents/dir1 /home/vijay/Downloads/
[vijay@localhost ~]$ ls /home/vijay/Downloads/
dir1
[vijay@localhost ~]$ mkdir dir2 dir3 dir4
[vijay@localhost ~]$ ls
data     dir2  dir4       Downloads  Pictures  Templates
Desktop  dir3  Documents  Music      Public    Videos
[vijay@localhost ~]$ mv dir2 dir3 dir4 /home/vijay/Documents/

For Example, I am willing to copy 3 directories respectively named dir2, dir3, and dir4 from /home/vijay/Documents/ location to destination /home/vijay/Downloads. Then I will use wildcard to perform this action The syntax is as follows:

$mv /home/vijay/Documents/dir* /home/vijay/Downloads/

[vijay@localhost ~]$ ls /home/vijay/Documents/
dir2  dir3  dir4
[vijay@localhost ~]$ mv /home/vijay/Documents/dir* /home/vijay/Downloads/
[vijay@localhost ~]$ ls /home/vijay/Downloads/
dir1  dir2  dir3  dir4
[vijay@localhost ~]$

Note: the all directories should be in same source.

mv command options

mv command is basic command like other in linux, So you can use this command with special options to complete move directory task.

To know about all options can use man command, This command allow you to see the manual page of mv command. The syntax will be as follows:

$man mv

Prompt before overwriting

Sometime, when you are moving files and folders then it may possible that source content available on destination.

By default, if the destination file exists, it will be overwritten. To prompt for confirmation, use the -i option:

vijay@localhost ~]$ ls /home/vijay/Downloads/
dir1  dir2  dir3  dir4
[vijay@localhost ~]$ ls
data  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[vijay@localhost ~]$ mkdir dir1
[vijay@localhost ~]$ mv -i dir1/ Downloads/
mv: overwrite 'Downloads/dir1'? y
[vijay@localhost ~]$

Force overwriting

If the content on the destination are with read-only permission, You can face the problem. Overwriting process works with contents have write permission.

If you try to overwrite a read-only file and folders, the mv command will prompt you whether you want to overwrite the file:

See the example below:

[vijay@localhost ~]$ chmod 444 Downloads/dir1/
[vijay@localhost ~]$ ls
data  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[vijay@localhost ~]$ mkdir dir1 
[vijay@localhost ~]$ mv -i dir1/ Downloads/
mv: replace 'Downloads/dir1', overriding mode 0444 (r--r--r--)? n
[vijay@localhost ~]$ mv dir1/ Downloads/
mv: replace 'Downloads/dir1', overriding mode 0444 (r--r--r--)? n
[vijay@localhost ~]$ mv -f dir1/ Downloads/
[vijay@localhost ~]$ 

In above example, I have used chmod command to set the read-only permission of directory named dir1 inside Downloads location.

Create new directory named dir1 in current working directory.

I used mv command with -i option, which is usually prompt with before write. it usually.

I used again mv command without any option, by default command will overwrite exisitng file. but it gives prompt option due to the read-only permission of dir1 folder.

If you are bothering with prompt then use -f option, which is especially useful when you need to overwrite multiple read-only files.

Do not overwrite existing files

You can use the -n option tells mv never to overwrite any existing file, Use the following syntax:

$mv -n Source Destination

[vijay@localhost ~]$ mkdir dir1
[vijay@localhost ~]$ ls
data     dir1       Downloads  Pictures  Templates
Desktop  Documents  Music      Public    Videos
[vijay@localhost ~]$ mv -n dir1/ Downloads/
[vijay@localhost ~]$ ls
data     dir1       Downloads  Pictures  Templates
Desktop  Documents  Music      Public    Videos
[vijay@localhost ~]$ 

Verbose output

Another important option is -v, which used for verbose the process.

[vijay@localhost ~]$ mv -v dir1/ Downloads/
renamed 'dir1/' -> 'Downloads/dir1'
[vijay@localhost ~]$ mkdir dir10
[vijay@localhost ~]$ mv -v dir10/ Downloads/
renamed 'dir10/' -> 'Downloads/dir10'
[vijay@localhost ~]$ 

Backing up files

Sometime, you must move the file to destionation and don’t overwrite existing file. It really very hard situation for adminstrator.

If you face such problem then you can backup the existing files. So your files and folders will be copied without overwriting.

Source files and folders will copy as a backup file and The backup file will have the same name as the original file with a tilde (~) appended to it.

[vijay@localhost ~]$ ls
data  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[vijay@localhost ~]$ mkdir dir1
[vijay@localhost ~]$ ls
data     dir1       Downloads  Pictures  Templates
Desktop  Documents  Music      Public    Videos
[vijay@localhost ~]$ mv -b dir1/ Downloads/
[vijay@localhost ~]$ ls Downloads/
dir1  dir1~  dir10  dir2  dir3  dir4
[vijay@localhost ~]$ mkdir dir11
[vijay@localhost ~]$ mv -b dir11/ Downloads/
[vijay@localhost ~]$ ls Downloads/
dir1  dir1~  dir10  dir11  dir2  dir3  dir4
[vijay@localhost ~]$ 

Video Move files and Directory using mv command in Linux

Conclusion

You have learnt about mv command, which is used to move and rename files and directories in linux and unix.

For more information about the mv command, check the man page or type man mv in your terminal.

New Linux users who are intimidated by the command line can use the GUI file manager to move their files.

If you have any questions or feedback, feel free to leave a comment.

If you like our content, please consider buying us a coffee.
Thank you for your support!