Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux mv Command: Move File Or Directory

The mv command is a versatile Linux utility used to move or rename files and directories. In this tutorial, we will discuss how to use the mv command with various options.

Basic Usage

The basic syntax of the mv command is:

mv [options] source destination

Replace options with any desired options, source with the file or directory to be moved or renamed, and destination with the target location or new name.

  1. Move a file or directory:

    To move a file or directory, specify the source and the target directory. For example, to move a file named example.txt to a directory called target_directory, use the following command:

    mv example.txt target_directory
    
  2. Rename a file or directory:

    To rename a file or directory, specify the source and the new name. For example, to rename a file named old_file.txt to new_file.txt, use the following command:

    mv old_file.txt new_file.txt
    

Move/Rename Multiple Files or Directories

You can move or rename multiple files or directories by specifying the sources followed by the target directory. For example, to move file1.txt, file2.txt, and file3.txt to a directory called target_directory, use the following command:

mv file1.txt file2.txt file3.txt target_directory

Overwriting Options

By default, the mv command will overwrite the destination file or directory if it already exists. However, you can modify this behavior using the following options:

  1. -i or --interactive: Prompt before overwriting:

    mv -i source destination
    

    The command will ask for confirmation before overwriting any existing file or directory.

  2. -n or --no-clobber: Do not overwrite an existing file or directory:

    mv -n source destination
    

    The command will not overwrite any existing file or directory.

Verbosity

To display information about the move operation, use the -v or --verbose option. The command will show the source and destination names for each moved item. For example:

mv -v source destination

Conclusion

The mv command is an essential tool for file and directory management on Linux systems. It allows you to move and rename files and directories with ease. By using various options, you can control the overwriting behavior and verbosity of the command. Mastering the mv command is crucial for efficient file organization and management on Linux systems.

  1. How to move files with mv command in Linux: To move files in Linux using mv, provide the source file and the destination directory or file:

    mv file.txt /path/to/destination/
    
  2. Using mv to relocate directories in Linux: Move directories using mv. For example, to move a directory to another location:

    mv my_directory /new/location/
    
  3. Move and rename files with mv in Unix-like systems: Use mv to both move and rename files. For instance, renaming a file during the move operation:

    mv old_name.txt new_name.txt
    
  4. Renaming and moving directories in the Linux terminal: Rename and move directories with mv. For example:

    mv old_directory/ new_name/
    
  5. Advanced options for the mv command in Linux: Explore advanced options with mv. For instance, preserving file attributes during the move:

    mv --preserve=all file.txt /path/to/destination/
    
  6. Moving files between directories using mv: Move files between directories using mv. Specify the source file and the destination directory:

    mv file.txt /path/to/new_directory/
    
  7. Batch moving files with the mv command in Linux: Batch move files using wildcard characters. For example, to move all text files to a specific directory:

    mv *.txt /path/to/destination/