How to Force cp Command to Overwrite without Confirmation

The cp command (which stands for a copy) is one of the commonly used commands on Linux and other UNIX-like operating systems, for copying files and directories. In this guide, we will show how to force the cp command to overwrite a copy operation without confirmation in Linux.

Usually, when you run a cp command, it overwrites the destination file(s) or directory as shown.

# cp bin/git_pull_frontend.sh test/git_pull_frontend.sh

To run cp in interactive mode so that it prompts you before overwriting an existing file or directory, use the -i flag as shown.

# cp -i bin/git_pull_frontend.sh project1/git_pull_frontend.sh

By default, modern Linux distributions especially those in the Red Hat Enterprise Linux (RHEL) family come with an alias for the cp command which makes a user run the cp command in interactive mode. This may not be the case on Debian and Ubuntu derivatives.

To check all your default aliases, run the alias command as shown.

# alias
View All Linux Aliases
View All Linux Aliases

The highlighted alias in the above screenshot implies that when you run the command, by default it will run in interactive mode. Even when you use the yes command, the shell will still prompt you to confirm the overwrite.

# yes | cp -r bin test
Run Copy Command With Confirmation
Run Copy Command With Confirmation

The best way to force the overwrite is to use a backward slash before the cp command as shown in the following example. Here, we are copying contents of the bin directory to test directory.

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

Alternatively, you can unalias the cp alias for the current session, then run your cp command in the non-interactive mode.

# unalias cp
# cp -r bin test
Unalias cp Command Alias
Unalias cp Command Alias

For more information, see the cp command man page.

# man cp

If you have any questions, ask us via the feedback form below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
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.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

9 thoughts on “How to Force cp Command to Overwrite without Confirmation”

  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.