cp

file managementLinux/Unix
The cp command is one of the most frequently used commands in Linux/Unix-like operating systems. cp The cp command is used to copy files and directories from one location to another. It's one of the core utilities in Linux and Unix-like operating systems that supports various options for preserving attributes, handling symbolic links, and creating backups.

Quick Reference

Command Name:

cp

Category:

file management

Platform:

Linux/Unix

Basic Usage:

cp source.txt destination.txt

Common Use Cases

    Syntax

    cp [OPTION]... SOURCE... DIRECTORY
    cp [OPTION]... -t DIRECTORY SOURCE...

    Options

    Option Description
    -a, --archive Same as -dR --preserve=all (preserves structure and attributes)
    -b, --backup Make a backup of each existing destination file
    --backup=CONTROL Control backup method (none, numbered, existing, simple)
    -d Same as --no-dereference --preserve=links
    -f, --force Remove existing destination files if needed
    -i, --interactive Prompt before overwrite
    -l, --link Hard link files instead of copying
    -n, --no-clobber Do not overwrite an existing file
    -P, --no-dereference Never follow symbolic links in SOURCE
    -p, --preserve Preserve mode, ownership, timestamps
    --preserve=ATTR_LIST Preserve specified attributes (mode, ownership, timestamps, links, context, xattr, all)
    -r, -R, --recursive Copy directories recursively
    -s, --symbolic-link Make symbolic links instead of copying
    -S, --suffix=SUFFIX Override the usual backup suffix
    -t, --target-directory=DIRECTORY Copy all SOURCE arguments into DIRECTORY
    -u, --update Copy only when the SOURCE file is newer than the destination file
    -v, --verbose Explain what is being done
    -x, --one-file-system Stay on this file system

    Examples

    How to Use These Examples

    The examples below show common ways to use the cp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    cp file.txt backup.txt

    Copy file.txt to backup.txt in the current directory.

    cp file.txt /path/to/directory/

    Copy file.txt to another directory.

    cp -r dir1/ dir2/

    Copy directory dir1 and all its contents to dir2.

    Advanced Examples:

    cp -a source_dir/ destination_dir/

    Archive mode: preserve all file attributes and copy recursively.

    cp -i file.txt destination/

    Interactive mode: prompt before overwriting files.

    cp -u *.txt destination/

    Update mode: copy only when source is newer than destination.

    cp -v source.txt destination/

    Verbose mode: show what files are being copied.

    cp --backup=numbered file.txt destination/file.txt

    Create numbered backups of existing files.

    cp -p file.txt destination/

    Preserve mode, ownership, and timestamps of the original file.

    cp -l original.txt hard_link.txt

    Create a hard link instead of copying the file.

    cp -s original.txt symbolic_link.txt

    Create a symbolic link instead of copying the file.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Key Points:

    • The cp command is one of the most fundamental file manipulation commands in Unix/Linux systems
    • By default, cp overwrites destination files without warning
    • Use the -i option for interactive prompt before overwriting files
    • The -r or -R option is required for copying directories
    • The -a option (archive) is useful for making exact copies including permissions, timestamps, and symbolic links
    • When copying directories, ensure the destination directory exists or cp will create a new directory with the source name

    Common Use Cases:

    • Creating backups of important files before editing them
    • Deploying files from a development environment to a production environment
    • Duplicating directory structures for testing or development
    • Moving configuration files to new locations
    • Creating sample files from templates
    • Copying user data when setting up new accounts

    Understanding Symbolic Links:

    • When copying files with cp (without special options), the contents of a symbolic link's target are copied
    • Use -P to avoid following symbolic links during copying
    • The -s option makes symbolic links instead of copying files
    • The -l option creates hard links instead of copying files (only works on the same filesystem)

    Safety Considerations:

    • Always use -i (interactive) mode when working with important files to prevent accidental overwrites
    • Consider using -n (no-clobber) in scripts to prevent overwriting existing files
    • Use -v (verbose) to see what files are being copied, especially for large operations
    • The --backup options can save previous versions of destination files
    • Be careful with symbolic links as they might point to locations you don't intend to modify

    Related Commands:

    • mv - Move (rename) files instead of copying them
    • rsync - Remote file copy with more features for synchronization
    • dd - Copy and convert files at the block level
    • scp - Secure copy between hosts on a network
    • tar - Archive files with ability to compress

    Tips & Tricks

    1

    Use cp -i for interactive mode to prevent accidental overwrites: cp -i important.txt backup/

    2

    Copy directory recursively with all attributes: cp -a source_dir/ destination/

    3

    Use cp -u to only update files that are newer in the source: cp -u *.py /backup/scripts/

    4

    Create hard links instead of copies to save space: cp -l largefile.iso /mnt/data/

    5

    Use cp -v (verbose) for large operations to see what's happening: cp -v *.txt /backup/

    6

    Backup files with numbering scheme: cp --backup=numbered config.ini /etc/

    7

    Copy a file while preserving all metadata: cp -p myfile.txt /backup/

    8

    Exclude certain files when copying recursively: cp -r source/ dest/ --exclude="*.tmp"

    Related Commands

    mv

    mv

    View command

    rsync

    rsync

    View command

    dd

    dd

    View command

    scp

    scp

    View command

    tar

    tar

    View command

    ln

    ln

    View command

    install

    install

    View command

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the cp command works in different scenarios.

    $ cp
    View All Commands