Linux mv Command: Move Files and Directories

As a Linux user, you often need to move files and directories around your file system, and this can be done quickly with the Linux mv command. This comprehensive guide will explore the different ways to use the Linux mv command, including basic usage, advanced techniques, and common scenarios. Whether a beginner or an experienced Linux user, you’ll learn something new about the Linux mv command.

Basic Usage of the Linux mv Command

The basic usage of the Linux mv command is to move a file or directory from one location to another or to rename a file or directory. The syntax of the Linux mv command is as follows:

mv [OPTIONS] SOURCE DESTINATION

Where SOURCE is the file or directory you want to move, and DESTINATION is the location to which you want to move it.

Here are five basic examples of using the Linux mv command with detailed explanations:

  1. Move a file named file.txt from the current directory to the /tmp directory:
mv file.txt /tmp/

This command moves the file named file.txt from the current directory to the /tmp directory. The file.txt file will no longer be in the current directory but will be in the /tmp directory instead.

  1. Rename a file named file.txt to newfile.txt:
mv file.txt newfile.txt

This command renames the file named file.txt to newfile.txt. The file will remain in the same location, but its name will change from file.txt to newfile.txt.

  1. Move a directory named directory/ to the /tmp directory:
mv directory/ /tmp/

This command moves the directory named directory/ and all of its contents to the /tmp directory. The directory/ will no longer be in its original location but will be in the /tmp directory instead.

  1. Move multiple files named file1.txt and file2.txt to the /tmp directory:
mv file1.txt file2.txt /tmp/

This command moves both the file1.txt and file2.txt files from their current location to the /tmp directory. Both files will no longer be in their original location but will be in the /tmp directory instead.

  1. Move all .txt files in the current directory to the /tmp directory:
mv *.txt /tmp/

This command moves all files with the .txt extension in the current directory to the /tmp directory. The .txt files will no longer be in their original location but will be in the /tmp directory instead.

These are just a few basic examples of using the Linux mv command. You can also use the Linux mv command with various options to modify its behavior, which we’ll explore in the advanced techniques section of this guide.

Advanced Techniques with the Linux mv Command

To begin with, here is a table that provides an overview of all the options and variables for using the Linux mv command:

OptionDescription
-bMakes a backup of each existing destination file.
–backup[=CONTROL]Makes a backup of each existing destination file.
-fDoes not prompt before overwriting an existing file.
–forceDoes not prompt before overwriting an existing file.
-iPrompts before overwriting an existing file.
–interactivePrompts before overwriting an existing file.
-nDoes not overwrite an existing file.
–no-clobberDoes not overwrite an existing file.
–strip-trailing-slashesRemoves any trailing slashes from each source argument.
-S or –suffix=SUFFIXOverrides the usual backup suffix.
-t or –target-directory=DIRECTORYMoves all source arguments into a directory.
-T or –no-target-directoryTreats the destination as a normal file.
-u or –updateMoves the source file only if it is newer than the destination file or if the destination file is missing.
-v or –verboseProvides a detailed explanation of what is being done.
-Z or –contextSets the SELinux security context of the destination file or directory.

Note: If you specify more than one of -i, -f, and -n, only the final one will take effect.

The Linux mv command has various options that allow you to modify its behavior, as the table demonstrates some of the abbreviations above. Here are some common scenarios and how to handle them using the Linux mv command’s options.

Making a Backup of an Existing Destination File

By default, the Linux mv command will overwrite an existing file with the same name at the destination. However, if you want to make a backup of the existing file before it is overwritten, you can use the –backup=CONTROL or -b option.

mv --backup=numbered file.txt /tmp/

This command makes a backup of the existing file.txt in the /tmp/ directory by appending a number to the backup file’s name. For example, if the existing file.txt in the /tmp/ directory is file.txt.~1~, the new backup file will be named file.txt.~2~.

Moving Only if the Source File is Newer Than the Destination

If you only want to move a file if the source file is newer than the destination file or if the destination file is missing, you can use the -u or –update option.

mv -u file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory only if the source file is newer than the destination file or if the destination file is missing.

Do Not Overwrite an Existing File:

To prevent overwriting an existing file at the destination, you can use the -n or –no-clobber option.

mv -n file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory, but if there is already a file with the same name in the /tmp/ directory, it will not overwrite it.

Overwrite an Existing File

If you want to overwrite an existing file at the destination without prompting for confirmation, you can use the -f or –force option.

mv -f file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory, and if there is already a file with the same name in the /tmp/ directory, it will overwrite it without prompting for confirmation.

Prompt Before Overwriting an Existing File

If you want to be prompted for confirmation before overwriting an existing file at the destination, you can use the -i or –interactive option.

mv -i file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory, and if there is already a file with the same name in the /tmp/ directory, it will prompt you for confirmation before overwriting it.

Explanation of What is Being Done

To get a detailed explanation of what the Linux mv command is doing, you can use the -v or –verbose option.

mv -v file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory, and it will display a detailed explanation of what is being done.

Setting SELinux Security Context:

If you want to set the SELinux security context of the destination file or directory, you can use the -Z or –context option.

mv -Z file.txt /tmp/

This command moves the file.txt file to the /tmp/ directory and sets its SELinux security context.

Overriding the Backup Suffix

By default, the Linux mv command will use the ~ symbol as the backup suffix when making a backup of an existing file. If you want to override the backup suffix, you can use the -S or –suffix=SUFFIX option.

mv --backup=suffix --suffix=.bak file.txt /tmp/

This command makes a backup of the existing file.txt in the /tmp/ directory by appending the .bak suffix to the backup file’s name. For example, if the existing file.txt in the /tmp/ directory is file.txt, the new backup file will be named file.txt.bak.

Moving Multiple Files to a Directory

If you want to move multiple files to a directory, you can use the -t or –target-directory=DIRECTORY option.

mv file1.txt file2.txt -t /tmp/

This command moves file1.txt and file2.txt to the /tmp/ directory.

Treating the Destination as a Normal File

By default, the Linux mv command treats the destination as a directory if it exists. If you want to treat the destination as a normal file, you can use the -T or –no-target-directory option.

mv -T file.txt /tmp/file.txt

This command moves the file.txt file to the /tmp/file.txt location and treats /tmp/file.txt as a normal file, not a directory.

These are just a few additional examples of using the Linux mv command’s options to modify its behavior. You can combine these options to achieve the desired result or use other options not covered here.

Conclusion

The Linux mv command is a powerful tool for moving files and directories around a Linux environment. With its basic usage and advanced techniques, you can handle all kinds of scenarios, from moving a single file to a different directory to moving multiple files with different names. With this comprehensive guide, you’ll be a pro at using the Linux mv command in no time.

Your Mastodon Instance
Share to...