cp Command Options and Overwriting Without Prompts | yes and Backslash

cp asks whether to overwrite each file when you copy several files, and the job stalls. The cp command is short for Copy and copies files and directories. This post covers basic usage, the options, and two ways to overwrite everything without the prompt.
The cp command overwrites a file of the same name at the destination without asking. To prevent that, [alias cp='cp -i'] is set, so it asks for confirmation before overwriting.
✅ Basic Usage
The basic syntax is as follows.
cp -[options] <source file or directory> <destination path or file or directory path>Write what to copy first, then the destination or the new name.

# Copy a file to a file
cp file1 file2
# Copy a file into a directory
cp file1 dir1/
# Copy two files into a directory
cp file1 file2 dir1/ testuser]# cp dir1/ dir2/
cp: -r not specified; omitting directory 'dir1/'
Copying a directory requires the -r, -R or -a option.
- -r/-R : copy a directory and its contents recursively
- -a : recursive copy that keeps attributes (includes -r)
✅ Two Ways to Overwrite Everything with cp
Running the cp command asks [overwrite] because the [cp -i] alias is set for cp.
cp -a
cp: overwrite 'messages_ori'? y
~]# alias cp
alias cp='cp -i'That is fine for one or two files, but with many files you have to answer for each one.
In that case, the two methods below overwrite without the prompt.
yes | cp -ar /home/ ./The first is to put [yes |] in front, which answers y to every prompt so the copy goes straight through. The second is to put a backslash [\] in front of the command, which skips the alias and runs the original cp command.
\cp -ar /home/ ./✅ cp Command Options & Simulator
| Option | Description | |
|---|---|---|
| -a | --archiveCopy while keeping all of the original file permissions, owner, group and timestamps[keeps permissions, owner, group, timestamps and more] + [-d : copy the symbolic link itself] + [-R : copy directories recursively] | |
| -b | If the destination file already exists, back it up before copying | |
| -d | If the source is a symbolic link, copy the link itself | |
| -f | If the destination file exists, remove it forcibly and copy | |
| -i | --interactiveAsk the user for confirmation before overwritingalias cp='cp -i' | |
| -l(lowercase) | Copy as a hard link | |
| -P | Copy the symbolic link as it is without following it | |
| -p | Keep the owner, group, permissions and timestamps of the original | |
| -r, -R | Copy directories recursively (including subdirectories and files)The -r option is not standard POSIX and can cause problems with symbolic links and device files. | |
| -s | Copy as a symbolic link (the source must be given as an absolute path) | |
| -u | Do not overwrite when the destination file is newer or the same | |
| -v | Print copy progress (verbose)Shows a message for each completed copy | |
| -x | Does not copy when the source and destination are on different file systems. |
🔲 -a: Keep File Attributes & Copy Subdirectories and Files
The [-a] option alone is enough to copy subdirectories while keeping the attributes of the original files. However, some scripts and automation tools explicitly require [-r], so [-r] or [-R] is sometimes added as well.
testuser]# cp -a dir1 dir2
testuser]# ll dir*
dir1:
total 0
-rw-r--r-- 1 root root 0 Jun 8 14:11 file1
-rw-r--r-- 1 root root 0 Jun 8 14:15 file2
dir2:
total 0
-rw-r--r-- 1 root root 0 Jun 8 14:11 file1
-rw-r--r-- 1 root root 0 Jun 8 14:15 file2
testuser]# [-d : copy the symbolic link itself]+[-R : copy directories recursively]+[--preserve=all]
| Items included in --preserve=all | Description |
|---|---|
| mode | File permissions (rwx, etc.) |
| ownership | Owner (user) and group |
| timestamps | Timestamps such as the last modification time |
| links | Number of hard links (keeps the shared inode) |
| context | SELinux security context |
| xattr | Extended attributes (extra metadata stored with the file) |
| all | Shorthand that includes all of the items above |
🔲 -u: Copy Only New and Changed Files
- Copies only when no file of the same name exists at the destination, or when the source is newer
- Often used for backups and synchronization
testuser]# cp -u file1 file2
testuser]# ll file*
-rw-r--r-- 1 root root 0 Jun 8 14:11 file1
-rw-r--r-- 1 root root 0 Jun 8 14:30 file2
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.