mv Command - Rename Files

You can change the file name using the mv command. The original file name is changed to the new file name. The name of mv comes from move.

mv OriginalFileName NewFileName

This is a sample to change the file name with the mv command. The file name foo.txt is renamed to bar.txt.

mv foo.txt bar.txt

Rename the directory

The mv command can also be used to rename a directory. On Linux, file names and directory names are treated the same. A directory is a folder in Windows.

mv OriginalDirectoryName NewDirectoryName

This is a sample to change the directory name. The directory name dir1 is changed to the directory name dir2.

mv dir1 dir2

Move the file location

The mv command can also be used to move the location of a file.

Let's move foo.txt in the directory called labo in the home directory to the directory called test in the home directory. "~" Represents the path of your home directory.

mv ~/labo/foo.txt ~/test/foo.txt

As mentioned above, the mv command is also used when moving files.

If you want to move the file, you can omit only the last file name. Notice the trailing slash.

# Syntack sugar of the above operation
mv ~/labo/foo.txt ~/test/

You can do the same if you want to move the directory location.

mv ~/labo/dir1 ~/test/dir1

or

# Syntack sugar of the above operation
mv ~/labo/dir1 ~/test/

Show help --help option

Use the "--help" option to get help.

mv --help

Help is displayed.

Usage: mv [OPTION] ... [-T] SOURCE DEST
  or: mv [OPTION] ... SOURCE ... DIRECTORY
  or: mv [OPTION] ... -t DIRECTORY SOURCE ...
Rename SOURCE to DEST, or move SOURCE (s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
      --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
      --strip-trailing-slashes remove any trailing slashes from each SOURCE
                                 argument 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
      --help display this help and exit
      --version output version information and exit

Associated Information