githubEdit

rectangle-terminalcp

πŸ“ cp Command in Linux

The cp command is used to copy files and directories in Linux.


🧰 Common Options

Option
Description

-r

Copy directories recursively

-i

Prompt before overwriting an existing file

-u

Copy only when the source file is newer than the destination file

-v

Verbose mode, show files being copied

-n

Do not overwrite an existing file

-p

Preserve file attributes (timestamps, mode, ownership)

-a

Archive mode (preserve structure, symlinks, and attributes)

--backup

Make a backup of the destination file before overwriting


πŸ“‹ Basic Syntax

cp [options] [source] [destination]
  • source: File or directory to copy.

  • destination: The location to copy the source to.


πŸ“ Common Commands

Copy a File

Copy a Directory Recursively

Verbose Mode (Show Progress)

Copy and Preserve File Attributes

Copy Without Overwriting Existing Files

Copy with Backup (e.g., file1.txt becomes file1.txt~)


βš™οΈ Copy Multiple Files to a Directory

Copy Multiple Files to a Directory


πŸ’‘ Additional Tips

  • Use -i for safety: If you are unsure whether files will be overwritten, use -i to be prompted before overwriting.

  • Combine with find: You can combine cp with find to copy files that meet certain criteria.


Last updated