Back to course

21. Copying Files and Directories: The `cp` Command (Part 1)

Linux Basics: From Zero to CLI Hero

Duplicating Data

The cp command (Copy) is used to copy files and directories from one location to another.

Syntax

The general syntax is cp [options] source destination.

Copying a Single File

To copy report.txt from the current directory to the backup directory:

bash $ cp report.txt backup/

If you want to copy the file and rename it in the process:

bash $ cp report.txt backup/report_v2.txt

Copying to the Current Directory

The dot (.) is a shortcut for the current working directory.

bash

Copy file from /etc to the current directory

$ cp /etc/hosts .

Useful Options

  • -i (Interactive): Prompts you before overwriting an existing destination file. Highly recommended for safety. bash $ cp -i fileA fileB cp: overwrite 'fileB'? y

  • -v (Verbose): Explains what is being done.

  • -r (Recursive): Required for copying directories (covered next).