How to Use the mv Command

The 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.

  • When multiple files or directories are given as a SOURCE, the DESTINATION must be a directory. In this case, the SOURCE files are moved to the target directory.
  • If you specify a single file as SOURCE, and the DESTINATION target is an existing directory, then the file is moved to the specified directory.
  • If you specify a single file as SOURCE, and a single file as DESTINATION target then you’re renaming the file.
  • When the 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

How to Move Files and Directories in Linux
1.30 GEEK