mv command in Linux with examples

The mv command in Linux is short for move. As you can probably guess or may already know, the command is used to move files and directories from one location to the other. Users that are more accustomed to a GUI might know this action better as “cutting and pasting.” The mv command is just the command line equivalent of that action.

Navigating around the Linux command line and being able to move files or directories is extremely essential when trying to master the command line. In a desktop environment, these things are more intuitive, but the command line requires that we learn a bit about how to get around. After reading this tutorial, you will have a clear understanding of how to move files and will feel much more comfortable on the command line – it’s actually a lot more efficient than a GUI.

As there is typically not any dedicated rename utility on the Linux command line by default, the mv command is also used to rename files. Most of the time, the mv command is very simple and doesn’t require that you learn any complicated syntax. However, this command is very old and used very frequently, so there are a lot of different options that you can use with it which will come in handy in certain situations.

In this tutorial, you’ll learn how to use the mv command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the mv command on Linux
mv command in Linux with examples
mv command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software mv
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The mv command in Linux is used to move or rename files and directories. Follow along with our examples below to learn about some of the most common and useful options to use with the mv command in Linux.

Various examples of the mv command in Linux
Various examples of the mv command in Linux

mv command in Linux Basic and Advanced Examples

  1. To use the mv command in Linux, you just need to specify the file you wish to move and the path to where you are moving it. You can use either absolute paths or relative paths (or both at the same time) with the command. For example, we will move the linux.iso file from our Downloads directory to the Desktop directory. Notice here we are using absolute paths, so this command would work from anywhere in the system.
    $ mv /home/linuxconfig/Downloads/linux.iso /home/linuxconfig/Desktop

    The end result is that the linux.iso file will now be in the /home/linuxconfig/Desktop directory.

  2. As mentioned earlier, we can also use the mv command to rename files. In this example, we will rename the linux.iso file to ubuntu.iso. This example assumes that our present working directory already contains the linux.iso file, so we can use relative paths here.
    $ mv linux.iso ubuntu.iso
  3. We could have also moved the file and renamed it at the same time. All we need to do is specify a new file name in the destination part of our mv command.
    $ mv ~/Downloads/linux.iso ~/Desktop/ubuntu.iso
  4. By default, the mv command will overwrite the destination file if it already exists. You can override this behavior with the -i option. Using this option will make the mv command prompt you for confirmation before overwriting a file.
    $ mv -i ~/Downloads/linux.iso ~/Desktop
    mv: overwrite 'linux.iso'?
  5. Use the -n option to instruct the mv command to never overwrite an existing destination file.
    $ mv -n ~/Downloads/linux.iso ~/Desktop
  6. By default, the mv command doesn’t output any information if it was able to successfully move or rename the file. You can change this with the -v option which will show you exactly what it has done.
    $ mv -v linux.iso ~/Desktop/
    renamed 'linux.iso' -> '/home/linuxconfig/Desktop/linux.iso' 
You can always use the man command to read more about the mv command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

In the section below, we’ve collected a few of the more advanced options that you can use with the mv command in Linux. Make no mistake, mv is pretty simple as far as Linux commands go. However, some of the options below are not as frequently used but still fill a certain niche and are handy to know.

mv command in Linux Advanced Examples

  1. You can move multiple files at once with mv. Just specify as many files as you want, followed by the destination directory.
    $ mv ubuntu.iso debian.iso ~/linuxfiles/

    In this example, the ubuntu.iso and debian.iso were both moved to the ~/linuxfiles directory.

  2. The -u option is not very common but can be very useful in some scenarios. With this option, the mv command will only move a file if the destination file is older than the source file or if the destination file does not exist.
    $ mv -u ~/Downloads/linux.iso ~/Desktop
  3. If a destination file already exists, you can instruct the mv command to move it anyway and rename the existing file by using the -b option. This way, you can move a file even if the destination file already exists.
    $ mv -b ~/Downloads/linux.iso ~/Desktop

Closing Thoughts

In this tutorial, we learned all about the mv command on Linux. The mv command is an essential beginner command to learn as you start your journey into the Linux terminal. As you’ve seen in this tutorial, the command is quite simple but comes packed with lots of options. What we’ve taught you here will allow you to comfortably move and rename any files or directories on your Linux system.