mv
CommandThe mv
command (short from move) is used to rename and move and files and directories from one location to another. The syntax for the mv
command is as follows:
mv [OPTIONS] SOURCE DESTINATION
Copy
The SOURCE
can be one, or more files or directories, and DESTINATION
can be a single file or directory.
SOURCE
, the DESTINATION
must be a directory. In this case, the SOURCE
files are moved to the target directory.SOURCE
, and the DESTINATION
target is an existing directory, then the file is moved to the specified directory.SOURCE
, and a single file as DESTINATION
target then you’re renaming the file.SOURCE
is a directory and DESTINATION
doesn’t exist, SOURCE
will be renamed to DESTINATION
. Otherwise if DESTINATION
exist, it be moved inside the DESTINATION
directory.To move a file or directory, you need to have write permissions on both SOURCE
and DESTINATION
. Otherwise, you will receive a permission denied error.
For example, to move the file file1
from the current working directory to the /tmp
directory you would run:
mv file1 /tmp
To rename a file you need to specify the destination file name:
mv file1 file2
The syntax for moving directories is the same as when moving files. In the following example, if the dir2
directory exists, the command will move dir1
inside dir2
. If dir2
doesn’t exist, dir1
will be renamed to dir2
:
mv dir1 dir2
#directories #move files #linux