Linux – mv Command

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.



Tutorials dojo strip

Initial Example

Using mv to rename a file from oldname.txt to newname.txt:

mv oldname.txt newname.txt




MV Parameters

ParameterDescription
--backup[=CONTROL]Create a backup for each existing destination file
-bSimilar to --backup but does not accept any argument
--debugProvide detailed information about how files are being moved
--exchangeSwap the source and destination files or directories
-f, --forceMove files without prompting for confirmation, even if overwriting
-i, --interactiveAsk for confirmation before overwriting existing files
-n, --no-clobberDo not overwrite any existing files
--no-copyDo not copy files if renaming fails
--strip-trailing-slashesRemove trailing slashes from each source argument
-S, --suffix=SUFFIXUse the specified suffix for backup files
-t, --target-directory=DIRECTORYMove all source files into the specified directory
-T, --no-target-directoryTreat 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
-uEquivalent to --update=older
-v, --verboseProvide detailed information about the move process
-Z, --contextSet SELinux security context of the destination file to the default type
--helpShow help information and exit
--versionDisplay 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/




Linux Playground

Scroll to Top