A command that never goes unnoticed is the mv command. This command is simple to use, but it allows us to move or rename a file or directory. It belongs to those management commands that are easy to use and allows us to have more flexibility when handling files.
The mv command, what is it?
In Linux, the mv command is mainly used to move files and folders from one location to another. But if the file or directory exists, then it will replace it. Therefore, this command is also used to rename files and folders, although it is not its forte.
This command belongs to the GNU Utils, so you can have it available in all Linux distributions that exist. So, as with the free command, we won’t have to install anything or do any extra procedures.
It is also necessary to say that the mv command is one of the most used tools in scripts or in the use of the terminal. It is very efficient and, at least personally, I use it to move large files because it is very fast.
If you are new to Linux, learning how to use the mv command is a must.
So let’s go for it.
Using the mv command in Linux
In this post, we will use examples to better explain the use of the command.
If you want to move a file to another location, you just need to execute this syntax.
mv [file-path] [destination]
For example,
mv file1.txt /home/user/Documents
Remember that with the mv command, you can use relative paths and absolute paths.
Another thing you have to remember is that if in the destination there is a file with the same name, it will be overwritten. So be careful.
You can move several files at the same time.
mv file1.txt file2.txt /home/user/Downloads
You can also use absolute paths
mv file1.txt /home/user/file2.txt /home/user/Downloads
As with many commands, mv accepts the use of wildcards. For example, you can move all files of one type.
mv *.txt ~/Documents
In the same way, you can move directories to another location. For example:
mv /home/user/docs ~/Documents/
This will move the entire docs
folder to the new path.
Other Uses
To rename a file, you only have to use a syntax like the one shown below
mv file.txt file1.txt
This way, the file file.txt
will now be called file1.txt
and will be in the same location.
The same goes for directories.
mv docs/ Docs/
So, the folder docs/
will now be called Docs/
.
As you can see, it is straightforward to do.
More uses of the mv command
If you are working with scripts or sensitive files, you can tell the command to ask for a confirmation before moving the files. This is achieved with the -i
option.
mv -i file1.txt ~/Pictures/
to complete the process, answer -i
Moreover, we can force mv
to only move files that are newer than the ones in the destination. This is quite useful to avoid losing valuable files through overwriting.
To achieve this, you can use the -u
option
mv -u file1.txt ~/Documents/
There, the command will only be executed if the file file1.txt
is newer than the one in ~/Documents
.
To make a backup before moving the files, there is the -b
option. Important to always use it because if you make a mistake in moving the wrong files you can always get them back.
mv -b *.txt ~/Documents/
So, before moving them, we will have a copy of these files.
In scripts, it is useful to show everything we are doing. The mv
command can also do it thanks to the -v
option.
mv -v *.txt ~/Documents/
You can use it, as simple as that.
Conclusion
The mv command is a basic command in the daily use of Linux in the terminal. This command allows us to move files, but also to rename files and folders.
I hope you found this guide useful and that you help us to grow by sharing this with your friends.