mv Command Examples in Linux

The mv command moves files and directories to other locations. It is similar to the cp command but does not leave the initial object in place. Therefore, mv is more like a cut and paste operation. The Bash shell does not have a dedicated rename command but instead uses mv to accomplish that function. The act of “moving” a file or directory and supplying a new name as the destination essentially renames that object.

Syntax

The syntax of the mv command is:

$ mv [options] {file/directory name to move} {file/directory name destination}

For example, to move ~/file1 to /opt/file1:

$ mv ~/file1 /opt/mylist

For renaming purposes, the syntax is:

$ mv [options] {old file/ directory name} {new file/directory name}

mv Command Options

The following table lists some of the options for the mv command.

Option Description
-i Interactive mode. Ask before overwriting destination files.
-f Force the move. If a destination file exists, overwrite it unconditionally.

mv Command Examples

1. Move a file to an arbitrary location:

# mv source target

2. Move files into another directory, keeping the filenames:

# mv source1 source2 source3 target_directory

3. Do not prompt for confirmation before overwriting existing files:

# mv -f source target

4. Prompt for confirmation before overwriting existing files, regardless of file permissions:

# mv -i source target

5. Do not overwrite existing files at the target:

# mv -n source target

6. Move files in verbose mode, showing files after they are moved:

# mv -v source target
Related Post