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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
mv command in Linux Basic and Advanced Examples
- 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.isofile from our
Downloadsdirectory to the
Desktopdirectory. 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.isofile will now be in the
- As mentioned earlier, we can also use the mv command to rename files. In this example, we will rename the
ubuntu.iso. This example assumes that our present working directory already contains the
linux.isofile, so we can use relative paths here.
$ mv linux.iso ubuntu.iso
- 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
- By default, the mv command will overwrite the destination file if it already exists. You can override this behavior with the
-ioption. 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'?
- Use the
-noption to instruct the mv command to never overwrite an existing destination file.
$ mv -n ~/Downloads/linux.iso ~/Desktop
- 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
-voption 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.
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
- 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
debian.isowere both moved to the
-uoption 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
- If a destination file already exists, you can instruct the mv command to move it anyway and rename the existing file by using the
-boption. This way, you can move a file even if the destination file already exists.
$ mv -b ~/Downloads/linux.iso ~/Desktop
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.