How to Move Files and Directories in Linux Using Mv Command

Suraj Joshi Feb 02, 2024
How to Move Files and Directories in Linux Using Mv Command

We can move files and directories using the mv command with various arguments through Linux Terminal.

Move Files and Directories Using the mv Command

The mv(move) command could move files and directories from one location to another. It can also be used to rename files and directories.

Syntax of mv Command:

mv [OPTIONS] source destination

The source in the above command can be one or more files or directories, and destination is always a single file or directory.

If we have multiple files or directories as a source, the destination is always directory. In this case, all the source files and directories are moved to the destination directory. If we have a single file as a source and a directory as a target, the file is moved to the target directory.

An important point to be noted while moving files and directories is that if we do not have the write permissions for both source and destination, we will get a permission denied error.

Move File to a Directory Using mv Command

We can use the following command to move a file inside a directory.

mv <filename> <path_of _destination_directory>

Example: Move File Inside a Directory Using mv Command

mv ILLUMEE.svg SVG

It moves the file ILLUMEE.svg in the current working directory to the folder SVG in the current working directory.

If the destination directory is not present, the source file is renamed as the destination file.

If the directory SVG is not present in the current working directory, the file ILLUMEE.svg is renamed to SVG.

If the destination path is also a filename, the source filename is renamed with destination filename.

mv ILLUMEE.svg 1.svg

It will rename the file ILLUMEE.svg to 1.svg.

In some cases, the destination file may already exist, and it will be overwritten if we use the mv command. To prompt for confirmation before overwriting, we use -i option with mv command.

mv -i ILLUMEE.svg 1.svg

If the filename 1.svg already exists, the terminal will prompt us before overwriting.

mv -i ILLUMEE.svg 1.svg

Output:

mv: overwrite '1.svg'? n

To prevent overwriting, press N and hit Enter key else press Y and hit Enter.

We can also prevent overwriting by using the -n option with the mv command.

mv -n ILLUMEE.svg 1.svg

It will prevent overwriting of 1.svg if it already exists.

Move a Directory Inside Another Directory Using the mv Command

To move a directory inside another directory , we can use the following command:

mv <path_of_source_directory> <path_of _destination_directory>

Example: Move a Directory Inside Another Directory Using mv Command

mv Python_Scripts Python_Scripts_New

It moves the directory Python_Scripts in our current working directory inside another directory Python_Scripts_New in the current working directory.

If the destination directory is not present, the source directory is renamed to the destination directory.

Move Multiple Files Inside Another Directory Using mv Command

To move multiple files inside another directory, we specify all the source files followed by the destination directory path.

mv <source_filepath_1> <source_filepath_2> <source_filepath_3> 
     <path_of_destination_directory>

Example: Move Multiple Files Inside Another Directory Using mv Command

mv 1.jpg 2.jpg 2.png Images

It moves the files 1.jpg, 2.jpg, and 2.png in our current working directory inside another directory Images in the current working directory.

We can also move multiple files inside a directory by using regular expressions to match the filenames that need to be moved.

mv *.jpg JPG-Images

It moves all the files ending with .jpg to the folder JPG-Images.

Take a Backup of an Existing File

To take the backup of existing files, we use the -b option. It will create a backup of overwritten file with ~ character attached to the name of the backup file.

mv -b abc.jpg 123.jpg
ls

Output:

123.jpg 123.jpg~
Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Files and Directories