Command Line Interface
File Management
Linux Commands
Operating Systems
Data Overwrite

How to force cp to overwrite without confirmation

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If cp is asking for confirmation before overwriting, the prompt often is not coming from cp alone. On many systems the real cause is an interactive alias such as alias cp='cp -i'. To make copying non-interactive, you need to force overwrite and also make sure you are not still invoking an alias or wrapper.

Use -f to force overwrite

The normal option is -f, short for force.

bash
cp -f source.txt backup/source.txt

For directories, combine it with recursive copying:

bash
cp -rf ./build-output ./release-copy

-f tells cp to remove the destination if necessary and continue without prompting. That is the standard choice in scripts and deployment steps where interactive confirmation would block execution.

Check whether cp is aliased to cp -i

In interactive shells, cp is often aliased to cp -i to protect against accidental overwrites. That means even cp -f can behave unexpectedly because the alias is expanded before the command runs.

Check the current shell behavior first:

bash
type cp
alias cp

If the command is aliased, bypass it with either a leading backslash or command:

bash
\cp -f source.txt destination.txt
command cp -f source.txt destination.txt

Those forms call the real command instead of the alias. This is one of the most common fixes when people say "cp -f still prompts me."

Write scripts so they are predictable

In scripts, prefer the real command explicitly and quote paths:

bash
1#!/bin/sh
2
3src="/tmp/export/data.csv"
4dst="/var/backups/data.csv"
5
6command cp -f -- "$src" "$dst"
7printf 'Copied %s to %s\n' "$src" "$dst"

There are two important details there:

  • 'command cp avoids shell aliases'
  • '-- stops option parsing in case a filename begins with a dash'

The quoting matters too. Without it, paths containing spaces or wildcard characters can produce the wrong copy target.

Avoid the wrong workaround

You will sometimes see suggestions such as:

bash
yes | cp source.txt destination.txt

That is usually a bad idea. It works around the prompt rather than fixing the cause, and it can mask whether the shell alias or command options are configured correctly.

If the goal is non-interactive overwrite, command cp -f is the cleaner and more intentional answer.

Know when cp is not the best tool

For simple file replacement, cp -f is fine. But if the real task is directory synchronization, preserving metadata, or copying only changed files, another tool may fit better.

Examples:

bash
cp -u config.ini /etc/myapp/config.ini
rsync -a --delete ./public/ ./deploy/public/
install -m 0644 app.conf /etc/myapp/app.conf

cp -u updates only when the source is newer. rsync is better for repeated tree synchronization. install is useful when you also want to set mode and ownership deliberately.

That distinction matters because sometimes the request to "force cp" is really a symptom that a deployment or backup workflow needs a more specific tool.

Be careful with recursive force copies

cp -rf is powerful and dangerous. A small path mistake can replace far more data than intended. Before using it in automation, validate:

  • the source path is exactly what you expect
  • the destination path is exactly what you expect
  • trailing slashes behave the way you intend

In operational scripts, it is often worth printing the resolved paths before the copy runs.

Common Pitfalls

The most common mistake is assuming the prompt comes from cp itself when the shell alias is actually responsible.

Another common problem is using cp -f inside a script without quoting paths. That turns a non-interactive copy into a path-parsing bug.

People also use yes | cp instead of fixing the actual command invocation. That hides the cause instead of addressing it.

Finally, cp -rf can do a lot of damage quickly. Make sure the source and destination are correct before using it in automation.

Summary

  • 'cp -f is the standard way to overwrite without confirmation.'
  • If prompts continue, check for cp -i aliases and bypass them with \cp or command cp.
  • In scripts, use quoted paths and -- for predictable behavior.
  • Do not rely on yes | cp when the real issue is alias expansion.
  • If the task is synchronization rather than copying, rsync or another tool may be the better answer.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions