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.

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

Moving file1 to the directory Dir.

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

Moving file2 and Dir to the directory Test:

[root@localhost /]# mv file2 Dir/ /root/Test/
---
[root@localhost /]# cd /root/Test/
[root@localhost 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 A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

Leave a Reply

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