cp Linux Command
Copies files or directories from one location to another.
What does cp do?
Copies files or directories from one location to another.
Syntax
cp [options] source destinationPractical example
cp -r /source/folder /destination/folderQuestions about cp
How do I copy a directory with cp?
Use the `-r` (recursive) flag: `cp -r source_dir dest_dir`. Without `-r`, cp will refuse to copy directories.
How do I preserve file permissions when copying?
Use `cp -p` to preserve permissions, timestamps, and ownership. Use `cp -a` for an archive copy that also follows symlinks.
How do I copy multiple files to a directory?
Use `cp file1 file2 file3 /destination/` or `cp *.txt /destination/` with a glob pattern to copy multiple files at once.
How do I copy files without overwriting?
Use `cp -i` to prompt before overwriting, or `cp -n` to never overwrite existing files.
What does cp -v do?
The `-v` (verbose) flag makes cp show each file as it is being copied, which is useful for tracking progress in scripts.
How do I copy files preserving symlinks?
Use `cp -P` to copy symbolic links as links rather than copying the files they point to.
How do I copy a file and rename it?
Specify the new filename as the destination: `cp original.txt backup.txt` creates a copy named backup.txt.
Related File Management
touchCreates an empty file or updates the timestamp of an existing file.
mvMoves or renames files and directories.
mkdirCreates a new directory (folder). Use -p to create nested directories.
rmRemoves (deletes) files or directories. Use -rf for recursive force delete.
diffCompares two files line by line and shows the differences.
rmdirRemoves an empty directory. Safer than rm -r for removing directories.