The mv
command stands for “move.” It is used to move or rename files and directories in the file system. The mv
command can transfer files to a new location or rename them without changing their location.
Initial Example
Using mv
to rename a file from oldname.txt
to newname.txt
:
mv oldname.txt newname.txt
MV Parameters
Parameter | Description |
---|---|
--backup[=CONTROL] | Create a backup for each existing destination file |
-b | Similar to --backup but does not accept any argument |
--debug | Provide detailed information about how files are being moved |
--exchange | Swap the source and destination files or directories |
-f, --force | Move files without prompting for confirmation, even if overwriting |
-i, --interactive | Ask for confirmation before overwriting existing files |
-n, --no-clobber | Do not overwrite any existing files |
--no-copy | Do not copy files if renaming fails |
--strip-trailing-slashes | Remove trailing slashes from each source argument |
-S, --suffix=SUFFIX | Use the specified suffix for backup files |
-t, --target-directory=DIRECTORY | Move all source files into the specified directory |
-T, --no-target-directory | Treat the destination as a normal file, not a directory |
--update[=UPDATE] | Update only files that are newer than the destination or when the destination file is missing |
-u | Equivalent to --update=older |
-v, --verbose | Provide detailed information about the move process |
-Z, --context | Set SELinux security context of the destination file to the default type |
--help | Show help information and exit |
--version | Display version information and exit |
Examples
1. Move a File to a Different Directory
To move a file to another directory:
mv file1.txt /path/to/destination/
2. Move Multiple Files to a Directory
To move multiple files to a directory, specify the files followed by the destination directory:
mv file1.txt file2.txt file3.txt /path/to/destination/
3. Rename a Directory
To rename a directory:
mv old_directory_name new_directory_name
4. Overwrite Without Prompting
To forcefully move or rename files without prompting for confirmation, use the -f
option:
mv -f file1.txt /path/to/destination/
5. Interactive Move
To prompt for confirmation before overwriting existing files, use the -i
option:
mv -i file1.txt /path/to/destination/
6. Verbose Move
To display detailed information about the move process, use the -v
option:
mv -v file1.txt /path/to/destination/