Master Linux File Management: Powerful mv Command Tricks & Examples
This guide explains the Linux mv command in depth, covering its syntax, useful options, and practical examples for renaming, moving single or multiple files and directories, interactive overwriting, conditional updates, and creating backups, all illustrated with real command‑line sessions.
mv Overview
The mv command (short for move) is a fundamental Linux utility used to move or rename files and directories. When the target is an existing file, its contents are overwritten by default. If the source is a file and the target a directory, the file is moved into that directory; if both are directories, the source directory is renamed.
Syntax
mv [options] source destinationKey Options
-b: Create a backup of the destination file before overwriting. -f: Force overwrite without prompting. -i: Interactive mode; ask for confirmation before overwriting. -u: Overwrite only when the source is newer than the destination. -t <dir>: Specify the target directory first, then list sources. -S<suffix>: Use a custom suffix for backup files. -n: Never overwrite existing files. -T: Treat the target as a regular file, not a directory. -v: Verbose output showing each move operation.
Practical Examples
Renaming a File or Directory
Command format:
mv source_file target_file # or mv source_dir target_dirEnsure the source and target reside in the same directory; otherwise the operation becomes a move rather than a rename.
# Create test files and directories
[root@CentOS7-1 mv]# ll
total 0
[root@CentOS7-1 mv]# touch mvfiles
[root@CentOS7-1 mv]# mkdir mvdir
[root@CentOS7-1 mv]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 8 09:03 mvdir
-rw-r--r-- 1 root root 0 Jan 8 09:02 mvfiles
# Rename them
[root@CentOS7-1 mv]# mv mvfiles mvfilessssss
[root@CentOS7-1 mv]# mv mvdir mvdirectory
[root@CentOS7-1 mv]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 8 09:03 mvdirectory
-rw-r--r-- 1 root root 0 Jan 8 09:02 mvfilessssssUse -v to see the operation details:
# Verbose rename
[root@CentOS7-1 mv]# mv -v mvfilessssss mvfiles
‘mvfilessssss’ -> ‘mvfiles’
[root@CentOS7-1 mv]# mv -v mvdirectory mvdir
‘mvdirectory’ -> ‘mvdir’Moving Multiple Files or Directories
Command format:
mv source1 source2 ... target_directory
mv *pattern* target_directoryTwo approaches: list each source separated by spaces, or use a wildcard pattern.
# Create sample files and directories
[root@CentOS7-1 mv]# touch 1.txt 2.txt 3.txt
[root@CentOS7-1 mv]# mkdir 1 2 3
[root@CentOS7-1 mv]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 8 09:20 1
-rw-r--r-- 1 root root 0 Jan 8 09:19 1.txt
... (similar entries for 2 and 3)
# Move them to /root/mv1/
[root@CentOS7-1 mv]# mv -v 1.txt 2.txt 3.txt /root/mv1/
‘1.txt’ -> ‘/root/mv1/1.txt’
‘2.txt’ -> ‘/root/mv1/2.txt’
‘3.txt’ -> ‘/root/mv1/3.txt’To avoid overwriting existing files, add -n:
# Prevent overwriting
[root@CentOS7-1 mv]# mv -nv test.txt /root/mv1/Interactive Overwrite
Add -i to prompt before overwriting:
# Interactive move
[root@CentOS7-1 mv]# mv test.txt -v -i /root/mv1/
mv: overwrite ‘/root/mv1/test.txt’? y
‘test.txt’ -> ‘/root/mv1/test.txt’Overwrite Only When Source Is Newer (-u)
The -u option updates the destination only if the source file is newer.
# Attempt to overwrite older file (will not replace)
[root@CentOS7-1 mv]# mv -v -u /root/mv1/test.txt ./
# No change because destination is newer
# Overwrite when source is newer
[root@CentOS7-1 mv]# mv -v -u ./test.txt /root/mv1/
‘./test.txt’ -> ‘/root/mv1/test.txt’Creating Backups Before Overwrite
Use -b to keep a backup of the overwritten file. The default backup suffix is ~, but you can specify a custom suffix with --suffix=.
# Backup example
[root@CentOS7-1 mv]# mv -v -b test1.txt test2.txt
mv: overwrite ‘test2.txt’? y
‘test1.txt’ -> ‘test2.txt’ (backup: ‘test2.txt~’)
# Custom suffix
[root@CentOS7-1 mv]# mv -v -b --suffix=.bak test2.txt test3.txt
mv: overwrite ‘test3.txt’? y
‘test2.txt’ -> ‘test3.txt’ (backup: ‘test3.txt.bak’)Conclusion
The mv command offers versatile file‑handling capabilities, from simple renames to bulk moves, interactive confirmations, conditional updates, and automatic backups. Mastering its options helps prevent accidental data loss and streamlines everyday Linux file management tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
