mv - macOS


Overview

mv is a command-line utility in macOS used to move, rename, or rename and move files or directories.

Syntax

mv source destination

  • source: The path to the file or directory to move or rename.
  • destination: The path to the destination directory or the new name for the file or directory.

Options/Flags

  • -f (force): Overwrite existing files or directories without prompting for confirmation.
  • -i (interactive): Prompt for confirmation before overwriting existing files or directories.
  • -n (no-clobber): Do not overwrite existing files or directories.
  • -v (verbose): Enable verbose mode, displaying the list of files or directories being moved or renamed.
  • -r (recursive): Move or rename directories and their contents recursively.
  • -C (copy): Copy files or directories before moving them. This implies -i.

Examples

Simple move operation:

mv /tmp/file1 /Users/johndoe/Documents

Rename a file:

mv /tmp/file1 /tmp/new_file1

Recursively move a directory:

mv -r /tmp/dir1 /Users/johndoe/Documents

Copy and move a file with confirmation:

mv -iC /tmp/file1 /Users/johndoe/Documents

Common Issues

  • Overwriting existing files: Use the -f or -i options to avoid accidentally overwriting files.
  • Permission errors: Ensure you have the necessary permissions to move or rename the files or directories.
  • Destination directory does not exist: Create the destination directory before moving the files or directories.

Integration

Combine with other commands:

  • ls | mv: Pipe the output of ls to move the listed files to a new directory.
  • find | mv: Use find to locate files based on specific criteria and then move them with mv.

Create scripts:

  • Write scripts that automate file management tasks, such as backing up files or moving files to a specific location based on criteria.
  • cp: Copy files or directories
  • ln: Create symbolic or hard links
  • rm: Remove files or directories