LinuxCommandLibrary

cp

File and directory copy utility

TLDR

Copy file

$ cp [source.txt] [destination.txt]
copy
Copy directory
$ cp -r [source_dir] [dest_dir]
copy
Copy with backup
$ cp --backup [source] [dest]
copy
Preserve attributes
$ cp -p [source] [dest]
copy
Interactive mode
$ cp -i [source] [dest]
copy

SYNOPSIS

cp [options] source destination

DESCRIPTION

cp copies files and directories. It can copy single files, multiple files to a directory, or entire directory trees. It preserves or modifies file attributes depending on options used.
The command is a fundamental tool for file management on Unix systems.

PARAMETERS

-r, -R, --recursive

Copy directories recursively
-i, --interactive
Prompt before overwrite
-f, --force
Force overwrite
-n, --no-clobber
Don't overwrite existing files
-p, --preserve
Preserve mode, ownership, timestamps
-a, --archive
Archive mode (same as -dR --preserve=all)
-u, --update
Copy only when source is newer
-v, --verbose
Verbose output
-l, --link
Hard link instead of copy
-s, --symbolic-link
Symbolic link instead of copy
--backup
Backup existing files

WORKFLOW

$ # Copy file
cp file.txt copy.txt

# Copy to directory
cp file.txt /tmp/

# Copy multiple files
cp file1.txt file2.txt file3.txt /destination/

# Copy directory
cp -r source_dir/ destination_dir/

# Preserve attributes
cp -p original.txt copy.txt

# Archive copy (preserve everything)
cp -a directory/ backup/

# Interactive (ask before overwrite)
cp -i file.txt existing.txt

# Backup existing file
cp --backup file.txt existing.txt
copy

COPYING DIRECTORIES

$ # Copy directory and contents
cp -r project/ project_backup/

# Archive mode (preserve all attributes)
cp -a /data/ /backup/data/

# Update only newer files
cp -ru source/ destination/
copy

CAVEATS

Without -r, cannot copy directories. Overwrites by default (use -i for safety). -p doesn't always preserve all attributes. Large recursive copies can be slow. Sparse files may not be handled efficiently. Symlinks copied as links or dereferenced depending on options.

HISTORY

cp has been a core Unix command since the early 1970s, part of the original Unix toolset.

SEE ALSO

mv(1), rm(1), rsync(1), install(1)

> TERMINAL_GEAR

Curated for the Linux community

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

$54.00
GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

$739.99
Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

$1,589.00
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

$239.99
Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99
GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

GMKtec Gaming PC, K11 AMD Ryzen 9 8945HS, 32GB DDR5 RAM 1TB Mini PC Desktop Computer, Oculink, Dual NIC 2.5GbE LAN, HDMI/DisplayPort/USB4*2

$739.99