cp (GNU) Cheatsheet #
The cp
command is used to copy files and directories in Unix-like operating systems.
Basic Commands #
Command/Option | Example | Description |
---|---|---|
cp file1 file2 | cp example.txt backup.txt | Copy example.txt to backup.txt |
cp -r dir1 dir2 | cp -r mydir backupdir | Copy directory mydir to backupdir |
cp -i file1 file2 | cp -i example.txt backup.txt | Prompt before overwriting files |
cp -u file1 file2 | cp -u example.txt backup.txt | Copy only when the source file is newer than the destination file |
Advanced Options #
Command/Option | Example | Description |
---|---|---|
cp -a src dest | cp -a mydir backupdir | Archive mode, copy directories recursively, preserving attributes |
cp -p file1 file2 | cp -p example.txt backup.txt | Preserve file attributes (timestamps, ownership) |
cp -l file1 file2 | cp -l example.txt linkfile | Create hard links instead of copying files |
cp -v file1 file2 | cp -v example.txt backup.txt | Verbose mode, show files as they are copied |
This cheatsheet covers essential GNU cp
options for copying files and directories.