Beginner · 7 min · practise every command in the built-in terminal

cp — Copy Files and Directories

Basic copy

cp source.txt destination.txt
cp documents/hello.txt hello-backup.txt

Copy to a directory

cp file.txt documents/
cp file.txt documents/newname.txt   # Copy and rename

Copy multiple files

cp file1.txt file2.txt directory/

Copy a directory (recursive)

cp -r documents/ documents-backup/

The -r flag (recursive) is required for directories.

Useful flags

cp -i file.txt dest/    # Interactive: ask before overwriting
cp -v file.txt dest/    # Verbose: show what's being copied
cp -p file.txt dest/    # Preserve timestamps and permissions
cp -u file.txt dest/    # Only copy if source is newer

Wildcards

cp *.txt documents/      # Copy all .txt files
cp file?.txt backup/     # ? matches any single character

Practice tasks

  1. Make a copy of README.txt called README-backup.txt
  2. Copy the entire documents/ directory to documents-backup/
  3. Copy README.txt into the documents/ directory
  4. Copy a file with verbose output so you can see what's happening

Commands in this lesson

Files & Directories