mv command to move files and folders

mv command to move files and folders

I. Presentation

Under Linux, the command “mv” which means “move” is used to move elements (files and folders) from a source to a destination. This command is also used to rename an element. To make a copy of a file or folder, use the command cp.

It is a very practical command and I would even say that it is one of the essential commands under Linux! It is a native command on all distributions, whether on Debian, Ubuntu, Rocky Linux, etc…. Hence the interest of presenting it to you in this article through various examples. For my part, I manipulate on a machine under Debian 11.

Article initial version: July 11, 2012

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 doing a “cut and paste”. We start by creating a file named “it-connect.txt” in “/home/flo” and who 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 full path:

mv /home/flo/it-connect.txt /tmp/

mv command - move folders and files in Linux

We can see that it works perfectly:

Linux - mv command

B. Move a folder to another folder

The mv command can also move complete folders, ie the folder with its contents. Always in “/home/flo“, we create a directory named “data“, then a file “it-connect.txt” on the inside.

mkdir data
touch data/it-connect.txt

Then, we move this directory to /tmp/:

mv /home/flo/data/ /tmp/

You see, the syntax is not more complicated. Since it is a folder, there is a “/” at the end of the source path.

C. Move multiple files to a folder

Rather than chaining 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 it faster to type, you can use the wildcard character (“*”) to get all files matching this filter. Thus, thanks to the command below, we target all the elements whose name begins with “it-connect”.

mv it-connect1.txt it-connect2.txt it-connect3.txt /tmp/

D. Move multiple folders to another folder

In the same spirit, you can move several folders to another folder. You can create two folders with mkdir:

mkdir data1 data2

Then, we move these two folders to the “/tmp/” directory:

mv data1/ data2/ /tmp/

E. Move all contents of a folder

Sometimes, we want to move all the contents of a folder, that is to say 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 named “data1“, which contains a file and a subfolder, which itself contains a file. The goal is to move the contents of “data1” towards “/tmp/“.

mkdir data1
mkdir data1/photos
touch data1/it-connect.txt
touch data1/photos/logo.png

When moving the content with the mv command, one does not simply specify “data1/“, otherwise we will move the folder itself. Here, we specify an asterisk after the “/” to specify that it is the content that we want to move, then we indicate the target directory.

mv data1/* /tmp/

In this case, the new directory path “photos” sera “/tmp/photos“.

F. Move a file if the source is newer

The mv command contains some pretty cool commands… We’ll use 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 in case the destination file already exists.

Here is 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 seeing other options, let’s look at a very special case: we want to move all the contents of a directory, except for a single file. The mv command does not have options to make an exclusion, but one can trick using another command… Several solutions are possible. Here is an example based on the command ls.

We create three files:

touch it-connect1.txt it-connect2.txt it-connect3.txt

Now we will move everything except the file “it-connect2.txt” ! Which give :

mv $(ls --ignore=it-connect2.txt)

Thus, all content will be copied except this file! You can add other exclusions one after the other:

mv $(ls --ignore=it-connect2.txt --ignore=it-connect1.txt) /tmp/

mv command with exclusion on a file

H. Renaming a File or Folder

let’s see how to rename an element with the mv command, either a file or a folder. Let’s start by creating a file:

touch it-connect.txt

We decide to rename the file “it-connect.txt” in “it-connect.txt.old“. It’s like moving the file to the same place, changing its name.

mv it-connect.txt it-connect.txt.old

Picture example:

Rename with mv command

For a folder, it’s the same principle except that we find the “/” at the end of the name as for the previous cases.

mkdir data2021
mv data2021/ data2022/

I. Confirmation request with the mv command

In the case where there is a conflict, that is to say that the destination file already exists, the mv command does not pose a question: it overwrites the destination file. Nevertheless, one can add the “-i” option so that a confirmation request is displayed 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

Then we 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 shows a message and asks us to make a choice:

mv : voulez-vous écraser '/tmp/it-connect.txt' ?

Phew! I was close to deleting a critical file…! Automatically overwriting can be handy, in a script for example, but it can also be dangerous so this option is something to be aware of.

mv command - confirmation request

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 it manually. Thus, the default operating mode is reversed. Just 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

Thanks to this tutorial, you have enough examples to master the mv command in Linux and move your files and folders!

Commandes et Système,Bash,CLI,Linux,Shell,

#command #move #files #folders

Leave a Comment

Your email address will not be published. Required fields are marked *