How to Copy Files and Directories with the Linux cp Command
The cp command copies files and directories. The basic form is cp source destination, so cp notes.txt notes-backup.txt creates a copy of notes.txt. Copying a folder needs an extra flag, which trips up beginners — this guide covers it.
Copy a single file
Give cp a source file and a destination. The destination can be a new file name or an existing directory.
Copy a directory with -r
Copying a directory with a plain cp fails with the message omitting directory. Add -r (recursive) to copy the folder and everything inside it.
Keep permissions with -p
By default cp sets the copy's permissions to your default umask. Use -p to preserve the original permissions, ownership and timestamps, which matters when backing up config files.
Never overwrite with -n
cp silently overwrites an existing destination file. Add -n to skip files that already exist, or -i to be asked before each overwrite.
Copy like a pro
Combine flags in one short call. cp -rva copies recursively, preserves everything, prints each file as it goes, and is the workhorse for moving a site or project folder.
To sync folders across machines instead, see rsync. The cp reference page lists every flag.
Keep learning
Related guides: The Linux ls Command: List Files Like a Pro · How to Find Files and Directories with the Linux find Command · Linux chmod Command: A Complete Permissions Guide. Read all tutorials on the Learn Linux blog.