Beginners Guide: Overwrite All Files With ‘cp’ Command In Linux

“cp” is one of the most frequently used commands to copy files or directories in a Linux environment. “cp” is short for “copy”. This Linux command is to copy files or directories in one server environment. To copy files or directories to another remote host you can use “rsync” or “scp” instead.

But when you want to copy and overwrite a whole directory with many files in it, it will keep asking you if you want to overwrite each one or not. Can you imagine when you are copying 1000 files and you need to answer “yes” 1000 times? So here is a little trick to answer “yes” for all the overwrite prompts.

yes | cp -R <source path> <target path>

The above command will answer all the overwrite prompts with “yes”, and overwrite all the files and directories.

The other alternative you can use is the rsync command, which also overwrites your target folder.

Warning: make sure you understand what you are doing since you cannot roll back the overwritten files.