mv command in Ubuntu
Commands

Using mv Command in Ubuntu

Abhishek
Abhishek

Table of Contents

The mv command is used for moving a file from one location to another in Ubuntu terminal. It's like a cut-paste operation as the original file moves to another location.

It can also be used to rename files if you move them to the same location but with a different name.

You can use the mv command to move and rename folders as well.

I'll show you all the important stuff about the mv command in this guide.

Using mv command in Ubuntu

The mv command has a simple syntax:

mv [options] source_file target_file_location

It is mandatory to have another filename or directory location otherwise you'll see "mv: missing destination file operand after" error.

Let's see some practical examples of this mv command in Ubuntu Linux.

1. Move files from one location to another

The actual intended use for the mv command in Ubuntu is to move a file from one place to another. This is similar to cut-paste operation.

mv filename other_directory

You won't see any output that tells you if the file was moved or not. You'll have to check the target directory location.

Here's an example. I move the sample.txt file to the my_dir from the current directory. And as you can see, the file is no longer in the current directory as it is moved to the my_dir location.

abhi@learnubuntu:~$ ls -l
total 12
drwxrwxr-x 2 abhi abhi 4096 Jul  5 04:53 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
-rw-rw-r-- 1 abhi abhi  348 Jul  5 04:52 sample.txt
abhi@learnubuntu:~$ mv sample.txt my_dir
abhi@learnubuntu:~$ ls -l
total 8
drwxrwxr-x 2 abhi abhi 4096 Jul  5 04:53 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
abhi@learnubuntu:~$ ls -l my_dir
total 4
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 sample.txt
mv command in Ubuntu Linux

2. Move multiple files simultaneously

If you have multiple files to move to the same destination, you don't need to do it one by one. You can move them all in a single command.

mv file1 file2 file3 destination_directory

Take a look at this example:

abhi@learnubuntu:~$ mv file1.txt file2.txt file3.txt my_dir
abhi@learnubuntu:~$ ls -l
total 8
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:10 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
abhi@learnubuntu:~$ ls -l my_dir
total 12
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 file1.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file2.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file3.txt
Command Line Tutorial: Learn The Command Line | Codecademy
Learn the command line and discover the power of this simple, yet essential master file system to increase your productivity as a developer.

3. Move a directory from one place to another

There is no separate command for moving directories. You can use the same mv command to move a directory from one place to another with all its contents.

mv dir another_directory

The above command will move the dir directory to another_directory location. Not very clear? Let me show with a real example.

I have a directory with a few files in it:

abhi@learnubuntu:~$ ls -l my_dir
total 12
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 file1.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file2.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file3.txt

I move this directory under another directory:

abhi@learnubuntu:~$ mv my_dir new_dir

Now, you'll see the my_dir under the new_dir:

abhi@learnubuntu:~$ ls -l new_dir
total 24
drwxrwxr-x 2 abhi abhi  4096 Jul  5 06:10 my_dir
-rw-r--r-- 1 abhi root 12813 May 30 07:30 new.txt
drwxr-xr-x 2 abhi root  4096 May 30 07:30 one_more_dir

The contents of the directory remain the same with all their file permissions and timestamps:

abhi@learnubuntu:~$ ls -l new_dir/my_dir/
total 12
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 file1.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file2.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:09 file3.txt

4. Rename a file with the mv command

Yes, you can even rename the files with mv command in Ubuntu. I don't know if it was the real intent behind the mv command or just an unintended use.

To rename a file, you have to move the file to the same location but with a different name.

Here's what you do. The second argument of the command is the new file name instead of the directory location.

mv old_filename new_filename

Here's an example. I have a file named sample.txt. I rename it to new_sample.txt.

abhi@learnubuntu:~$ ls -l
total 12
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:01 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
-rw-rw-r-- 1 abhi abhi  348 Jul  5 04:52 sample.txt
abhi@learnubuntu:~$ mv sample.txt new_sample.txt
abhi@learnubuntu:~$ ls -l
total 12
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:01 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
-rw-rw-r-- 1 abhi abhi  348 Jul  5 04:52 new_sample.txt

