Moving files and folders is one of the most basic operations you'll need to conduct on a Linux system. 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 move files to different directories swiftly.
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 like Debian 12 or Ubuntu 22.04
- 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: 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 moving 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 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 file versions, 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
FAQs
1. What is the mv command in Linux?
The mv command is used for moving or renaming files and directories. It transfers items within the filesystem, changing their location or name without duplicating content.
2. How do you use the mv command to move files?
To move files, the syntax is mv [options] source destination
. For example, mv file1.txt /home/user/documents/
moves file1.txt to the specified directory.
3. Can the mv command be used to rename a file?
Yes, the mv command can rename a file. The syntax is mv oldname newname
. For example, mv file1.txt file2.txt
renames file1.txt to file2.txt.
4. Does the mv command work with directories?
Yes, mv can move or rename directories. For moving, use mv dir1/ /path/to/destination/
. To rename, use mv dir1/ dir2/
.
5. How can you move multiple files at once?
To move multiple files, list them before the destination: mv file1.txt file2.txt /path/to/destination/
.
6. What happens if you move a file to a destination with a file of the same name?
The file in the destination will be overwritten without a prompt. To avoid this, use the -i
option for an interactive prompt.
7. Is there a way to move files without overwriting existing files?
Yes, use the -n
option to prevent overwriting: mv -n file.txt /destination/
.
8. How can I undo a mv command operation?
There is no direct way to undo a mv command. You must manually move or rename the files back to their original location or name.
9. Can mv command be used across different filesystems?
Yes, mv can move files across different filesystems. However, this involves copying the file to the new location and then deleting the original.
10. What are some common options used with mv?
Common options include -i
for interactive mode, -n
to avoid overwriting, and -v
for verbose mode, which shows the files being moved.
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.