A Complete Guide to Forcing cp Command to Overwrite without Confirmation

A Complete Guide to Forcing cp Command to Overwrite without Confirmation

cp Command (or the copy command) is used for copying files and/or directories from one location to another in Linux. It is a quite common command. If you’ve been using it for a while, you should be familiar with the overwriting scenario. Under usual circumstances, it overwrites destination files as shown below:

Code:
# cp bin/git_pull_frontend.sh test/git_pull_frontend.sh
However, you’ll be prompted when the cp Command tries to overwrite the destination folder when you run cp in interactive mode like this:

Code:
# cp -i bin/git_pull_frontend.sh project1/git_pull_frontend.sh
The above command uses the -i alias, which confirms that you’re running the command in interactive mode. Modern Linux distributions, primarily those in the RHEL family, come with that alias for the cp command. So you’re going to get the prompt message.

To check if that’s the case, you can run the following command and see all the default aliases:

Code:
# alias
If you see the alias cp=‘cp-i’ in the list, then you should know that cp will run in interactive mode. You’d have to type yes every time when you’re prompted.

To get past this requirement, you need to execute the cp command with a backward slash like this:

Code:
# \cp -r bin test
In the above command, you’re copying the files in bin to test folder. You will not get the prompt message even in interactive mode.

Another way to force cp command to overwrite without confirmation is to unalias the cp alias. Here’s how to do it:

Code:
# unalias cp
Next time when you run cp, you will not get the confirmation prompt.

So that’s how you deal with cp prompt messages. For more information, check the cp command man page:

Code:
# man cp
Author
kumkumsharma
Views
2,492
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top