Sagar.BlogArticle
All posts
All posts
Linux

cp — Copy Files and Directories in Linux

Learn to copy files and directories with cp: recursive copies, preserving permissions, safe overwrite options, and archive mode for backups.

January 12, 20255 min read
LinuxFilesCommands

cp copies files and directories. The key rule to remember: you always need -r to copy a directory. Without it, cp refuses to copy directories at all.

Options

cp Options

OptionLong FormDescription
-r, -R--recursiveCopy directories recursively (required for dirs)
-i--interactivePrompt before overwriting
-f--forceForce overwrite without prompting
-n--no-clobberNever overwrite existing files
-u--updateCopy only when source is newer than destination
-v--verboseShow each file as it's copied
-p--preservePreserve permissions, ownership, timestamps
-a--archiveArchive mode = -dR --preserve=allbest for backups

How Destination Works

How cp behaves depends on whether the destination already exists and whether it's a directory:

  • Destination is a directory → file is copied into that directory keeping its name
  • Destination doesn't exist → file is copied with the new name
  • Destination exists as a file → file is overwritten (unless -n or -i)
cp-examples.sh
# ─── Copying files ───────────────────────────────────────────
cp file.txt backup.txt               # Copy and rename (backup.txt is new name)
cp file.txt /path/to/dir/            # Copy into directory (keeps name: dir/file.txt)
cp file.txt /path/to/dir/newname.txt # Copy into directory with new name
cp file1.txt file2.txt dir/          # Copy multiple files into a directory

# ─── Copying directories ─────────────────────────────────────
cp -r folder/ folder_backup/         # Copy entire directory tree
cp -r dir1 dir2 destination/         # Copy multiple directories
cp -rv folder/ backup/               # Verbose: see each file copied

# ─── Safety ──────────────────────────────────────────────────
cp -i source.txt dest.txt            # Ask before overwriting
cp -n source.txt dest.txt            # Never overwrite (silently skip)
cp -u source.txt dest.txt            # Only copy if source is newer

# ─── Preserving attributes ───────────────────────────────────
cp -p file.txt backup.txt            # Preserve: permissions, owner, timestamps
cp -a folder/ backup/                # Archive mode — preserves EVERYTHING
                                     # Use -a for backups, system copies

Use -a for Backups

cp -a (archive mode) is the recommended way to make a backup copy of a directory. It recursively copies everything and preserves file permissions, ownership, timestamps, and symlinks exactly. A plain cp -r might change timestamps or miss symlinks.

Common Patterns

# Backup a config file before editing it
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
# Now you can safely edit nginx.conf

# Copy all .txt files to a directory
cp *.txt /backup/

# Interactive copy (safe for important files)
cp -ri important-folder/ backup-folder/

# Sync: only copy newer files
cp -ru source/ destination/

# Create a hard link instead of copying (saves disk space)
cp -l file.txt hardlink.txt

# Create a symlink instead of copying
cp -s /path/to/original symlink.txt

Quick Check

You run `cp config.yaml /backup/` and `/backup/` is an existing directory. Where does the file end up?

Exercise
  1. Create a file with some content: echo "important data" > data.txt
  2. Make a backup: cp data.txt data.txt.bak
  3. Create a directory backup/ and copy data.txt into it
  4. Now try: cp -n data.txt backup/data.txt — what happens (does it overwrite)?
  5. Create a directory src/ with a few files, then copy the whole thing with -a flag