mv Command in Linux Explained [With Examples]

Written by: Linuxopsys   |   Last updated: September 3, 2023

The mv command, short for "move,". It is used in Linux and UNIX-like systems to move or rename files and directories.

Mainly used for two purposes:

  • Moving: It helps in relocating files or directories from one location to another.
  • Renaming: When used within the same directory, it can rename files or directories.

Syntax:

mv [OPTIONS] SOURCE... DEST

Where SOURCE is the file/directory you want to move or rename, and DEST is the new location or name.

Basic usage

Moving a single file

The primary purpose of using the mv command for moving a file is to transfer it from one location to another without making a copy of the file.

If you have a file named file1.txt in your current directory and want to move it to a directory named projects, you would use:

mv file1.txt projects/
mv moving a single file

This will move the file1.txt file to the projects directory but retain its original name.

You can add -v or --verbose option to display information about the move operation.

mv -v file2.txt projects/
mv verbose to display information about the move

Moving multiple files

The mv command can also handle multiple files at once, making it a valuable tool for bulk file operations.

If you want to move file1.txt, file2.txt, and file3.txt to a directory named projects, you would use:

mv file1.txt file2.txt file3.txt projects/

This command will transfer all three files into the projects directory, retaining their original names.

Note: To avoid potential overwrites you can include -i option for safety.

Moving directories

In similar moving files you use mv command to relocate the entire directory and its contents (subdirectories and files) to another location.

Syntax:

mv [OPTION] SOURCEDIR DESTINATION

Suppose you have a directory named projects and you want to move it into another directory called backup. Here's how you'd do it:

mv projects/ backup/

After executing this command, the entire projects directory, along with its contents, will be inside the backup directory.

Note: The mv command in Linux doesn't require a specific "recursive" option like -r or --recursive (which you might be familiar with from commands like cp or rm). When you use mv to move or rename directories, it inherently acts recursively, meaning it moves the directory and all of its contents, which includes subdirectories, files, and any nested structures therein.

Renaming with mv

The mv command in Linux isn't only for moving files and directories; it's also a primary method for renaming them. When you use mv to change the name of a file or directory, you're essentially moving it to a new name in the same location.

Renaming a file:

To rename a file from file1.txt to file2.txt

mv file1.txt file2.txt
mv rename a file

After executing this command, file1.txt will no longer exist, but file2.txt will exist with the content that file1.txt had. By default in all standard Linux distributions, the mv command will overwrite the destination without prompting the user.

Renaming a directory:

To rename a directory from dir1 to dir2:

mv dir1/ dir2/

If dir2/ doesn't exist and you use the mv command as mv dir1/ dir2/, then dir1/ will simply be renamed to dir2/. After the operation, dir1/ will no longer exist, and all its contents will now be under dir2/.

But If you use the mv command to move dir1/ into dir2/ and dir2/ already exists, the entire dir1/ directory will be moved inside dir2/, resulting in dir2/dir1/. If dir2/dir1/ already exists prior to the move, the contents of dir1/ will be merged with the existing dir2/dir1/.

rename a directory when target directory exists

Note: The trailing slashes (/) are optional when renaming directories.

Moving and renaming a file:

Suppose you have a file named file1.txt in your current directory and you want to move it to a directory named projects and simultaneously rename it to new_file1.txt. You would use

mv file1.txt projects/new_file1.txt

Moving and renaming a directory:

If you wish to move the projects directory to backup but rename it to projectsbackup:

mv projects/ backup/projectsbackup

Now, within the backup directory, you'll find projectsbackup which contains all the files and subdirectories that projects had.

Options

Breakdown of useful options of mv command:

  • -f, --force: Overwrite without confirmation. Use with caution.
  • -i, --interactive: Prompt before overwrite. Useful for preventing accidental file overwrites.
  • -n, --no-clobber: Prevents file overwriting by not moving a file if a file with the same name already exists in the destination.
  • -u, --update: Move only if SOURCE file is newer than the destination file or if the destination file is missing.
  • --backup[=CONTROL]: Make a backup of each file that would be overwritten.
  • -t, --target-directory=DIRECTORY: Move all SOURCE arguments into DIRECTORY.
  • -T, --no-target-directory: Treat DEST as a regular file.
  • -v, --verbose: Provides a verbose output, showing files being moved.

Interactive Operation

By default, the mv command will overwrite the destination without prompting the user. This can vary based on system configurations and shell settings, but for many standard Linux distributions and setups, this is the case. If you want to ensure that mv prompts you before overwriting use -i option.

The interactive move (-i or --interactive) option to prompt you for confirmation if the move operation will overwrite an existing file.

mv -i file2.txt projects/
mv interactive operation using -i option

If projects already contains a file named file2.txt, you will be prompted to confirm the overwrite.

If you find that you want this behavior all the time, you can set an alias in your shell's configuration file (e.g., .bashrc for the Bash shell): alias mv='mv -i'

The -f (force) option with mv can seem redundant if the default behavior of mv is to overwrite without prompting. The existence and purpose of the -f option with mv can be understood better in the context of shell environments, aliases, and scripts. By using mv -f in a script, the script author ensures that the mv operation will not pause the script to ask for user input, even if the command is aliased to mv -i in the environment where the script runs.

Using mv with Wildcards

Using wildcards with the mv command in Linux can be extremely powerful, allowing you to move multiple files or directories based on a pattern. The most commonly used wildcard character in Linux is the asterisk *, which matches zero or more characters.

Moving Files Based on Extensions

For example to move all .txt files to a directory:

mv *.txt /home/ubuntu/projects/

This command will move all files in the current directory with a .txt extension to /home/ubuntu/projects/ directory.

Moving Files with Common Prefixes

Move all files starting with file:

mv file* /home/ubuntu/projects/

This moves all files that start with the string file to the /home/ubuntu/projects/ directory.

The mv command also supports other wildcards such as '?' (represents a single character) and '[...]' (matches any one of the characters inside the square brackets).

Specify Target Directory using mv -t

The -t option in the mv command specifies a target directory where you want to move or rename source files or directories. This makes it abundantly clear where the files are being moved, as the target directory is specified upfront.

Let's say you want to move file1.txt, file2.txt, and file3.txt to a directory named projectDir:

mv -t projectDir/ file1.txt file2.txt file3.txt
mv -t example

This command will move all three specified files into the projectDir directory.

Let's take another example by combining of xargs with the mv -t option. This is a common and useful pattern.

Example:

find . -maxdepth 1 -type f | sudo xargs -I {} mv {} /home/ubuntu/backup/

The command uses find to locate files in the current directory and then employs xargs with superuser permissions to move them to the /home/ubuntu/backup/ directory.

Treat Destination as a normal file

The -T option in the mv command is used to explicitly specify that the destination of the move operation is a file and not a directory. If the destination is a file and the -T option is not specified, mv will treat the destination as a directory and will move the source file or directory into it.

Example:

mv -T file1.txt file2.txt

Here If file2.txt doesn't exist, it will rename/move file1.txt to file2.txt. In a scenario when the target is an existing file, It will replace file2.txt with file1.txt.

When the target is an existing directory, instead of moving source-file into the directory (which is the usual mv behavior), the -T option will result in an error.

Example:

mv -T file1.txt projects/
mv -T example

This will produce an error because -T prevents moving into projects directory.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment