Linux cp yes to all

When you try to copy files or folders in Linux using the “cp” command, and the location already contains a file with the same filename, it can ask you whether you want to overwrite the old file:

[root@webserver]# cp -r /aaa/aaa.txt /bbb/bbb.txt
cp: overwrite '/aaa/aaa.txt'?

Normally you enter “y” or “n” here, but this message will pop up for every file you are trying to copy. So if you are trying to copy a folder with thousands of files you will get this question thousands of times. Lets try to speed this up!

1. Try the -rf option

The first thing you should try is use the “cp” function with the “-rf” flag set.

[root@webserver]# cp -rf /aaa/aaa.txt /bbb/bbb.txt

However, if it still asks you if you want to overwrite the target file, there is a big chance the cp function is aliased in your .bashrc or .profile. In this case, read on.

2. Call cp with an absolute location

Instead of using the “cp” alias, try calling “cp” directly. On CentOS 8.x cp is found in “/bin”. Try the following:

[root@webserver]# /bin/cp -rf /aaa/aaa.txt /bbb/bbb.txt
Categorized as UNIX Shell

By Leendert de Borst

Freelance software architect with 10+ years of experience. Expert in translating complex technical problems into creative & simple solutions.

Leave a comment

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