G2Labs Grzegorz Grzęda
Linux 'mv' command
May 21, 2024
Mastering the Linux ‘mv’ Command: A Comprehensive Guide
The mv
command in Linux is a powerful utility that allows you to move or rename files and directories with ease. Whether you’re a seasoned Linux user or a beginner, understanding and utilizing the full potential of mv
can greatly enhance your productivity. In this blog post, we’ll explore the various use cases of the mv
command, providing you with extensive examples to help you become a ‘mv’ command master.
Basic Usage
The basic syntax of the mv
command is as follows:
|
|
Where SOURCE
represents the file or directory you want to move or rename, and DEST
indicates the destination path or new filename.
Examples:
1. Moving Files
To move a file from one directory to another:
|
|
For example, let’s move a file called example.txt
from the current directory to the docs
directory:
|
|
2. Renaming Files
To rename a file, you can use the mv
command by specifying the new name as the DEST
argument. For instance, let’s rename old_name.txt
to new_name.txt
:
|
|
3. Moving Multiple Files
You can move multiple files by providing multiple source paths as arguments, followed by the destination directory:
|
|
4. Moving Directories
To move a whole directory, recursively including all its subdirectories and files, use the -r
or --recursive
option:
|
|
5. Overwriting Existing Files
By default, mv
will not overwrite files with the same name in the destination directory. However, if you want to override existing files without being prompted for confirmation, add the -f
or --force
option:
|
|
6. Interactive Moves
You can enable interactive mode to prompt for confirmation before overwriting files, by using the -i
or --interactive
option:
|
|
7. Retaining File Metadata
To preserve file metadata such as permissions, timestamps, and ownership, use the -p
or --preserve
option:
|
|
8. Moving With Parent Directories
If you want to move a file while also creating any necessary parent directories (if they don’t exist), use the -t
or --target-directory
option with the full destination path:
|
|
This ensures that the file is moved to the desired location while maintaining the directory structure.
9. Verbose Output
For more detailed information during the move operation, you can utilize the -v
or --verbose
option:
|
|
This option will show you each file as it’s moved.
Conclusion
The mv
command offers incredible flexibility for file and directory manipulation in Linux. Understanding its diverse set of options will enable you to efficiently move or rename files with ease. We’ve covered essential examples and use cases, but there’s much more to explore. Feel free to refer to the mv
command’s manual (man mv
) for further details and additional options.
Now that you’re armed with this knowledge, go forth and master the power of the mv
command in Linux!