How to say YES to ALL with cp command

The cp command is used to copy files and directories on a Linux system. If a user tries to copy a file over to a location that already contains the same file name, the default behavior of cp is to overwrite the destination file with the source file. However, on some Linux systems, this behavior can be configured differently, and the user might see a prompt to confirm overwriting in their terminal. When copying many files, this prompt can get repetitive and annoying to deal with. In this tutorial, you will learn how to say YES to ALL with the cp command when trying to copy files via the Linux command line.

In this tutorial you will learn:

  • How to say YES to ALL with cp command
How to say YES to ALL with cp command
How to say YES to ALL with cp command
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software cp
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

cp command – say YES to ALL

If you have ended up here, you most likely are trying to copy a lot of files, and do not want to keep getting prompted for confirmation on overwriting files that have duplicate names each time. In the examples below, we will go over a few different scenarios in which you may face repetitive prompts, and the solutions to each of these problems.

  1. Although the default behavior of cp is to overwrite without prompting, some systems may have an alias set up so that the cp command automatically appends the -i (interactive) option, which will always prompt a user when file names collide. We can say YES to ALL of the prompts that show up by piping the output of the yes command to our cp command. For example:
    $ yes | cp -i file1 file2 file3 test/
    cp: overwrite 'test/file1'? 
    cp: overwrite 'test/file2'? 
    cp: overwrite 'test/file3'?

    The yes command is built for situations just like this, and will automatically say y (same as yes) to all prompts that the cp command produces.

    If your cp command is prompting you for overwrite confirmation, even when you do not explicity use the -i flag, then your system must have an alias to cp -i configured (this is common on RHEL based distros when using the root account). Check out our tutorial on How to remove an alias on Linux for help with getting rid of that alias.
  2. Another way to bypass your cp -i alias is by using the full path to your cp executable.
    $ /bin/cp file1 file2 file3 test/
  3. Just in case you are working with a variety of files and want to ensure that you do not accidentally overwrite any destination files that have the same name as your source files, you can use the -n (no clobber) option with cp:
    $ cp -n file1 file2 file3 test/

    Note that the -n option will override -i, so this option will also get rid of the repetitive prompts from the cp command, but is the equivalent to answering NO to all prompts.

Closing Thoughts

In this tutorial, we saw how to say YES to ALL when using the cp command on a Linux system. This allows us to bypass the annoying and repetitive prompts that show up when we try to copy a lot of files to a destination directory that contains files of the same name as our source files, or when we forget to turn off the -i switch in our cp command.