GO KALI FREE
File Management

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 destination

Practical example

cp -r /source/folder /destination/folder

Questions 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

touch

Creates an empty file or updates the timestamp of an existing file.

mv

Moves or renames files and directories.

mkdir

Creates a new directory (folder). Use -p to create nested directories.

rm

Removes (deletes) files or directories. Use -rf for recursive force delete.

diff

Compares two files line by line and shows the differences.

rmdir

Removes an empty directory. Safer than rm -r for removing directories.