The mv command under Linux: examples of use
Table of Contents
- I. Introduction
- II. Using the mv command
- B. Moving a folder to another folder
- C. Moving multiple files to a folder
- D. Move multiple folders to a single folder
- E. Move the entire contents of a folder
- F. Move a file if the source is more recent
- G. Move all files in a folder except a specific file
- H. Rename a file or folder
- I. Request confirmation with mv command
- J. mv command: do not overwrite existing files
- III. Conclusion
I. Introduction
Under Linux, the "mv" command, which stands for "move", is used to move items (files and folders) from a source to a destination. This command is also used to rename an item. To make a copy of a file or folder, use the cp command.
It's a very handy command, and I'd even go so far as to say that it's one of the must-have commands in Linux! It's a native command on all distributions, whether Debian, Ubuntu, Rocky Linux, etc.... That's why I've decided to present it to you in this article, using various examples. For my part, I'm working on a Debian machine.
II. Using the mv command
A. Move a file to another folder
Since the mv command is used to move items, let's start by moving a file to another folder. In other words, it's like cutting and pasting. We start by creating a file named"it-connect.txt" in"/home/flo", which will serve as a guinea pig.
cd /home/flo touch it-connect.txt
To move the it-connect.txt file to the "/tmp" directory, run :
mv it-connect.txt /tmp/
We can also specify the complete path:
mv /home/flo/it-connect.txt /tmp/

You can see that it works perfectly:

B. Moving a folder to another folder
The mv command can also move complete folders, i.e. the folder with its contents. Still in"/home/flo", we create a directory named"data", then a file named"it-connect.txt" inside.
mkdir data touch data/it-connect.txt
We then move this directory to /tmp/ :
mv /home/flo/data/ /tmp/
As you can see, the syntax isn't much more complicated. Since it's a folder, there's a "/" at the end of the source path.
C. Moving multiple files to a folder
Rather than using the mv command several times, you can move several files at once. Let's start by creating 3 files:
touch it-connect1.txt it-connect2.txt it-connect3.txt
Then, we can move these three files to "/tmp/" like this:
mv it-connect1.txt it-connect2.txt it-connect3.txt /tmp/
To make this quicker to enter, we can use the wildcard character ("*") to grab all files matching this filter. Thus, with the command below, we target all elements whose name begins with "it-connect".
mv it-connect* /tmp/
D. Move multiple folders to a single folder
In the same spirit, you can move several folders to another folder. Two folders can be created with mkdir :
mkdir data1 data2
Then, we move these two folders to the "/tmp/" directory:
mv data1/ data2/ /tmp/
E. Move the entire contents of a folder
Sometimes you want to move the entire contents of a folder, i.e. all the files it contains, as well as all the sub-folders and their files, without touching the source directory.
In this example, we create a folder called"data1", which contains a file and a subfolder, which itself contains a file. The aim is to move the contents of"data1" to"/tmp/".
mkdir data1 mkdir data1/photos touch data1/it-connect.txt touch data1/photos/logo.png
When moving content with the mv command, don't simply specify"data1/", otherwise you'll move the folder itself. Here, we specify an asterisk after the "/" to specify that it's the contents we wish to move, then indicate the target directory.
mv data1/* /tmp/
In this case, the new path for the"photos" directory will be"/tmp/photos".
F. Move a file if the source is more recent
The mv command contains some pretty cool commands... Let's take a look at a few of them. For example, the "-u" option moves a file only if the source file is newer than the destination file. Of course, this applies if the destination file already exists.
Here's an example:
mv -u /home/flo/it-connect.txt /tmp/it-connect.txt
G. Move all files in a folder except a specific file
Before looking at other options, let's look at a very special case: we want to move the entire contents of a directory, with the exception of a single file. The mv command has no options for exclusion, but we can trick you into using another command... Several solutions are possible. Here's an example based on the ls command.
Three files are created:
touch it-connect1.txt it-connect2.txt it-connect3.txt
Now we're going to move everything except the"it-connect2.txt" file! Which gives:
mv $(ls --ignore=it-connect2.txt)
In this way, all content will be copied except this file! Other exclusions can be added one after the other:
mv $(ls --ignore=it-connect2.txt --ignore=it-connect1.txt) /tmp/

H. Rename a file or folder
Let's see how to rename an item with the mv command, whether it's a file or a folder. Let's start by creating a :
touch it-connect.txt
We decide to rename the file"it-connect.txt" to"it-connect.txt.old". It's as if we were moving the file to the same location, but changing its name.
mv it-connect.txt it-connect.txt.old
Here's an example:

For a folder, it's the same principle, except that the "/" appears at the end of the name, as in the previous cases.
mkdir data2021 mv data2021/ data2022/
I. Request confirmation with mv command
If there is a conflict, i.e. if the destination file already exists, the mv command doesn't bother: it overwrites the destination file. However, you can add the "-i" option to display a confirmation prompt in the event of a conflict.
The same file is created in two different locations:
touch /tmp/it-connect.txt touch /home/flo/it-connect.txt
We then move the file from one location to another, adding the "-i" option.
mv -i /home/flo/it-connect.txt /tmp/it-connect.txt
The mv command indicates a message and asks us to make a choice:
mv: do you want to overwrite '/tmp/it-connect.txt'?
Phew! I was close to deleting a critical file...! Automatic overwriting can be handy, in a script for example, but it can also be dangerous, so this option is worth knowing.

J. mv command: do not overwrite existing files
In the event of a conflict, you can ensure that the destination file is not overwritten, without having to specify this manually. This reverses the default mode of operation. Simply add the "-n" option:
mv -n /home/flo/it-connect.txt /tmp/it-connect.txt
In the event of a conflict, the operation will be cancelled. No information will be specified in the console.
III. Conclusion
With this tutorial, you'll have enough examples to master the mv command under Linux and move your files and folders!