Uncategorized

cp Command: Copy Files and Directories in Linux

The cp command, short for “copy,” is the main Linux utility for duplicating files and directories. Whether making a backup, organizing a project, or moving content, cp allows smooth transfers and ensures the original remains unchanged.

Syntax of cp Command

To use cp, provide at least two arguments: a source and a target.

cp [source_file] [destination]

If you specify a directory as the destination, the file is copied into that directory.

Why Use the cp Command?

  • Protect your data with backups.
  • Distribute files across different locations or drives.
  • Safeguard originals before editing.
  • Clone entire directory structures for new projects.

How to Use cp: Modes and Examples

1. Copy A File to Another File

cp report.md backup_report.md

If backup_report.md doesn’t exist, it gets created. If it exists, the old content is lost, replaced by the new copy.

2. Copy Multiple Files to a Folder

cp intro.txt summary.txt notes.txt /archive/

All three files now appear in the /archive folder. If the directory doesn’t exist, cp creates it and fills it with the specified files (as long as you have permissions).

3. Copy Directories (Recursively)

cp -R projectX/ backup_projectX/

The -R flag makes cp copy the entire directory, including subfolders and files, preserving structure.

Useful cp Options

OptionDescriptionExample
-iPrompt before overwriting existing filescp -i notes.txt /archive/
-nDon’t overwrite existing files in the destinationcp -n report.md /archive/
-r, -RCopy directories and their contents recursivelycp -r images/ backup_images/
-vShow progress and copied filescp -v *.pdf /backup/
-pPreserve original file attributes (timestamps, permissions, etc.)cp -p config.yaml /backup/
-uOnly copy if source file is newer or missing in destinationcp -u draft.txt /docs/
-fForce overwrite by removing destination file if neededcp -f log.csv results/
-aArchive mode: preserve structure and metadata, copy recursivelycp -a mydir/ copydir/

Wildcards for Quick Copying

Easily move groups of files by pattern:

cp *.jpg dummy_photos/

Copies all .jpg files to the dummy_photos folder.

Back Up With the –backup Option

Instead of overwriting, create a backup copy:

cp --backup report.md /archive/

If a file with the same name exists at the destination, a backup gets created.

Conclusion

Mastering the cp command helps you protect, organize, and move data efficiently on any Linux system. Understanding its options keeps your work safe and saves time especially when handling critical system or project files.

Read More: History of Linux

Install Kali Linux Here

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Upgrade to Ubuntu 20.04 LTS: Prepare, Update, and Confirm

Ubuntu 20.04 LTS (code name Focal Fossa) was released on April 23, 2020. It is a…

17 hours ago

Install Google Chrome on Ubuntu 20.04: Download and Setup Guide

Google Chrome is the most widely used web browser in the world. It is fast, secure,…

17 hours ago

Install Java on Ubuntu 20.04: OpenJDK 11, JDK 8, and JAVA_HOME

Java is one of the most widely used programming languages in the world. It runs on…

18 hours ago

Install Ubuntu on Raspberry Pi: Flash, Configure, and Boot

Raspberry Pi is the most popular single-board computer ever made. It is small, affordable, and surprisingly…

18 hours ago

Install pip on Ubuntu 20.04: Python 3, Python 2, and Usage Guide

pip is Python's package manager. It lets you search, download, and install packages from the Python Package…

18 hours ago

Install MySQL on Ubuntu 20.04: Setup, Security, and Root Access

MySQL is the most popular open-source relational database management system. It is fast, reliable, and a…

2 days ago