Linux Move File

The mv command in Linux is a versatile utility used for both moving files and directories from one location to another, and for renaming them. It's a fundamental command for organizing your filesystem.

Syntax

mv [OPTION]... SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY

Description

When moving files or directories, mv effectively changes their location on the filesystem. When renaming, it changes the name of a file or directory within the same location. Unlike copying (cp), mv does not create a new copy of the data; it simply updates the filesystem's pointers to the data.

Common uses include:

  • Moving files to a different directory.
  • Renaming files or directories.
  • Consolidating files from multiple locations.
  • Preventing accidental overwrites.

Common Options

Option Description
-i, --interactive Prompt before overwrite.
-f, --force Do not prompt before overwriting.
-n, --no-clobber Do not overwrite an existing 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.

Examples

Move a file to a directory

mv myfile.txt /home/user/documents/

Moves myfile.txt from the current directory to /home/user/documents/.

Rename a file

mv oldname.txt newname.txt

Renames oldname.txt to newname.txt in the same directory.

Move and rename a directory

mv old_dir /new_location/new_dir_name

Moves old_dir to /new_location/ and renames it to new_dir_name.

Move multiple files to a directory

mv file1.txt file2.txt file3.txt /backup/data/

Moves file1.txt, file2.txt, and file3.txt into the /backup/data/ directory.

Prevent overwriting existing files

mv -n report.pdf /archive/

Moves report.pdf to /archive/ only if a file named report.pdf does not already exist there.

See also