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,

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

Move the ~/sysadm folder to /tmp

[[email protected] ~]$ mv ~/sysadm /tmp/

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

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

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,

[[email protected] ~]$ cd /tmp/sysadm/
[[email protected] sysadm]$ mv devops.txt linuxadmins.txt

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

[[email protected] sysadm]$ ls *.txt
linuxadmins.txt
[[email protected] sysadm]$

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

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

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

[[email protected] ~]$ mv file1.txt file2.txt file3.txt /tmp/sysadmin

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

[[email protected] ~]$ ls /tmp/sysadmin/file*.txt
/tmp/sysadmin/file1.txt  /tmp/sysadmin/file2.txt  /tmp/sysadmin/file3.txt
[[email protected] ~]$

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,

[[email protected] ~]$ 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,

[[email protected] ~]$ 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
[[email protected] ~]$

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,

[[email protected] ~]$ mv -n tools.txt /tmp/sysadmin/tools.txt
[[email protected] ~]$

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.

[[email protected] ~]$ 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
[[email protected] ~]$

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

Without -f option

[[email protected] ~]$ 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,

[[email protected] ~]$ mv -f bands.txt /tmp/sysadmin/bands.txt
[[email protected] ~]$

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

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

[[email protected] ~]$ 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’
[[email protected] ~]$

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,

[[email protected] ~]$ mv -b buzz55.txt /tmp/sysadmin/buzz55.txt
[[email protected] ~]$ 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~
[[email protected] ~]$

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

[[email protected] ~]$ 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
[[email protected] ~]$

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

[[email protected] ~]$ mv -u tools.txt /tmp/sysadmin/tools.txt
[[email protected] ~]$

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 Reply

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

three × three =