How to Move Files in Linux Using the mv Command

Updated on August 16, 2024
How to Move Files in Linux Using the mv Command header image

Introduction

The mv command in Linux enables you to move files and directories from one location to another. It's also known as move mv and can also be used to rename files and directories in a file system.

This article explains how to use the mv command in Linux to move and rename files.

Syntax of mv Command

Below is the basic mv command syntax:

console
$ mv [options] source destination

Within the above command, source is the target file or directory to move, and destination is the new file or directory location in the file system.

mv Command Options

Option Description
-i Interactive mode. Enables prompts when overwriting existing files.
-f Force mode. Moves files without prompting to overwrite existing files
-n No clobber. Does not overwrite existing files.
-u Update. Moves the file only if the source is newer than the destination or if the destination file is missing.
-v Verbose. Displays the progress of the move command.

Practical Examples of the mv Command

  1. Move a file from your working directory to another directory.

    console
    $ mv file1.txt /home/user/documents/
    

    The above command moves file1.txt from your active working directory to the /home/user/documents/ directory.

    Output:

    move file to different dir

  2. Rename a file.

    console
    $ mv oldname.txt newname.txt
    

    The above command renames oldname.txt to newname.txt in your working directory.

    Output:

    Rename a file

  3. Move multiple files to a directory.

    console
    $ mv file1.txt file2.txt /home/user/documents/
    

    The above command moves file1.txt and file2.txt to the /home/user/documents/ directory.

    Output:

    move multiple files

  4. Move a directory.

    console
    $ mv /home/user/oldfolder /home/user/newfolder
    

    The above command moves the oldfolder directory to /home/user/newfolder.

    Output:

    move a directory

  5. Enable overwriting prompts before replacing an existing file.

    console
    $ mv -i file1.txt /home/user/documents/
    

    The above command displays a confirmation prompt before overwriting file1.txt in the destination directory if the file exists.

    Output:

    Prompt before overwriting

  6. Force move a file without prompting overwrite changes.

    console
    $ mv -f file1.txt /home/user/documents/
    

    The above command moves file1.txt to the destination directory and overwrites the existing file without a confirmation prompt.

    Output:

    Force move

  7. Move files without overwriting the existing files.

    console
    $ mv -n file1.txt file2.txt /home/user/documents/
    

    The above command moves file1.txt and file2.txt only if no files exist with the same name in the destination directory.

    Output:

    Prevent overwriting to existing file

  8. Backup file an existing file with a new name.

    console
    $ mv -b file1.txt file2.txt
    

    The above command renames file1.txt to file2.txt to create a backup of file2.txt with a tilde character (~) appended if the file exists.

    Output:

    Create a backup file

  9. View progress of the move operation.

    console
    $ mv -v file1.txt /home/user/documents/
    

    The above command moves file1.txt and displays the progress of the move operation.

    Output:

    move file to different dir

Handling Special Scenarios

  1. Move files with spaces in the names.

    console
    $ mv "file name with spaces.txt" /home/user/documents/
    

    The above command moves the filename defined with quotes and moves it to the destination directory.

    Output:

    move file with spaces in name

  2. Move hidden files.

    console
    $ mv .hiddenfile /home/user/documents/
    

    The above command moves the .hiddenfile file to the /home/user/documents/ directory.

    Output:

    move hidden file

Advanced Usage Scenarios

  1. Use the mv command in scripts to automatate file management tasks as described in the following steps.

    • Create a new sample script file mv_script.sh using a text editor such as Nano

      console
      $ nano mv_script.sh
      
    • Add the following contents to the file.

      bash
      #!/bin/bash
      for file in *.txt; do
          mv "$file" "${file%.txt}_$(date +%F).txt"
      done
      
    • Enable execute privileges on the file.

      console
      $ chmod +x mv_script.sh
      
    • Run the script

      console
      $ ./mv_script.sh
      

    The above script appends the system date to all .txt files in the working directory.

    Output:

    mv in a script

  2. Move files based on a pattern.

    console
    $ mv *.txt /home/user/textfiles/
    

    The above command uses a wildcard character to move all files with the .txt extension to the /home/user/textfiles/ directory.

    Output:

    move files using wild card

  3. Enable confirmation while moving files.

    console
    $ mv -i *.txt /home/user/documents/
    

    The above command moves all .txt files using interactive mode and enables confirmation prompts before overwriting any existing files in the destination directory.

    Output:

    Move files in interactive mode

  4. Move files based on the age.

    console
    $ find /source_directory -type f -mtime +30 -exec mv {} /destination_directory/ \;
    

    This above command finds and moves all files older than 30 days from /source_directory to /destination_directory.

    Output:

    Move files based on age

Conclusion

You have used the mv command to perform file movement and renaming tasks in Linux. For more command options, run the man mv command to view the mv manual page.