The cp command in Linux is used to copy files and directories, but by default, it may prompt for a confirmation before overwriting an existing destination. If you’re running the cp command from some script, these prompts can interrupt the execution. To ensure the cp command overwrites files without confirmation, you can bypass aliases or use the yes command. Below, you’ll find practical ways to achieve this.
Why Does `cp` Ask for Confirmation?
On some Linux systems, cp may be aliased to cp -i, meaning it always prompts users before overwriting files. You can check whether this alias exists by running:
$ alias cp
- sample output -
alias cp='cp -i
If you see alias cp='cp -i', this is why the command asks for confirmation.
Solutions to Force `cp` to Overwrite Without Prompt
Call the cp binary directly: Instead of using the aliased version of cp, run:
$ /bin/cp SOURCE DEST
This bypasses the alias and ensures files are copied without confirmation.
Use the yes command: If cp prompts for confirmation, you can also automate the response by piping yes into the command:
$ yes | cp SOURCE DEST
This automatically confirms any overwrite prompt, ensuring smooth execution in automated workflows.
Conclusion
If the Linux cp command is prompting for confirmation, it’s likely due to an alias. You can either bypass it by calling /bin/cp directly or automate confirmation with the yes command. This allows the files to be copied without prompts, avoiding interruption of the automated scripts.
Related Articles: