9 Quick ‘mv’ Command Practical Examples in Linux

In Unix and Linux like systems, mv command is used to move files and directories from once place to another. It is also used to rename files and directories (or folders).

linux-mv-command-examples

New Linux users may face issues and confusions while using mv command, some of them are listed below:

  • Identifying the source and destination files in Linux
  • How to move one or more files or directories in a single command
  • Fear to loss of data and overwrites
  • Options of mv command and their functionalities
  • How to take backup successfully

We will try to cover these issues and confusions by doing practical examples of mv command.

Syntax of mv command,

# mv {options} <Source> <Destination>

  • Source: file or directory which will be either renamed or moved
  • Destination: File or directory where files will be moved or renamed

Different options used in mv command,

mv-command-options

Let’s study the mv command with beneath practical examples.

Examples:1) Move file & directory from one folder to another

Let’s suppose we want to move a file ‘devops.txt‘ from current working directory to “sysadm” directory,

[linuxbuzz@web ~]$ mv devops.txt sysadm/
[linuxbuzz@web ~]$ ls -l sysadm/
total 0
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 24 09:05 devops.txt
[linuxbuzz@web ~]$

Move the ~/sysadm folder to /tmp

[linuxbuzz@web ~]$ mv ~/sysadm /tmp/

Use below command to verify whether sysadm is moved to /tmp or not,

[linuxbuzz@web ~]$ ls -ld /tmp/sysadm
drwxrwxr-x. 2 linuxbuzz linuxbuzz 23 Aug 24 09:07 /tmp/sysadm
[linuxbuzz@web ~]$

Example:2) Rename file and directory

Let’s assume we want to rename file ‘devops.txt’ as ‘linuxadmins.txt’ from folder /tmp/sysadm, run the beneath mv command,

[linuxbuzz@web ~]$ cd /tmp/sysadm/
[linuxbuzz@web sysadm]$ mv devops.txt linuxadmins.txt

Use below ls command verify file has been renamed or not,

[linuxbuzz@web sysadm]$ ls *.txt
linuxadmins.txt
[linuxbuzz@web sysadm]$

Let’s assume we want to rename folder /tmp/sysadm to /tmp/sysadmin,

[linuxbuzz@web ~]$ mv /tmp/sysadm /tmp/sysadmin
[linuxbuzz@web ~]$ ls -ld /tmp/sysadm*
drwxrwxr-x. 2 linuxbuzz linuxbuzz 28 Aug 24 09:15 /tmp/sysadmin
[linuxbuzz@web ~]$

Example:3) Moving multiple files into directory

Let’s assume we want to move files like file1.txt, file2.txt and file3.txt from present working directory to /tmp/sysadmin

[linuxbuzz@web ~]$ mv file1.txt file2.txt file3.txt /tmp/sysadmin

Execute below ls command to verify whether files have been moved or not,

[linuxbuzz@web ~]$ ls /tmp/sysadmin/file*.txt
/tmp/sysadmin/file1.txt  /tmp/sysadmin/file2.txt  /tmp/sysadmin/file3.txt
[linuxbuzz@web ~]$

Example:4) Prompt before overwriting (mv -i)

Let’s suppose we want to move a file called “tools.txt” from our current working directory to /tmp/sysadmins folder. I am assuming tools.txt file already exists in sysadmins folder. So, in this scenario if we use mv command without any options then it will overwrite the file at destination, But if we want prompt before overwriting then use ‘-i’ option. Example is shown below,

[linuxbuzz@web ~]$ mv -i tools.txt /tmp/sysadmin/
mv: overwrite ‘/tmp/sysadmin/tools.txt’?

Example:5) Do not overwrite existing file at destination (mv -n)

Use ‘-n’ option in mv command in case if we don’t want to overwrite an existing file at destination,

[linuxbuzz@web ~]$ ls -l tools.txt /tmp/sysadmin/tools.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 24 09:59 /tmp/sysadmin/tools.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 24 10:10 tools.txt
[linuxbuzz@web ~]$

As we can see tools.txt is present in our current working directory and in /tmp/sysadmin, use below mv command to avoid overwriting at destination,

[linuxbuzz@web ~]$ mv -n tools.txt /tmp/sysadmin/tools.txt
[linuxbuzz@web ~]$

Example:6) Forcefully overwrite write protected file at destination (mv -f)

Use ‘-f’ option in mv command to forcefully overwrite the write protected file at destination. Let’s assumes we have a file named “bands.txt” in our present working directory and in /tmp/sysadmin.

[linuxbuzz@web ~]$ ls -l bands.txt /tmp/sysadmin/bands.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 25 00:24 bands.txt
-r--r--r--. 1 linuxbuzz linuxbuzz 0 Aug 25 00:24 /tmp/sysadmin/bands.txt
[linuxbuzz@web ~]$

As we can see under /tmp/sysadmin, bands.txt is write protected file,

Without -f option

[linuxbuzz@web ~]$ mv bands.txt /tmp/sysadmin/bands.txt

mv: try to overwrite ‘/tmp/sysadmin/bands.txt’, overriding mode 0444 (r–r–r–)?

To forcefully overwrite, use below mv command,

[linuxbuzz@web ~]$ mv -f bands.txt /tmp/sysadmin/bands.txt
[linuxbuzz@web ~]$

Example:7) Verbose output of mv command (mv -v)

Use ‘-v’ option in mv command to print the verbose output, example is shown below

[linuxbuzz@web ~]$ mv -v  buzz51.txt buzz52.txt buzz53.txt buzz54.txt /tmp/sysadmin/
‘buzz51.txt’ -> ‘/tmp/sysadmin/buzz51.txt’
‘buzz52.txt’ -> ‘/tmp/sysadmin/buzz52.txt’
‘buzz53.txt’ -> ‘/tmp/sysadmin/buzz53.txt’
‘buzz54.txt’ -> ‘/tmp/sysadmin/buzz54.txt’
[linuxbuzz@web ~]$

Example:8) Create backup at destination while using mv command (mv -b)

Use ‘-b’ option to take backup of a file at destination while performing mv command, at destination backup file will be created with tilde character appended to it, example is shown below,

[linuxbuzz@web ~]$ mv -b buzz55.txt /tmp/sysadmin/buzz55.txt
[linuxbuzz@web ~]$ ls -l /tmp/sysadmin/buzz55.txt*
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 25 00:47 /tmp/sysadmin/buzz55.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 0 Aug 25 00:37 /tmp/sysadmin/buzz55.txt~
[linuxbuzz@web ~]$

Example:9) Move file only when its newer than destination (mv -u)

There are some scenarios where we same file at source and destination and we wan to move the file only when file at source is newer than the destination, so to accomplish, use -u option in mv command. Example is shown below

[linuxbuzz@web ~]$ ls -l tools.txt /tmp/sysadmin/tools.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 55 Aug 25 00:55 /tmp/sysadmin/tools.txt
-rw-rw-r--. 1 linuxbuzz linuxbuzz 87 Aug 25 00:57 tools.txt
[linuxbuzz@web ~]$

Execute below mv command to mv file only when its newer than destination,

[linuxbuzz@web ~]$ mv -u tools.txt /tmp/sysadmin/tools.txt
[linuxbuzz@web ~]$

That’s all from this article, we have covered all important and basic examples of mv command.

Hopefully above examples will help you to learn more about mv command. Write your feedback and suggestions to us.

Leave a Comment

three × five =