We can move files and directories using the
mv command with various arguments through Linux Terminal.
Move Files and Directories Using the
mv(move) command could move files and directories from one location to another. It can also be used to rename files and directories.
mv [OPTIONS] source destination
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
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 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
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
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 -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
mv: overwrite '1.svg'? n
To prevent overwriting, press
N and hit
Enter key else press
Y and hit
We can also prevent overwriting by using the
-n option with the
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
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 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
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 1.jpg 2.jpg 2.png Images
It moves the files
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
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