2025-02-20
6 min read

How to Force cp to Overwrite Files Without Confirmation

How to Force cp to Overwrite Files Without Confirmation

You run cp source.txt dest.txt to copy a file, but you keep getting prompted "overwrite dest.txt?" even though you want to force the overwrite. How do you make cp skip the confirmation?

TL;DR

Use cp -f source dest to force overwrite without confirmation. If that doesn't work, you likely have an alias for cp that includes -i (interactive mode). Use /bin/cp -f to bypass the alias, or use \cp -f to temporarily disable the alias. To permanently fix it, remove the alias from your shell configuration files like ~/.bashrc or ~/.bash_aliases.

The cp command's behavior varies depending on your system's default configuration and any aliases that might be set.

Let's say you're copying a file and it keeps prompting you:

cp config.txt /etc/app/config.txt

Output:

cp: overwrite '/etc/app/config.txt'?

You have to type y and press Enter every time. For automated scripts or bulk operations, this is impractical.

Using the -f Flag

The -f (force) flag tells cp to overwrite existing files without asking:

cp -f config.txt /etc/app/config.txt

This should copy without prompting. But on many systems, this still prompts. Why?

The Alias Problem

Many Linux distributions set up an alias for cp that includes the -i (interactive) flag:

# Check if cp is aliased
alias cp

Output might show:

alias cp='cp -i'

This alias makes cp always run in interactive mode, prompting before overwrites. Even when you use -f, the alias adds -i, and -i takes precedence when both are present.

Bypassing the Alias

You have several options to bypass the alias:

Option 1: Use the full path to cp

/bin/cp -f source.txt dest.txt

This calls the actual cp command directly, skipping any aliases.

Option 2: Use backslash to escape the alias

\cp -f source.txt dest.txt

The backslash tells Bash to ignore aliases for this command.

Option 3: Use the command builtin

command cp -f source.txt dest.txt

The command builtin runs the command without aliases or functions.

Copying Multiple Files

When copying multiple files, force overwrite for all:

\cp -f *.txt /backup/

Or with full path:

/bin/cp -f *.conf /etc/myapp/

Recursive Copying with Force

For directories, combine -r (recursive) with -f:

\cp -rf source_dir/ dest_dir/

This copies the entire directory tree, overwriting files without prompts.

Removing the Alias Permanently

If you frequently need non-interactive cp, remove the alias from your shell configuration.

Check where the alias is defined:

grep -r "alias cp" ~/.*rc ~/.*profile ~/.bash_aliases

Common locations:

  • ~/.bashrc
  • ~/.bash_aliases
  • ~/.profile
  • /etc/bash.bashrc (system-wide)

Edit the file and comment out or remove the line:

nano ~/.bashrc

Find and comment out:

# alias cp='cp -i'

Or remove the line entirely. Save and reload your shell:

source ~/.bashrc

Now cp -f works as expected without needing workarounds.

When to Keep Interactive Mode

The -i alias exists for safety - it prevents accidental overwrites. Consider keeping it for interactive use and using explicit methods for scripts:

  • For manual commands: Let the alias provide safety
  • For scripts: Use /bin/cp -f to force overwrite

Using cp in Scripts

In scripts, always use the full path or escape the alias:

#!/bin/bash

SOURCE="/var/app/config.txt"
DEST="/backup/config.txt"

# Use full path to avoid alias issues
/bin/cp -f "$SOURCE" "$DEST"

echo "Backup created: $DEST"

Or at the top of your script, unalias cp:

#!/bin/bash

# Disable cp alias for this script
unalias cp 2>/dev/null || true

# Now cp behaves normally
cp -f source.txt dest.txt

The 2>/dev/null || true prevents errors if no alias exists.

Combining with Other Options

Common combinations with -f:

# Force overwrite and preserve attributes
\cp -fp source.txt dest.txt

# Force overwrite, preserve attributes, and be verbose
\cp -fpv source.txt dest.txt

# Recursive, force, preserve
\cp -rfp source_dir/ dest_dir/

# Copy only if source is newer than dest
\cp -fu source.txt dest.txt

The Difference Between -f and -n

Two related but opposite flags:

  • -f (force): Overwrite without prompting
  • -n (no-clobber): Never overwrite, no prompting
# Overwrite existing files
cp -f new.txt existing.txt

# Skip existing files (don't overwrite)
cp -n new.txt existing.txt

The -n flag is useful when you want to copy only files that don't exist in the destination.

Practical Example: Backup Script

A backup script that forcefully overwrites old backups:

#!/bin/bash
set -e

SOURCE_DIR="/var/www/app"
BACKUP_DIR="/backup/app-$(date +%Y%m%d)"

# Create backup directory
mkdir -p "$BACKUP_DIR"

# Force copy, preserving attributes
/bin/cp -rfp "$SOURCE_DIR"/* "$BACKUP_DIR"/

echo "Backup complete: $BACKUP_DIR"

# Keep only last 7 days of backups
find /backup -name "app-*" -type d -mtime +7 -exec rm -rf {} \;

Practical Example: Config File Deployment

Deploy configuration files, overwriting existing ones:

#!/bin/bash

CONFIG_SOURCE="./configs"
CONFIG_DEST="/etc/myapp"

# Array of config files to deploy
CONFIGS=(
    "app.conf"
    "database.conf"
    "cache.conf"
)

for config in "${CONFIGS[@]}"; do
    echo "Deploying $config..."
    /bin/cp -f "$CONFIG_SOURCE/$config" "$CONFIG_DEST/$config"
done

echo "All configs deployed"

Handling Permissions

If you get permission errors, you might need sudo:

# Force copy with sudo
sudo cp -f source.txt /etc/protected/dest.txt

# Or with full path
sudo /bin/cp -f source.txt /etc/protected/dest.txt

Checking What Will Be Overwritten

Before forcing overwrites, see what would be affected:

# List files that would be overwritten
for file in *.txt; do
    if [ -f "/backup/$file" ]; then
        echo "Would overwrite: /backup/$file"
    fi
done

# Then actually copy
\cp -f *.txt /backup/

Alternative: Using rsync

For more control over overwrites, consider rsync:

# Force overwrite with rsync
rsync -av --force source.txt dest.txt

# Overwrite only if source is newer
rsync -av source.txt dest.txt

rsync gives you more options for handling existing files and is generally safer for complex copy operations.

Security Considerations

Forcing overwrites can be dangerous:

  • You might overwrite important files unintentionally
  • No confirmation means no second chance
  • Consider using backups before force operations

For critical files, verify before overwriting:

# Create backup before force overwrite
DEST="/etc/app/config.txt"
[ -f "$DEST" ] && cp "$DEST" "$DEST.backup.$(date +%s)"

# Now safe to force overwrite
\cp -f new-config.txt "$DEST"

Debugging cp Behavior

If cp is still prompting unexpectedly:

Check for aliases:

alias | grep cp

Check for functions:

type cp

See what command actually runs:

which cp

Test with full path:

/bin/cp --version

To force cp to overwrite without prompts, use the -f flag and bypass any aliases with /bin/cp or \cp. For production scripts, always use the full path to avoid alias surprises. Consider whether you need the safety of interactive mode for your use case.

Published: 2025-02-20|Last updated: 2025-02-20T09:00:00Z

Found an issue?