• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

mv command in Linux with Useful Example

Written by Rahul, Updated on December 14, 2020

mv command is used to move files from one directory to other directory. Also mv command is used to rename file in Linux systems. It is an frequently uses command by the Linux users. You must learn about mv command in Linux and its parameters.

In this tutorial you will learn Linux mv command with useful examples

Syntax:

Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...

The move command is useful to rename SOURCE file to DEST file name. Also you can move a SOURCE file to other DIRECTORY. This command also allowed us to move DIRECTORY including all subdirectories to target directory.

mv Command Examples

Below is the basic mv command examples on Linux terminal.

  1. Rename file in current directory – For example rename file source.txt to dest.txt in current directory.
    mv source.txt dest.txt 
    
  2. Move file to destination directory – Move a source.txt file available in current directory to /tmp directory.
    mv source.txt /tmp/ 
    

    In above command the file name is unchanged at target directory. You can also rename file name at destination directory just by providing filename at destination. Like:

    mv source.txt /tmp/dest.txt 
    
  3. Move file from source directory to destination directory – We can also move files or directory from some other directory to destination directory.

    The below command will move /var/dir1/source.log file to /var/log directory with same name.

    mv /var/dir1/source.log /var/log/ 
    
  4. Move multiple files to destination at once – The mv accepts multiple source files and move them to the destination directory at once.

    The following command will move file1.txt, file2.txt in current directory and /opt/file3.txt to the /tmp directory. Here the last command line parameter is used as destination by the mv command.

    mv file1.txt file2.txt /opt/file3.txt /tmp 
    

    You can also use -t option to provide destination directory.

    mv -t /tmp file1.txt file2.txt /opt/file3.txt 
    

mv Command Line Options

You must remember some of the command line options for mv command useful while working on terminal.

  • -b Use this option to create a backup of each existing destination file. This is very useful from unwanted overwritten and data loss. If the destination file already exists it create a copy of destination file with ~ appended in file name.
    mv -b source.txt dest.txt 
    
  • -f, --force Use this option to overwrite destination file without prompting for the confirmation. This is useful for using mv command in automation tasks or shell scripts.
    mv -f source.txt dest.txt 
    
  • -i, --interactive This option is used to always prompt for confirmation before overwriting destination file.
    mv -i source.txt dest.txt 
    
    cp: overwrite 'dest'?
    

    Enter y/n option to allow or deny overwrite request.

  • -t, --target-directory=DIRECTORY Use this option to move multiple files to destination directory.
    mv -t /tmp file1.txt file2.txt /opt/file3.txt  
    

    The above command will move file1.txt, file2.txt and /opt/file3.txt files under the /tmp directory.

  • -T, --no-target-directory If a directory at destination with same name already exists, then default command moves source file under the destination directory. This option instruct mv command to treat destination as file and stop moving source under the destination.
    mv -T source.txt  output 
    
    mv: cannot overwrite directory 'output' with non-directory
    
  • -u, --update – This option tells mv command to move only if SOURCE file is newer than the destination file or the destination file is missing.
    mv -u source.txt dest.txt
    

Conclusion

In this tutorial, you have learned uses of Linux mv command with the useful examples and command line options.

Please do share you thoughts about this article via comments to improve it. Your

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy