Blog Datasheets Home About me Clients My work Services Contact

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:

1
mv [OPTIONS] SOURCE DEST

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:

1
mv /path/to/source/file /path/to/destination/directory

For example, let’s move a file called example.txt from the current directory to the docs directory:

1
mv example.txt docs/

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:

1
mv old_name.txt new_name.txt

3. Moving Multiple Files

You can move multiple files by providing multiple source paths as arguments, followed by the destination directory:

1
mv file1.txt file2.txt file3.txt destination_directory/

4. Moving Directories

To move a whole directory, recursively including all its subdirectories and files, use the -r or --recursive option:

1
mv -r directory_to_move/ destination_directory/

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:

1
mv -f source_file destination_directory/

6. Interactive Moves

You can enable interactive mode to prompt for confirmation before overwriting files, by using the -i or --interactive option:

1
mv -i source_file destination_directory/

7. Retaining File Metadata

To preserve file metadata such as permissions, timestamps, and ownership, use the -p or --preserve option:

1
mv -p source_file destination_directory/

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:

1
mv -t /path/to/destination_directory source_file

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:

1
mv -v source_file destination_directory/

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!


➡️ MQTT Communication with ESP32 and ESP-IDF


⬅️ Implementing WebSocket Communication on ESP32


Go back to Posts.