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

How to use mv command to move a directory in Linux Guide for beginners

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

[[email protected] ~]$ touch file1
[[email protected] ~]$ ls
data     Documents  file1  Pictures  Templates
Desktop  Downloads  Music  Public    Videos
[[email protected] ~]$ mv file1 /tmp/
[[email protected] ~]$ 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
[[email protected] ~]$ 

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

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

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/

[[email protected] ~]$ mv /home/vijay/Documents/dir1 /home/vijay/Downloads/
[[email protected] ~]$ ls /home/vijay/Downloads/
dir1
[[email protected] ~]$ mkdir dir2 dir3 dir4
[[email protected] ~]$ ls
data     dir2  dir4       Downloads  Pictures  Templates
Desktop  dir3  Documents  Music      Public    Videos
[[email protected] ~]$ 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/

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

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:

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

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:

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

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

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

Verbose output

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

[[email protected] ~]$ mv -v dir1/ Downloads/
renamed 'dir1/' -> 'Downloads/dir1'
[[email protected] ~]$ mkdir dir10
[[email protected] ~]$ mv -v dir10/ Downloads/
renamed 'dir10/' -> 'Downloads/dir10'
[[email protected] ~]$ 

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.

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

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 Appreciate My Work, You should consider: