Back to course

23. Moving and Renaming: The `mv` Command

Linux Basics: From Zero to CLI Hero

Relocating Data

The mv command (Move) serves two purposes:

  1. Moving files/directories to a new location.
  2. Renaming files/directories (which is essentially moving them to the same location but with a new name).

Syntax

mv [options] source destination

Renaming a File

To rename old_name.txt to new_name.txt in the same directory:

bash $ mv old_name.txt new_name.txt

Moving a File

To move data.csv to the archive directory:

bash $ mv data.csv archive/

Moving and Renaming Simultaneously

To move data.csv to archive/ and rename it to 2023_data.csv:

bash $ mv data.csv archive/2023_data.csv

Moving Directories

mv works on directories without needing the -r option, as it only changes the pointer (inode) to the directory, it doesn't read the contents.