Linux Terminal Command: cp

The cp command is an essential tool in File Manipulation & Viewing. In this tutorial, we will explore what cp does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.


Concept & Explanation

The cp command duplicates files or directories from a source location to a destination. By default, it preserves file contents but may update ownership and timestamps depending on flags.


Common Options & Syntax

cp [options] [arguments]

Here are the most common flags used with cp:


1. Interactive Example (Simple)

Here is how most people run the command:

# Example
cp config.cfg config_backup.cfg

What it does: Creates a copy of ‘config.cfg’ named ‘config_backup.cfg’ in the same directory.


2. Power-User Example (Advanced)

For scripting and advanced diagnostics, use this configuration:

# Advanced
cp -R -p source_dir/ destination_dir/

What it does: Recursively (-R) copies folders and preserves original file modification times, access times, and file permissions (-p).


⚙️ Warning & Common Pitfalls

[!WARNING] By default, cp will overwrite destination files without warning. Use the -i (interactive) flag to prompt before overwriting.


Here are some related posts on cli_tty1 you might want to check out: