How to move files in Linux

Moving files is an essential task individuals often need to perform.

The mv command and its usage

The mv command is used to rename or move files and directories from one location to another.

Basic syntax:

1
2
3
4
> mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...`

The SOURCE can be one or many files / directories, and the DEST can be a single file or directory.

Moving multiple files to a directory

1
mv file1 file2 dir1

Moving matching files to a directory

1
mv *.md dir1

Moving a directory to another directory

1
mv dir1 ~/Documents

Moving files without clobbering

Using the -i or --interactive option, mv will iteratively request user input for files with naming conflicts.

1
2
> mv -i file1 ~/Documents
mv: overwrite '~/Documents/file1'?

Renaming files

Rename file1 to file2:

1
mv file1 file2