CP
command in Linux is used to copy files and folders from one location to other. It can also preserve the file’s permissions, copy the file to another location with a different name and much more. Let’s see some copy command examples to understand it better.

cp [OPTION] Source Destination cp [OPTION] Source Directory cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
The first and second syntax is used to copy the Source file to the Destination file or Directory. The third syntax is used to copy multiple Sources(files) to Directory.
Simple copy command
below is the simple copy command,
$ cp a.txt b.txt
Copy two directories
The copy command is smart enough to determine if the passed argument is a file or directory. If the copy command contains two directories, then it will copy the contents of the first directory to the second one. You should also pass -R
option to copy recursively
$ cp -R source_directory dest_directory
Preserving permissions.
When copying files, if it is very important for you to preserve the permission of the file then you can use -p
. This will preserve the permissions of the file, along with the things mentioned below.
- Modification time/date
- Access time
- File flags
- File mode
- User ID (UID)
- Group ID (GID)
- Access Control Lists (ACLs)
- Extended Attributes (EAs)
Wild card copy
The star wildcard means anything. This means it will copy all the files in a directory to a new directory.
$ cp * /home/justgeek/backup
You can also use it to copy a specific type of extension. So, to copy all the HTML files (*.html) in a directory to a new directory, you would have to use.
$ cp *.doc /home/justgeek/backup
Avoid confirmations
While copying files, using the cp command you would have noticed that it always asks for the confirmation overwriting files like below.
$ cp -v test.txt example.txt cp: overwrite ‘example.txt’?
Will see how you can avoid that, but before that let’s see why it happens.
This happens because of alias set in the .bashrc file.
[root@server ~]# cat .bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
you can remove the alias in the .bashrc file, and you won’t be asked for confirmation again.
but if you want to skip confirmation for time being then you can call the cp command directly from bin – Example.
[root@server ~]# /bin/cp -v test.txt example.txt ‘test.txt’ -> ‘example.txt’ [root@server ~]#
I hope you have got know something new through my post Copy command examples. Also, I hope you have already read grep command examples