mv command in Linux with Examples



Name

mv - Move (rename) one or more files and directories to a given location (If location is not defined, It renames files on current location).

Synopsis

mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Description

mv command stands for move. This command supports moving or renaming files / group of files / directories. It renames SOURCE to DEST, or moves SOURCE(s) to DIRECTORY. If both filenames are in the same directory, this simply does the file rename; otherwise the file content is copied to the new location and the old file is removed. By default, mv command overwrites files without warning. If the destination file already exists, its data will vanish.

If you want to use the mv command, it is mandatory for users to have write permissions on the directories where the file will move. This happens because mv changes the content of both directories (i.e., the SOURCE and DEST) which are involved in the move. When mv command is used on the files located in the same directory, the timestamp of the file is not updated.

If both SOURCE and DESTINATION arguments are files, this command copies the first file to second and removes the first file. If a DESTINATION file doesn’t exist, mv command creates it and removes SOURCE.

If SOURCE contains various files or directories as arguments, the DESTINATION argument should be a directory.

If both SOURCE and DESTINATION arguments are directories, it copies the first directory into second and removes the first directory recursively.

Options

TagDescription
--backup[=CONTROL]make a backup of each existing destination file
-blike --backup but does not accept an argument
-f, --forcedo not prompt before overwriting
-i, --interactiveprompt before overwrite
-n, --no-clobberdo not overwrite an existing file
--strip-trailing-slashesremove any trailing slashes from each SOURCE argument
-S, --suffix=SUFFIXoverride the usual backup suffix
-t, --target-directory=DIRECTORYmove all SOURCE arguments into DIRECTORY
-T, --no-target-directorytreat DEST as a normal file
-u, --updatemove only when the SOURCE file is newer than the destination file or when the destination file is missing
-v, --verboseexplain what is being done
-Z, --contextset SELinux security context of destination file to default type
--helpdisplay this help and exit
--versionoutput version information and exit

If you specify more than one of -i, -f, -n, only the final one takes effect.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the --backup option or through the VERSION_CONTROL environment variable.Here are the values:

ValueDescription
none, offnever make backups (even if --backup is given)
numbered, tmake numbered backups
existing, nilnumbered if numbered backups exist, simple otherwise
simple, neveralways make simple backups

Examples

Rename file in current directory

$ mv book_list.txt movie_list.txt

Move in ‘archive’ directory

$ mv viewers_list.txt archive/

Move in ‘archive’ directory with different name

$ mv viewers_list.txt archive/users_list.txt

Move multiple files in one command

$ mv current_news.txt headlines.txt cover_story.txt current_news/

Move multiple files using wild card

$ mv *.txt news_portal/

Move a directory including it's content

$ mv current_news/ news_portal/

Move file using interactive mode

$ mv -i viewers_list.txt archive/users_list.txt
mv: overwrite 'archive/users_list.txt'? y
$ mv --interactive viewers_list.txt archive/users_list.txt
mv: overwrite 'archive/users_list.txt'? y

Move file without prompting

$ mv -f viewers_list.txt archive/users_list.txt
$ mv --force viewers_list.txt archive/users_list.txt

move only if destination file does not exist already.

$ mv -n headlines.txt current_news/
$ mv --no-clobber headlines.txt current_news/

Create backup of destination file if that exists already

$ mv -b viewers_list.txt users_list.txt
$ mv --backup viewers_list.txt users_list.txt
$ mv --backup=numbered viewers_list.txt users_list.txt

Create backup with specific suffix

$ mv -S .bak viewers_list.txt users_list.txt
$ mv --suffix=.bak viewers_list.txt users_list.txt

Move only when source file is newer than the destination

$ mv -u headlines.txt current_news/
$ mv --update headlines.txt current_news/

Move in a defined directory

$ mv -t news_portal/ current_news/ archive/
$ mv --target-directory=news_portal current_news/ archive/
Advertisements