Linux Command: mv

Published on September 9, 2021 at 7:07 am by LEW

Introduction

The mv command is another import form early UNIX implementations. The mv is short for move. It is present in all major Linux Distributions. It’s purpose is to move files or directories form one location to another.

Syntax and Function

The syntax of the mv command is:

mv [options] Source Destination

If files/directories are on the same partition, the mv command results in a simple change to the file/directory pointers. If the files/directories are being moved to a different partition, then the data is physically copied to the new location and the old files/directories are deleted. Note that mv is also used to rename files.

Examples

To rename a file with mv, simply type the old file name (source) and the new file name (destination). If the file is in your current working directory, no paths are needed.

mv testfile1A.txt testfile1B.txt

If a file is to be moved to a new directory, then the path (either relative or full) will be needed. If we are in our home directory and we want to move our test file from our project directory to our archive directory (which are both in our home directory) and add bak to the file name, the command with relative paths would be

mv project/testfile1b.txt archive/testfile1b.bak.txt

Lets assume we are root (so we have permissions to actually do this) and we want to move our test file from user1 home directory to user2 home directory using full absolute file paths. The command would look like this.

mv /home/user1/testfile.txt /home/user2/testfile.txt

Options

The mv command has some options available. These are the ones I tend to use the most. For others, check the man pages.

Options can be strung together or used individually. “-f -b” is the same as “-fb”.

Conclusion

The mv command is one of the more often used commands, even for casual users. Especial since there is no rename command. The command is not complicated, but it does have a few idiosyncrasies.

This short introduction to the mv command covered the majority of the common uses that I have run across. Like a lot of terminal commands it can be far more powerful than its desktop equivalent.

Add New Comment

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