Actually, you can rename a file and move to another location at the same time.

mv old_filename another_directory/new_filename

An example for you:

abhi@learnubuntu:~$ mv new_sample.txt my_dir/same_sample.txt
abhi@learnubuntu:~$ ls -l
total 8
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:06 my_dir
drwxr-xr-x 3 abhi root 4096 May 30 07:30 new_dir
abhi@learnubuntu:~$ ls -l my_dir
total 4
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 same_sample.txt

5. Rename a directory with mv command

Similarly, you can also rename directories in Ubuntu. The directory contents remain the same while the name gets changed.

mv old_dir new_dir

In this example, I have two directories. I am going to rename the new_dir to your_dir:

abhi@learnubuntu:~$ ls -l 
total 8
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:10 my_dir
drwxr-xr-x 3 abhi root 4096 Jul  5 06:15 new_dir
abhi@learnubuntu:~$ mv my_dir your_dir
abhi@learnubuntu:~$ ls -l
total 8
drwxr-xr-x 3 abhi root 4096 Jul  5 06:15 new_dir
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:10 your_dir

6. Don't override files while moving them

If you are moving a file to another location but a file with the same name already exists, it gets overridden.

This is not the ideal case all the time because the existing file content will be lost.

If you want to avoid overriding, use the option n (no override):

mv -n filename target_dir

Now if you have another file with the same name in the target directory, the move operation doesn't take place. Both source and target files remain as it is.

abhi@learnubuntu:~$ ls
file1.txt  my_dir  new_dir
abhi@learnubuntu:~$ ls my_dir
file1.txt  file2.txt  file3.txt
abhi@learnubuntu:~$ mv -n file1.txt my_dir
abhi@learnubuntu:~$ ls my_dir
file1.txt  file2.txt  file3.txt
abhi@learnubuntu:~$ ls
file1.txt  my_dir  new_dir

The only thing is that it doesn't display any output that indicates that the file was not moved.

7. Override the older file with mv command

There is another way to handle the files with the same name in the target directory.

Use the update option -u.

What does it do? If the file which is being moved is newer than the one in the target directory, then only the file is moved and the content of the target file is replaced.

mv -u filename target_dir

In this example, I have an older file in the current directory (file2.txt) and a newer file (file1.txt) :

abhi@learnubuntu:~$ ls -l
total 16
-rw-rw-r-- 1 abhi abhi  348 Jul  5 06:17 file1.txt
-rw-rw-r-- 1 abhi abhi  348 Jul  5 06:23 file2.txt
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:26 my_dir
drwxr-xr-x 3 abhi root 4096 Jul  5 06:15 new_dir
abhi@learnubuntu:~$ ls -l my_dir
total 8
-rw-rw-r-- 1 abhi abhi 348 Jul  5 04:52 file1.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:25 file2.txt

I try to move them to another location with -u option:

abhi@learnubuntu:~$ mv -u file1.txt file2.txt my_dir

And as you can see, the newer file1.txt is moved to the new location but the older file file2.txt remains in its original place.

abhi@learnubuntu:~$ ls -l
total 12
-rw-rw-r-- 1 abhi abhi  348 Jul  5 06:23 file2.txt
drwxrwxr-x 2 abhi abhi 4096 Jul  5 06:27 my_dir
drwxr-xr-x 3 abhi root 4096 Jul  5 06:15 new_dir
abhi@learnubuntu:~$ ls -l my_dir
total 8
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:17 file1.txt
-rw-rw-r-- 1 abhi abhi 348 Jul  5 06:25 file2.txt

There may be a few more usage of the mv command but you are likely to use the first four listed here.

I hope you have a better understanding of this essential Ubuntu command now. Feel free to leave a comment with your questions and suggestions.