Bash Copy Files
cp duplicates files and, with -r, entire directory trees, leaving the original untouched at the source.
Copying a Single File
cp takes a source and a destination and creates a copy of the source at the destination, leaving the original file exactly as it was. If the destination is an existing directory, the file is copied into it under its original name.
Copy a file
$ cp report.txt report-backup.txt
$ ls
report-backup.txt report.txtCopying Into a Directory
When the destination argument is a directory, cp places the copy inside it using the same filename. You can copy multiple files this way in a single command by listing them all before the final destination directory.
Copy several files into a folder
$ cp invoice1.pdf invoice2.pdf archive/
$ ls archive
invoice1.pdf invoice2.pdfCopying Entire Directories with -r
By default cp refuses to copy a directory — it only works on files. Add -r (recursive) and cp walks the entire directory tree, recreating every subdirectory and file inside the destination.
Copy a whole project folder
$ cp -r site/ site-backup/
$ find site-backup -maxdepth 2
site-backup
site-backup/index.html
site-backup/assets
site-backup/assets/logo.png- cp src.txt dest.txt — copy a file to a new name
- cp file.txt folder/ — copy a file into a directory, keeping its name
- cp -r folder1/ folder2/ — copy a directory and everything inside it
- cp -i src.txt dest.txt — ask for confirmation before overwriting
- cp -u src.txt dest.txt — copy only if src is newer than dest