force cp command to overwrite

How to Force CP Command to Overwrite Without Confirmation

cp command is one of the most commonly used command to copy files & directories from one location to another in Linux. In many cases, it will overwrite the files & directories at the destination, without asking for confirmation. In some cases, it may ask for confirmation before copying them. In this article, we will learn how to force cp command to overwrite without confirmation.


How to Force CP Command to Overwrite Without Confirmation

Here is a typical cp command used to copy file from one location to another.

$ cp /path/to/file /path/to/destination

Here is an example to copy files from /home/data.txt to /home/project

$ cp /home/data.txt /home/project

The above command copies & overwrites files & folders. If you want to cp command to display a confirmation before overwriting, you need to use -i option, to run cp command in interactive mode.

$ cp -i /path/to/file /path/to/destination

Many Linux systems have configured an alias for cp command to run in interactive mode. In other words, when you run just cp command, the Linux system will run ‘cp -i’ instead. So, although cp command is supposed to overwrite files & directories by default, it does not happen due to this alias.

To check all aliases in your system run the following command.

$ alias
alias cp='cp -i'
...

If you see the above alias command in output, it means that when you run cp command, the Linux system will run ‘cp -i’ instead.

There are a couple of ways to overcome this problem.

The first solution is to simply add a backslash right before cp command, to prevent Linux system from using the alias for cp command.

$ \cp -r /path/to/file /path/to/destination

Alternatively, you can unalias the cp command for current session by running the unalias command as shown below.

$ unalias cp
$ cp -r /path/to/file /path/to/destination

If you want to permanently disable alias for cp command, you need to open the ~/. bashrc (bash) or ~/. tcshrc (tcsh) files which generally contain all aliases in your system, and remove the following line from it.

alias cp='cp -i'

Save and close the file. Run the following command to apply changes.

$ source ~/. bashrc

In this article, we have learnt how to force cp command to overwrite without confirmation.

Also read:

How to Force Delete Directory in Linux
How to Upload File Asynchronously in JavaScript
How to Store Data in Local Storage in JavaScript
How to Display Local Storage Data in JavaScript
How to Enable, Disable & Install Yum Plugins

Leave a Reply

Your email address will not be published. Required fields are marked *