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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
- Although the default behavior of
cpis to overwrite without prompting, some systems may have an alias set up so that the
cpcommand 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
cpcommand. For example:
$ yes | cp -i file1 file2 file3 test/ cp: overwrite 'test/file1'? cp: overwrite 'test/file2'? cp: overwrite 'test/file3'?
yescommand is built for situations just like this, and will automatically say
y(same as yes) to all prompts that the
DID YOU KNOW?
cpcommand is prompting you for overwrite confirmation, even when you do not explicity use the
-iflag, then your system must have an alias to
cp -iconfigured (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.
- Another way to bypass your
cp -ialias is by using the full path to your
$ /bin/cp file1 file2 file3 test/
- 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 -n file1 file2 file3 test/
Note that the
-noption will override
-i, so this option will also get rid of the repetitive prompts from the
cpcommand, but is the equivalent to answering NO to all prompts.
In this tutorial, we saw how to say YES to ALL when using the
cpcommand 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
-iswitch in our