One of the most basic operations you'll need to conduct on a Linux system is moving files and folders. You can move or transfer files using the given GUI in any system, but you might be curious if the terminal has a command that allows you to swiftly move files to different directories.
The mv command is what you're looking for, and it's simple to use because of its short and straightforward syntax. In this tutorial, you'll learn how to move files with the mv command.
Prerequisites
- A Linux-based system
- Terminal access
Note: The commands in this tutorial are executed on the Linux Mint system. All the methods in the tutorial will work for any Linux-based system.
Move a Single File With mv Command
The basic syntax of the mv command is as follows
mv [option] <source> <destination>
Note: Take care to add your source and destination with the correct directory path in the command.
You can simply move a single file by following the above syntax with your source and destination without any additional options.
You can navigate to the folder on the command line and check its content by running the ls command, to verify the results.
Move Multiple Files With mv Command
You can also move multiple files with the mv command. This is particularly useful when you need to move multiple files from different folders to one place. To move multiple files, you can add multiple sources before the destination.
mv <source1><source2><source3> <destination>
You can verify the results by running the list command.
Although you can mention multiple sources in the mv command, you cannot add more than one destination in the mv command.
This is not the only way to move multiple files through the mv command, You can tweak it with the singular source to move multiple files with similar names or types. For example, if you want to move files with a similar name, you can mention the similar part with *.
This will move all the files containing the mentioned part to the destination folder.
You can also just mention the file format and the mv command will move all the files with that format to the destination folder.
Printing an Operation Record Using the Mv Command
As you can see, the mv command does not show any dialog in the case of success. If you want to see some sort of dialog or assurance, you can run the command with the -v option.
mv -v <source> <destination>
You can also save this in an output file to keep a record of the operations.
Overwrite Files With mv Command
One concern with the mv command is that it automatically overwrites any file with the same name that exists in the destination folder. Some good options are good practices to avoid in such a case.
The first one is to use the -i option. This option at the same time lets you know of the file conflict and will ask you if you want to proceed with the overwrite.
mv -i <source> <destination>
Another option is to use the -u option. With this option, you can ensure the most updated version of the file exists in the destination folder.
mv -u <source> <destination>
If you want to keep all versions of the file, you can use the -backup=numbered option. This option will save the source file within hidden files in the destination.
mv --backup=numbered <source> <destination>
More About mv Command
To explore more mv options, you can run the following command to see the detailed information.
mv --help
Conclusion
The mv command is a command-line utility for moving files or directories from one location to another. It can move single files, numerous files, and directories. It is quite similar to the copy command (cp), which is used to copy, and the remove command (rm), which is used to delete. The move command includes elements from both the copy and remove commands.
This article shows you how to move files while taking advantage of the options provided by the mv command.