In this tutorial, you’ll explore how to use the mv command in Linux. The mv
command in Linux is used to move or rename files and directories. The mv
command is a powerful tool that allows users to move files and directories between different locations on their system, including renaming files and directories. In this blog, we will discuss the mv
command in detail and how to use it.
The mv
command takes the following syntax:
mv [options] source destination
The mv
command takes the following options:
-i
: Interactive mode, prompts the user before overwriting any files.-v
: Verbose output, displays the name of each file as it is moved.
Usage of mv Command
Here are some examples to understand how the mv
command works:
Move a single file:
To move a single file, you can specify the source file name and the destination file name as arguments to the mv
command. For example, to move a file named file1.txt
to a new file named file2.txt
, you can run the following command:
mv file1.txt file2.txt
Rename a file:
To rename a file, you can specify the source file name and the new file name as arguments to the mv
command. For example, to rename a file named file1.txt
to file2.txt
, you can run the following command:
mv file1.txt file2.txt
Move multiple files:
To move multiple files at once, you can specify the source file names and the destination directory name as arguments to the mv
command. For example, to move three files named file1.txt
, file2.txt
, and file3.txt
to a directory named mydir
, you can run the following command:
mv file1.txt file2.txt file3.txt mydir/
Move a directory:
To move a directory, you need to specify the source directory name and the new directory name as arguments to the mv
command. For example, to move a directory named mydir
to a new directory named mydir2
, you can run the following command:
mv mydir mydir2
Interactive mode:
To prompt the user before overwriting any files, you can use the -i
option with the mv
command. For example, to move a file named file1.txt
to a new file named file2.txt
with interactive mode, you can run the following command:
mv -i file1.txt file2.txt
Verbose output:
To display the name of each file as it is moved, you can use the -v
option with the mv
command. For example, to move a file named file1.txt
to a new file named file2.txt
with verbose output, you can run the following command:
mv -v file1.txt file2.txt
Conclusion
The mv
command is a powerful tool that allows users to move and rename files and directories on their Linux system. It is important to use this command with caution as it can overwrite existing files and directories. Always double-check the files and directories before moving them to avoid any accidental deletion or overwriting, see the man page for the mv command in Linux.
I hope this article has been helpful in understanding the mv command in Linux. Thank you for reading!
If you have any questions please leave them in the comments below