How to Force cp Command to Overwrite Without Confirmation in Linux

The cp command (short for copy) is one of the most commonly used commands in Linux and other UNIX-like operating systems. It’s used to copy files and directories from one location to another on the same system.

If you’re copying files between systems over a network, the scp (secure copy) command is typically used instead of cp. Unlike cp, which only works locally, scp allows you to securely transfer files between your local machine and a remote server, or between two remote systems over SSH.

In this guide, we’ll focus on the cp command and show you how to force it to overwrite files without asking for confirmation on Linux.

Basic Usage of cp Command

Let’s say you’re working on a static website project and you want to copy the latest version of your HTML files from the dev directory to the live public_html directory.

cp dev/index.html /var/www/html/index.html

This will silently overwrite the existing index.html file in your web server’s root directory (/var/www/html) without any warning.

Run cp in Interactive Mode

If you want the cp command to prompt you before overwriting existing files, you can use the -i (interactive) option:

cp -i dev/index.html /var/www/html/index.html

This will ask for confirmation before replacing the destination file, which is useful when you want to avoid accidental overwrites.

Why cp Might Prompt You Even Without -i

On modern Linux distributions, especially those based on Red Hat Enterprise Linux (RHEL) and its derivatives, the cp command is often aliased to run in interactive mode by default.

To check all your default aliases, run the alias command and look for something like alias cp='cp -i' as shown.

alias
View All Linux Aliases
View All Linux Aliases

This means that even if you don’t explicitly use -i, the shell is adding it for you. So every cp command will act as if it’s in interactive mode.

Why yes | cp Doesn’t Work as Expected

Some users try to bypass the confirmation using the yes command:

yes | cp -r bin test

However, if cp is aliased to cp -i, the shell may still force the confirmation prompt, and piping yes might not work as intended.

Run Copy Command With Confirmation
Run Copy Command With Confirmation

To force the cp command to run without any alias (and thus avoid interactive mode), prefix it with a backslash (\):

\cp -r bin test
Force cp Command to Overwrite Files without Confirmation
Force cp Command to Overwrite Files without Confirmation

This tells the shell to use the actual binary /bin/cp instead of the aliased version. The files will be copied without any confirmation.

If you want to disable the alias for cp during your current terminal session, you can run:

unalias cp
cp -r bin test

Now, the cp command will behave in non-interactive mode, as expected. Note that this change only lasts for the current session.

If you want to permanently remove or modify the alias, edit your shell configuration file (~/.bashrc, ~/.zshrc, etc.) and remove or change the alias line.

Bonus Tip: Use the -f Option for Force

You can also use the -f (force) flag to tell cp to overwrite destination files without prompting, even if the files are write-protected:

cp -rf bin test

However, this won’t bypass the alias if one exists, so you still may need to unalias or use \cp.

For more details on all available options, refer to the man page:

man cp
Conclusion

In summary, while the cp command is straightforward, but the presence of default aliases on many Linux systems can lead to unexpected prompts during copy operations. To avoid confirmation messages and ensure files are overwritten without interaction, you can:

  • Use a backslash to bypass the alias: \cp
  • Temporarily unalias cp using unalias cp
  • Add the -f flag for forceful overwrites (only if alias is not active)

Understanding how your shell handles aliases and using the right options will save you time and prevent common mistakes when copying files in Linux.

If you have any questions or need clarification, feel free to ask via the comment form below – we’re always here to help!

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Aaron Kili
Aaron Kili
Contributor
About the author

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

9 Comments

Leave a Reply
  1. in bash, the “command” command will run the first argument without any shell functions or aliases.

    This is useful in something like:

    ls() { command ls -l - - color=always ¦ less -r ;}
    
    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.