How to move a File/Directory from one location to another – The “mv” command and switches with example in Linux

We can use the command “mv” to move something from one location to another. Here, I am explaining the uses of the command “mv” and different switches of mv.

Syntax:

mv [OPTION]... SOURCE DEST

Example:
I have created two files(file1 and file2) and one directory(Dir)for illustrating the MV.

[[email protected] Test]# touch file{1..2}
[[email protected] Test]# mkdir Dir
---
[[email protected] Test]# ll file
file1 file2
[[email protected] Test]# ll -d Dir/
drwxr-xr-x. 2 root root 4096 Mar 14 21:26 Dir/
---

Moving file1 to the directory Dir.

[[email protected] /]# mv file1 Dir/
---
[[email protected] /]# cd Dir/
[[email protected] Dir]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 14 21:26 file1
---

Moving file2 and Dir to the directory Test:

[[email protected] /]# mv file2 Dir/ /root/Test/
---
[[email protected] /]# cd /root/Test/
[[email protected] Test]# ll
drwxr-xr-x. 2 root root 4096 Mar 14 21:29 Dir
-rw-r--r--. 1 root root 0 Mar 14 21:26 file2
---

Switches with example:
The move (mv) command has a lot of useful switches. Some of them are listed below:

1, -f, –force
do not prompt before overwriting

2, -i, –interactive
prompt before overwrite

3, -n, –no-clobber
do not overwrite an existing file

Note : If you specify more than one of -i, -f, -n, only the final one takes effect.

4, -u, –update
move only when the SOURCE file is newer than the destination file or when the destination file is missing

5, -v, –verbose
explain what is being done

6, –help
display this help and exit

7, –version
output version information and exit

That’s it!!

We can use the command “mv” to rename files and directories. Click here for more details, How to rename a File/Directory.

 

Related posts:
groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab, ftp commands
tar, rpm, rkhunter, Rsync, grep, netstat command switches, netstat command’s practical usages, OpenVZ commands, lsof command

Post navigation

Arunlal Ashok

DevOps Engineer. Linux lover. Traveller.
Always happy for an open discussion! Write to arun ((@)) crybit ((dot)) com.

Leave a Reply

Your email address will not be published. Required fields are marked *