LinuxCommandLibrary

cp

Copy files and directories

TLDR

Copy a file to another location

$ cp [path/to/source_file] [path/to/target_file]
copy

Copy a file into another directory, keeping the filename
$ cp [path/to/source_file] [path/to/target_parent_directory]
copy

Recursively copy a directory's contents to another location (if the destination exists, the directory is copied inside it)
$ cp [[-r|--recursive]] [path/to/source_directory] [path/to/target_directory]
copy

Copy a directory recursively, in verbose mode (shows files as they are copied)
$ cp [[-vr|--verbose --recursive]] [path/to/source_directory] [path/to/target_directory]
copy

Copy multiple files at once to a directory
$ cp [[-t|--target-directory]] [path/to/destination_directory] [path/to/file1 path/to/file2 ...]
copy

Copy all files with a specific extension to another location, in interactive mode (prompts user before overwriting)
$ cp [[-i|--interactive]] [*.ext] [path/to/target_directory]
copy

Follow symbolic links before copying
$ cp [[-L|--dereference]] [link] [path/to/target_directory]
copy

Use the full path of source files, creating any missing intermediate directories when copying
$ cp --parents [source/path/to/file] [path/to/target_file]
copy

SYNOPSIS

cp [OPTION]... [-T] SOURCE... DEST
or: cp [OPTION]... SOURCE... DEST...
or: cp [OPTION]... -t DIRECTORY SOURCE...

PARAMETERS

-a, --archive
    equivalent to -dR --preserve=all

--attributes-only
    copy attributes only, not data

--backup[=CONTROL]
    make backups (CONTROL: none,number,existing,nil)

-b
    like --backup=existing (deprecated)

--copy-contents
    copy contents of special files in recursive copy

-d
    equivalent to --no-dereference --preserve=links

-f, --force
    remove existing dest files, ignore -i

-i, --interactive
    prompt before overwrite

-H
    follow command-line symlinks

-l, --link
    hard-link files instead of copying

-L, --dereference
    always follow symlinks

-n, --no-clobber
    do not overwrite existing files

-P, --no-dereference
    never follow symlinks

-p
    preserve mode,ownership,timestamps

--preserve[=ATTR_LIST]
    preserve specified attrs (mode,ownership,timestamps,links,etc)

--reflink[=WHEN]
    use clone/CoW (WHEN: auto,always,never)

-r, -R, --recursive
    copy directories recursively

--remove-destination
    remove dest before copy

--sparse=WHEN
    handle sparse files (WHEN: auto,always,never)

--strip-trailing-slashes
    remove trailing slashes from sources

-s, --symbolic-link
    make symlinks instead of copies

-S, --suffix=SUFFIX
    override backup suffix

-t, --target-directory=DIRECTORY
    copy into DIRECTORY

-T, --no-target-directory
    treat DEST as normal file

-u, --update
    copy only newer or missing files

-v, --verbose
    explain what is done

-x, --one-file-system
    stay on source filesystem

-Z
    set SELinux context to default

--context[=CTX]
    set security context

--help
    display help

--version
    output version info

DESCRIPTION

The cp command is a core Linux utility for duplicating files and directories. It copies SOURCE to DEST, handling single files, multiple files into directories, or recursive directory trees with -r. Key features include preserving attributes (-p), interactive overwrite prompts (-i), verbose logging (-v), and updates only for newer sources (-u). Advanced GNU options support hard links (-l), symlinks (-s), sparse files, reflinks for efficient copies, and security contexts.

Common uses: backups, data migration, scripting file ops. For directories, add -r; detect DEST type by slashes or source count. POSIX-compliant base with GNU extensions for modern filesystems. Risks include accidental overwrites or disk exhaustion on large recursives—use -i or -n for safety. Essential for admins and users alike.

CAVEATS

Overwrites files without warning unless -i or -n; recursive copies (-r) can fill disks or fail on specials; no cross-filesystem by default unless -x absent; preserve needs root for ownership; trailing / on DEST forces directory treatment.

EXIT STATUS

0: success
1: minor errors (e.g. perms)
>1: serious issues (e.g. no space)

EXAMPLES

cp file.txt dest.txt
cp -r src/ dest/
cp -i *.log /backup/
cp -u -v files/ new/

HISTORY

Originated in Version 1 Unix (1971); standardized in POSIX.1-1988; GNU coreutils version adds reflink, SELinux support since 1990s.

SEE ALSO

mv(1), install(1), ln(1), rsync(1), dd(1)

Copied to clipboard