cp command
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.
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.
cp report.md backup_report.mdIf backup_report.md doesn’t exist, it gets created. If it exists, the old content is lost, replaced by the new copy.
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).
cp -R projectX/ backup_projectX/The -R flag makes cp copy the entire directory, including subfolders and files, preserving structure.
| Option | Description | Example |
|---|---|---|
| -i | Prompt before overwriting existing files | cp -i notes.txt /archive/ |
| -n | Don’t overwrite existing files in the destination | cp -n report.md /archive/ |
| -r, -R | Copy directories and their contents recursively | cp -r images/ backup_images/ |
| -v | Show progress and copied files | cp -v *.pdf /backup/ |
| -p | Preserve original file attributes (timestamps, permissions, etc.) | cp -p config.yaml /backup/ |
| -u | Only copy if source file is newer or missing in destination | cp -u draft.txt /docs/ |
| -f | Force overwrite by removing destination file if needed | cp -f log.csv results/ |
| -a | Archive mode: preserve structure and metadata, copy recursively | cp -a mydir/ copydir/ |
Easily move groups of files by pattern:
cp *.jpg dummy_photos/Copies all .jpg files to the dummy_photos folder.
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.
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
Ubuntu 20.04 LTS (code name Focal Fossa) was released on April 23, 2020. It is a…
Google Chrome is the most widely used web browser in the world. It is fast, secure,…
Java is one of the most widely used programming languages in the world. It runs on…
Raspberry Pi is the most popular single-board computer ever made. It is small, affordable, and surprisingly…
pip is Python's package manager. It lets you search, download, and install packages from the Python Package…
MySQL is the most popular open-source relational database management system. It is fast, reliable, and a…