Fundamentals 8 min read

Unlock the Full Power of Linux mv: Advanced Options You Should Know

This guide explains both basic and advanced usages of the Linux mv command, covering options like -v for verbose output, -i for interactive prompts, -n to prevent overwriting, -u to update only newer files, and -b to create backups before replacement.

ITPUB
ITPUB
ITPUB
Unlock the Full Power of Linux mv: Advanced Options You Should Know

The mv command is one of the most frequently used utilities in Linux; beyond its basic functions, several options can make file moving safer and more informative.

1. Basic usage

Move one or multiple files.

Move one or multiple directories.

Rename files or directories.

These are the core operations of mv. The following sections introduce more advanced options.

2. Print operation details (-v)

Adding -v makes mv output each move, which is handy when handling many files.

[alvin@VM_0_16_centos mv_test]$ mv -v *.txt /home/alvin/test/mv_test/des/
‘file1.txt’ -> ‘/home/alvin/test/mv_test/des/file1.txt’
‘file2.txt’ -> ‘/home/alvin/test/mv_test/des/file2.txt’
‘file3.txt’ -> ‘/home/alvin/test/mv_test/des/file3.txt’
‘file4.txt’ -> ‘/home/alvin/test/mv_test/des/file4.txt’
‘file5.txt’ -> ‘/home/alvin/test/mv_test/des/file5.txt’

3. Interactive mode (-i)

With -i, mv prompts before overwriting an existing file, allowing you to confirm with y.

[alvin@VM_0_16_centos mv_test]$ mv -i file1.txt /home/alvin/test/mv_test/des/
mv: overwrite ‘/home/alvin/test/mv_test/des/file1.txt’? y

4. Never overwrite (-n)

The -n flag prevents mv from overwriting files that already exist at the destination.

[alvin@VM_0_16_centos mv_test]$ ll *.txt des/*.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file3.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 des/file1.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 des/file2.txt
[alvin@VM_0_16_centos mv_test]$ mv -nv *.txt /home/alvin/test/mv_test/des/
‘file3.txt’ -> ‘/home/alvin/test/mv_test/des/file3.txt’    # target does not have file3.txt, so it is moved
[alvin@VM_0_16_centos mv_test]$ ls
des  file2.txt

5. Update only newer files (-u)

Using -u, mv replaces a destination file only when the source file has a newer timestamp.

[alvin@VM_0_16_centos mv_test]$ ll *.txt des/*.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file1.txt    # source newer than destination
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file2.txt    # source older than destination
- rw-rw-r-- 1 alvin alvin 0 Feb 8 16:53 des/file1.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 des/file2.txt
[alvin@VM_0_16_centos mv_test]$ mv -uv *.txt /home/alvin/test/mv_test/des/
‘file1.txt’ -> ‘/home/alvin/test/mv_test/des/file1.txt’    # only the newer file is replaced
[alvin@VM_0_16_centos mv_test]$ ls
des  file2.txt

6. Backup before overwriting (-b)

The -b option creates a backup of the existing destination file (appending a tilde ~) before overwriting.

[alvin@VM_0_16_centos mv_test]$ mv -bv *.txt /home/alvin/test/mv_test/des/
‘file1.txt’ -> ‘/home/alvin/test/mv_test/des/file1.txt’ (backup: ‘/home/alvin/test/mv_test/des/file1.txt~’)
‘file2.txt’ -> ‘/home/alvin/test/mv_test/des/file2.txt’ (backup: ‘/home/alvin/test/mv_test/des/file2.txt~’)
[alvin@VM_0_16_centos mv_test]$ ll des/
total 0
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:41 file1.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:26 file1.txt~
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 file2.txt
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:27 file2.txt~
- rw-rw-r-- 1 alvin alvin 0 Feb 8 17:35 file3.txt

By combining these options, you can make mv safer, more transparent, and better suited to batch operations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxfile managementcommand-linemv
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.