Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to move a file, group of files, and directories in Linux?
The mv (move) command is used to move one or more files or directories from one location to another in Linux/Unix systems. Unlike copying, the mv command transfers files by removing them from the source and placing them at the destination. The mv command also serves as a rename tool when moving files within the same directory.
Syntax
The general syntax of the mv command follows these patterns −
mv [OPTION]... [-T] SOURCE DESTINATION mv [OPTION]... SOURCE... DIRECTORY mv [OPTION]... -t DIRECTORY SOURCE...
Command Options
| Option | Description |
|---|---|
--backup[=CONTROL] |
Create backup of destination file before overwriting |
-b |
Create backup file without argument |
-f, --force |
Do not prompt before overwriting existing files |
-i, --interactive |
Prompt before overwriting files |
-n, --no-clobber |
Do not overwrite an existing file |
-t, --target-directory=DIR |
Move all source arguments into specified directory |
-T, --no-target-directory |
Treat destination as a normal file, not directory |
-u, --update |
Move only when source is newer than destination |
-v, --verbose |
Show what is being done |
--help |
Display help message and exit |
--version |
Show version information and exit |
Moving a Single File
To move a file from the current directory to another directory −
mv file.txt ../documents/
This moves file.txt from the current directory to the documents directory located one level up.
Moving Multiple Files
To move multiple files to a destination directory −
mv file1.txt file2.txt file3.txt /home/user/backup/
You can also use wildcards to move groups of files −
mv *.txt /home/user/documents/ mv report*.pdf /home/user/reports/
Moving Directories
To move an entire directory to another location −
mv source_directory destination_directory
Example moving a directory called projects to backup folder −
mv projects /home/user/backup/
Renaming Files and Directories
The mv command can rename files by moving them within the same directory −
mv oldname.txt newname.txt mv old_directory new_directory
Interactive Mode Example
Using the -i option prompts before overwriting existing files −
mv -i file.txt /home/user/documents/
mv: overwrite '/home/user/documents/file.txt'? y
Creating Backup Files
When moving files that might overwrite existing ones, create backups −
mv --backup file.txt /home/user/documents/
This creates a backup file (usually with a ~ suffix) if a file with the same name exists at the destination.
Verbose Output
Use the -v option to see what the mv command is doing −
mv -v *.txt /home/user/documents/
'file1.txt' -> '/home/user/documents/file1.txt' 'file2.txt' -> '/home/user/documents/file2.txt' 'file3.txt' -> '/home/user/documents/file3.txt'
Common Use Cases
Organizing files − Moving files to appropriate directories for better organization
Renaming − Changing file or directory names without creating copies
Archiving − Moving old files to backup or archive directories
Cleanup − Moving temporary files to designated locations
Conclusion
The mv command is a fundamental Linux tool for moving and renaming files and directories efficiently. It provides various options for safe operations, including interactive prompts and backup creation. Understanding mv is essential for effective file management in Linux systems.
