The Linux Move (mv) Command

Technology - The Linux Move (mv) Command

The Linux move command (mv) is one of the essential commands that can be very useful in Linux, Unix, and AIX.  The move command’s primary purpose is to move files and directories.   The move command may also be used to rename files and to make backups.

Here’s the basic syntax of the mv command:

mv [options] source destination
  • source: This is the file or directory you want to move or rename.
  • destination: This is the location where you want to move the source or the new name if you’re renaming it.

Here are some common usages and options of the mv command:

  1. Move a File: To move a file from one location to another, you can use the mv command like this:
   mv file.txt /path/to/destination/

This command will move the file file.txt to the /path/to/destination/ directory.

  1. Rename a File: You can also use mv to rename a file. Simply specify the source file and the desired new name:
   mv oldfile.txt newfile.txt

This will rename oldfile.txt to newfile.txt.

  1. Move and Rename Directories: The mv command can be used to move and rename directories as well. For example:
   mv /path/to/source_directory /path/to/destination_directory

This moves the entire source_directory to destination_directory. If the destination doesn’t exist, it renames the source directory to the destination.

  1. Force Overwriting: If a file with the same name exists in the destination directory, you can force the overwrite using the -f option:
   mv -f sourcefile.txt /path/to/destination/
  1. Interactive Mode: To prompt for confirmation before overwriting files, use the -i option:
   mv -i sourcefile.txt /path/to/destination/
  1. Verbose Mode: To display what the mv command is doing, use the -v (verbose) option:
   mv -v file.txt /path/to/destination/
  1. Preserve Timestamps: To preserve the timestamps of the source file or directory, use the -p option:
   mv -p file.txt /path/to/destination/
  1. Move Multiple Files: You can move multiple files to a directory using wildcards:
   mv *.txt /path/to/destination/

This will move all files with the .txt extension to the destination directory.

The mv command is a versatile and powerful tool for managing files and directories in a Linux or Unix-like operating system. Always be cautious when using it to avoid accidentally overwriting or deleting files.

Move Command options

option description
mv -f force move by overwriting destination file without prompt
mv -i interactive prompt before overwriting
mv -u update – move when the source is newer than the destination
mv -v verbose – print source and destination files
MV – t explicitly saying to move the file or directory here, rather trying to fit everything into the last argument.
mv * Move all (Multiple) files to a specific director without listing by name

For More move command details see the Linux documentation manuals using the man command

$ man mv

mv command examples

Here are some quick and very simple move command (MV) examples for reference.

Move Move to files  to the /Archive/ directory:

$ mv happy.txt garden.txt /Archive/

Move all “.txt” files in the current directory to subdirectory backup:

$ mv *.txt backup

Move all files in subdirectory ‘backup’ to current directory:

$ mv backup/*

Rename file happy.txt to happy.bak filename:

$ mv happy.txt happy.bak

Rename directory backup to backup2:

$ mv backup backup2

Update – move when happy.txt is newer or missing in target directory:

$ mv -u happy.txt backup

Move happy.txt and prompt before overwrite backup / happy.txt:

$ mv -v happy.txt backup

Linux mv command summary with examples
%d bloggers like this: