How to Force cp Command to Overwrite without Confirmation

cp command stands for a copy. Here, we will show you how to force the cp command to overwrite a copy operation without confirmation in Linux.

 

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

 

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

 

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

 

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

 

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

 

For more information, see the cp command man page.

# man cp
Was this answer helpful? 0 Users Found This Useful (0 Votes)