Linux mv command - how to 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. 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. First, create a new directory named owner and make a new file named history.txt file in user's home directory. Redirect the history output to a file name history.txt with '>'. The command is history > history.txt. Use 'ls' command to see the result.
Now, we are going to 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:
luzar@slackware:~$ mkdir owner
luzar@slackware:~$ ls
owner/
luzar@slackware:~$ history > history.txt
luzar@slackware:~$ ls
history.txt owner/
luzar@slackware:~$ mv history.txt owner/
luzar@slackware:~$ ls -R
.:
owner/
./owner:
history.txt
luzar@slackware:~$
|
We can view the result with 'ls -R' to view a directory content. See that history.txt has been moved into /owner directory? That's the basic Linux mv command usage.
Here is an example on how to move multiple files using Linux mv command:
luzar@slackware:~/examples$ ls
BUKUMERAH-BM.pdf UserManual.pdf hardwarePrices.pdf
luzar@slackware:~/examples$ mv UserManual.pdf hardwarePrices.pdf /home/luzar/slackware/
luzar@slackware:~/examples$ ls /home/luzar/slackware/
UserManual.pdf hardwarePrices.pdf
luzar@slackware:~/examples$ ls
BUKUMERAH-BM.pdf
luzar@slackware:~/examples$
|
The UserManual.pdf and hardwarePrices.pdf are source files. The /home/luzar/slackware/ is the destination directory. See that when we move file using Linux mv command, it doesn't leave a source copy in the original location. The mv command move the source, not duplicate it.
Here is an example on how to move directory to another location using Linux mv command:
luzar@slackware:~$ mv labu labi
luzar@slackware:~$ ls labi/
labu/
luzar@slackware:~$
|
The example above shows the labu directory has been move to a destination directory called labi.
How to rename file or directory using Linux mv command
The next thing you can do with Linux mv command is, you can rename file or directory with mv command. There is no ren command in Linux. The usual command 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:
luzar@slackware:~$ mv examples.tar .
mv: `examples.tar' and `./examples.tar' are the same file
luzar@slackware:~$
|
If we change the file's name, it should be ok, isn't it? Let's see an example:
luzar@slackware:~$ mv examples.tar move.tar luzar@slackware:~$ 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. But it's not move any more, it's rename. 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:
luzar@slackware:~/slackware$ mv move.tar rename/NewName.tar luzar@slackware:~/slackware$ ls rename examples.tar.gz NewName.tar luzar@slackware:~/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:
luzar@slackware:~$ mkdir buzz
luzar@slackware:~$ ls
buzz/ rename/
luzar@slackware:~$ mv buzz labu
luzar@slackware:~$ ls
labu/ rename/
luzar@slackware:~$
|
You can see from example above, the 'buzz' directory has been renamed to 'labu' directory.
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.
Popular content


Delicious
Digg
StumbleUpon
Propeller
Reddit
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket
This is wrong: mv
This is wrong:
mv [OPTION]... [-T] SOURCE1 SOURCE2 SOURCE3 ... DEST
Only one SOURCE is possible with -T, as suggested both by synopsis and logic of the thing and confirmed by an empirical test.
Thanks for the correction.
Thanks for the correction. My bad. I just copied the synopsis and add the sources without thinking about the option. Update.
Post new comment