Cyberithub

Top 15 Most Important mv command in Linux

In this article, I will take you through the usage of mv command in Linux. mv command is one of the linux terminal commands used on daily basis by Linux and System Admin across the world to perform day to day files and directories operations. So it is very important to understand the options and features available with mv command so that one can understand its use in effective manner.

What is mv ?

mv command in Linux is used to move and rename files and directories.

Synopsis

mv [OPTION]… [-TSOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE

 

Top 15 Most Important mv command in Linux 1

MV Command in Linux

Also Read: 16 Best ls command examples in Linux

Check Linux mv command version

If you want to check mv command version, you can do that by running mv --version command as shown below.

[root@localhost ~]# mv --version
mv (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker, David MacKenzie, and Jim Meyering.

Move file in Linux without any option

If you want to simply move the file hello.txt without any option to destination folder test, you can do that by launching command mv hello.txt test.

[root@localhost ~]# mv hello.txt test/

Do not Overwrite File

If you want to prevent any overwriting of file in destination folder then you need to use -n with command to check if the file is already available in destination directory or not. If it is there, it will not overwrite the file and will simply ignore the move command.

[root@localhost ~]# mv -n hello.txt test/

Check overwriting of file through Interactive Mode

If you want to check if the file is getting overwritten, you can do it through -i option. By default, usually you will see this option set in your system by using alias mv command.

[root@localhost ~]# mv -i hello.txt test/
mv: overwrite ‘test/hello.txt’?

Forcefully Overwriting the File

If you do not want System to throw any notification of overwriting the file, then you can launch forceful overwriting by -f option as shown below.

[root@localhost ~]# mv -f hello.txt test/

Create a backup before overwriting the file

If you want to take backup of the file before overwriting them, you can do it by using -b option. When you launch the command with -b option, it will take backup of the destination file by renaming the file with the suffix ~ at the end of the file and then it will overwrite the file.

This command sometimes is very helpful in saving our previous file before we accidentally overwrite them. If you make a habit of using this option when you don’t know if the destination folder have this file or not, then you will probably never accidentally end up in overwriting them.

[root@localhost test]# mv --backup hello.txt test/

Check the suffix of the backup’ed file.

[root@localhost test]# ls -lrt
total 8
-rw-r--r-- 1 root root 19 Jan 15 02:00 hello.txt~
-rw-r--r-- 1 root root 3893 Jan 15 02:13 hello.txt

Rename the backup using Custom Suffix

If you want to use some other suffix instead of System provided ~ suffix to rename your destination file while overwriting them, you can do it by using -S or --suffix option as shown below.

[root@localhost ~]# mv -S .bkp hello.txt test/
mv: overwrite ‘test/hello.txt’? y

Notice that now suffix is .bkp instead of ~ which can be seen at the end of the file hello.txt.

[root@localhost test]# ls -lrt
total 8
-rw-r--r-- 1 root root 3893 Jan 15 02:21 hello.txt.bkp
-rw-r--r-- 1 root root 3893 Jan 15 02:22 hello.txt

Set SELinux Security Context

If you want to set the SELinux Security Context of destination file to default, you can do that by using -Z option with mv command.

[root@localhost ~]# mv -Z hello.txt test/

Move only if Source is newer or destination file missing

If you want to move a file only when source file is having some update over the destination file or if the destination file is missing, then you can use -u option to do that as shown below.

[root@localhost ~]# mv -u hello.txt test/

Using verbose option to monitor the transfer of file

If you want monitor the output of mv command in linux, then you can do it through by using verbose -v option as shown below.

[root@localhost ~]# mv -v hello.txt test/
mv: overwrite ‘test/hello.txt’? y
‘hello.txt’ -> ‘test/hello.txt’

Rename File in Linux

If you want to use mv command in renaming files, you can do that by running below command.

[root@localhost ~]# mv hello.txt example.txt

Check the file after renaming it.

[root@localhost ~]# ls -lrt example.txt
-rw-r--r-- 1 root root 3893 Jan 15 03:32 example.txt

Rename Directory in Linux

If you want to rename any directory name, you can use mv command to do that as shown below.

[root@localhost ~]# mv test hello

Notice that all the contents available in test directory is now showing in hello directory.

[root@localhost hello]# ls -lrt
total 4
-rw-r--r-- 1 root root 3893 Jan 15 03:18 hello.txt

Move all the contents from directory using -t option

Sometimes it might happen that running mv command for a directory does not know the difference if you are trying to move the directory and its all contents to target directory and just trying to rename it. So to avoid this condition, you can use -t option with mv command. This will make sure that you are transferring directory and its contents to target directory and not renaming it.

[root@localhost ~]# mv -t java/ hello/

Notice from below output that hello directory and all its contents are moved to java target directory.

[root@localhost java]# ls -lrt
total 8
-rw-r--r-- 1 root root 336 Dec 12 07:54 example.java
-rw-r--r-- 1 root root 3893 Jan 15 01:55 abc.txt
drwxr-xr-x 2 root root 23 Jan 15 03:18 hello

Check mv help command

If you want to check all the options available with mv command, you can check by using --help option.

[root@localhost ~]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] 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 overwrite
-n, --no-clobber do not overwrite an existing file

Check mv command alias

If you want to check any aliases set for mv command in Linux, you can check it by using alias mv command in Linux.

[root@localhost hello]# alias mv
alias mv='mv -i'

Few More Options

OptionDescription
--backup[=CONTROL] make a backup of each existing destination file
-blike --backup but does not accept an argument
-f, --forcedo not prompt before overwriting
-i, --interactiveprompt before overwrite
-n, --no-clobberdo not overwrite an existing file
-S, --suffix=SUFFIXoverride the usual backup suffix
-u, --updatemove 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, --contextset SELinux security context of destination file to default type

Also Read: 16 zip command examples in Linux/Unix

Reference: mv man page

 

Popular Searches

  • linux move file
  • move directory linux
  • rename directory linux
  • terminal move file
  • rename command in linux
  • move folder linux

Leave a Comment