In Linux, the cp command by default prompts for confirmation before overwriting (due to the system alias alias cp=’cp -i’). Here are several methods to force overwrite without prompting:
1. Bypass the alias using a backslash
\cp -rf source_file destination_file
The backslash \ ignores the alias and directly calls the native cp command, where -rf forces recursive copying.
2. Use the absolute path of the cp command
/bin/cp -rf source_file destination_file
This directly calls the cp command from the system path, avoiding alias interference.
3. Temporarily remove the alias
unalias cp # Temporarily remove the alias cp -rf source_file destination_file
Or remove it permanently: edit ~/.bashrc, comment out alias cp=’cp -i’, and then execute source ~/.bashrc.
4. Use the yes command for automatic confirmation
yes | cp -rf source_file destination_file
The yes command automatically inputs ‘y’ to confirm overwriting, suitable for script scenarios.
5. Notes on the -f option
Using cp -f alone may be ineffective (due to alias overriding), so it should be combined with the above methods.
Summary: It is recommended to use \cp -rf or the absolute path for simplicity and efficiency; if permanent effect is needed, modify the .bashrc file. It is advisable to back up important data before performing operations.