The mv (move) command is used to move or rename files and directories in a Linux filesystem.
-
Syntax:
mv [options] source destination -
Examples:
mv file.txt /backup/
- Moves
file.txtto the/backup/directory.
mv file.txt newfile.txt
- Renames
file.txttonewfile.txt.
mv /documents /backup/
- Moves the
/documents/directory to/backup/.
mv -i file.txt /backup/
- Prompts for confirmation before overwriting any existing files in
/backup/.
mv -v file.txt /backup/
- Verbosely shows what is being moved.
- Moves
-
-ior--interactive:- Prompts before overwriting files.
mv -i file.txt /backup/
- Asks for confirmation before overwriting
file.txtin/backup/.
-
-for--force:- Forces overwriting of existing files without prompting.
mv -f file.txt /backup/
- Overwrites
file.txtin/backup/without confirmation.
-
-vor--verbose:- Displays what is being moved or renamed.
mv -v file.txt /backup/
- Shows that
file.txtis being moved to/backup/.
-
-uor--update:- Moves only when the source file is newer than the destination file or when the destination file is missing.
mv -u file.txt /backup/
- Updates
file.txtin/backup/only if it is newer than the existing file.
-
--help:- Displays help information about the
mvcommand.
mv --help
- Shows usage information and options.
- Displays help information about the
-
Renaming Files or Directories:
- The
mvcommand can be used to rename files or directories by specifying a new name as the destination.
mv oldname.txt newname.txt
- The
-
Moving Multiple Files:
- To move multiple files to a directory, list the files followed by the destination directory.
mv file1.txt file2.txt /backup/
-
Wildcard Usage:
- Use wildcards to move multiple files that match a pattern.
mv *.txt /backup/- Moves all
.txtfiles to the/backup/directory.
-
No Undo:
- The
mvcommand does not create copies; it moves the file or directory. Be cautious, as there is no undo.
- The
The mv command is essential for moving and renaming files and directories in Linux. It provides options to control overwriting, prompting, and verbosity. Use it carefully, especially when moving important files, as there is no undo for a move operation.