Linux mv command with examples

Linux mv command with examples

mv stands for move. mv is used to move one or more files or directories from one place to another in file system like UNIX. It has two distinct functions:
(i) It rename a file or folder.
(ii) It moves group of files to different directory.

Syntax:

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

1. How to rename a file in Linux:

mv file_name new_file_name

Example:

mv aa.txt bb.txt

Linux mv command with examples

2. Move a file to new location.

mv file_name destionation_directory

Example:

mv abcd.ssh logs/

Here abcd.sh file moved to the directory logs.

Options of mv command:

–backup[=CONTROL]
make a backup of each existing destination file

-b like –backup but does not accept an argument

-f, –force
do not prompt before overwriting

-i, –interactive
prompt before overwrite

-n, –no-clobber
do not overwrite an existing file

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

–strip-trailing-slashes
remove any trailing slashes from each SOURCE argument

-S, –suffix=SUFFIX
override the usual backup suffix

-t, –target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY

-T, –no-target-directory
treat DEST as a normal file

-u, –update
move only when the SOURCE file is newer than the destination
file or when the destination file is missing

-v, –verbose
explain what is being done