Linux mv command - move or rename files and directory

The Linux mv command is used to move file or directory in Linux system. When we move a file or directory to a new location, we changed its location. That means we didn't duplicate the file or directory and didn't leave a copy in the original location. So, understand the statement, we can also use mv command to rename file in the same directory. The Linux mv command also allows us to move multiple files to a new location in the Linux system.

Linux mv command syntax

In order to use Linux mv command, we need to know the command's syntax. Below is a part of Linux mv manual page:

NAME
mv - move (rename) files

SYNOPSIS
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

You can see the mv command syntax in the manual page's 'SYNOPSIS' section above. Basically, to move files we'd use:

mv [OPTION] [-T] SOURCE DEST

Where:

  • SOURCE - The file that we want to move.
  • DEST - The new destination for the file.

To move multiple files, use the mv command's syntax below:

mv [OPTION] SOURCE1 SOURCE2 SOURCE3... DIRECTORY

To move a directory, use the mv command's syntax below:

mv [OPTION] [-t] SOURCE DEST

Here are options available with Linux mv command:

-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done

How to move files using Linux mv command

Let's take a look at this example to see how mv command works:

  1. First, create a new directory named owner and make a new file named history.txt file in user's home directory.
  2. Redirect the history output to a file name history.txt with '>'. The command is history > history.txt.
  3. Use 'ls' command to see the result.
  4. Move the history.txt file to another directory using mv command. To use mv command to move a file to another location, you can use mv <source> <destination> format.

See the step by step example below:

[email protected]:~$ mkdir owner
[email protected]:~$ ls
owner/

[email protected]:~$ history > history.txt
[email protected]:~$ ls
history.txt owner/
[email protected]:~$ mv history.txt owner/
[email protected]:~$ ls -R
.:
owner/

./owner:
history.txt
[email protected]:~$

We can view the result with 'ls -R' to view a directory content recursively. See that history.txt has been moved into /owner directory? That's how to use mv command to move file in Linux system.

Can we move more than one file in Linux using the mv command? Yes we can. Here is an example on how to move multiple files using Linux mv command:

[email protected]:~/examples$ ls
BUKUMERAH-BM.pdf UserManual.pdf hardwarePrices.pdf
[email protected]:~/examples$ mv UserManual.pdf hardwarePrices.pdf /home/luzar/slackware/
[email protected]:~/examples$ ls /home/luzar/slackware/
UserManual.pdf hardwarePrices.pdf
[email protected]:~/examples$ ls
BUKUMERAH-BM.pdf
[email protected]:~/examples$

Here is the explanation of the example above. The UserManual.pdf and hardwarePrices.pdf are source files. The /home/luzar/slackware/ is the destination directory. See that when we move files using Linux mv command, it doesn't leave source copy in the original location. The mv command move the source, not duplicate it.

How to move directory in Linux? Basically, it's the same as moving file. Here is an example on how to move a directory called 'labu' to another location (another directory called 'labi') using Linux mv command:

[email protected]:~$ mv labu labi
[email protected]:~$ ls labi/
labu/
[email protected]:~$

The example above shows the labu directory has been moved to a destination directory called labi. Can we move not empty directory? Yes, we can move a directory with all its content including child directory and files.

Linux rename file or rename directory using mv command

The next thing you can do with Linux mv command is, you can rename file or rename directory. There is no ren command in Linux. The usual commands used to rename file in Linux is mv, rename or cp command. Now let's see the mv command first, rename and cp command later. Remember that when we move a file using mv command, we didn't leave a copy in the original location? What happens if we move a file to its original location? Here is what happens:

[email protected]:~$ mv examples.tar .
mv: `examples.tar' and `./examples.tar' are the same file
[email protected]:~$

Linux warns us that we are moving the same file in the same directory. If we change the file's name, it should be ok, isn't it? Let's see another example:

[email protected]:~$ mv examples.tar move.tar
[email protected]:~$ ls | grep tar
examples.tar.gz
move.tar

There we go. So, if we change the file's name, we can move the file in its original location or more accurately, we rename file. Hence, to rename file in Linux, we can use mv command and change the destination file name. Here is another example on how Linux rename file works:

[email protected]:~/slackware$ mv move.tar rename/NewName.tar
[email protected]:~/slackware$ ls rename
examples.tar.gz
NewName.tar
[email protected]:~/slackware$

The example above shows how to rename file and move it to a new directory.

The mv command also can be used to rename directory. The format is still the same 'mv <source> <destination>' Below is an example on how to rename directory in Linux using mv command:

[email protected]:~$ mkdir buzz
[email protected]:~$ ls
buzz/ rename/
[email protected]:~$ mv buzz labu
[email protected]:~$ ls
labu/ rename/
[email protected]:~$

You can see from example above, the 'buzz' directory has been renamed to 'labu' directory. Can we rename directory not empty? Yes we can.

That's all about how to use Linux mv command to move or rename files in Linux system. You can check other tutorials regarding file and directory management in the manage file and directory index page.

Back to Linux basic commands main page.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.