15+ cp Commands for Linux Beginners

cp command is available in almost all Unix and Linux like operating systems

Being a Linux user, copying files and directories is one of the most common day to day operations task.cp command is used to copy the files and directories from one local place to another using command line. cp command is available in almost all Unix and Linux like operating systems.

In this article we will demonstrate 16 useful cp command examples specially for the Linux beginners. Following is the basic syntax of cp command.

 

Copy a file to another file

# cp {options} source_file target_file

Copy File(s) to another directory or folder

# cp {options} source_file   target_directory

Copy directory to directory

# cp {options} source_directory target_directory

Let’s jump into the practical examples of cp command,

Also Read: Introduction To Shell Scripting

 

1. Copy file to target directory

Let’s assume we want copy the /etc/passwd file to /mnt/backup directory for some backup purpose, so run below cp command,

root@datamounts:~# cp /etc/passwd /mnt/backup/
root@datamounts:~#

Use below command to verify whether it has been copied or not.

root@datamounts:~# ls -l /mnt/backup/
total 4
-rw-r--r-- 1 root root 2410 Feb  3 17:10 passwd
root@datamounts:~#

2. Copying multiple files at the same time

Let’s assume we want to copy multiples (/etc/passwd, /etc/group & /etc/shadow) at same time to target directory (/mnt/backup)

root@datamounts:~# cp /etc/passwd /etc/group /etc/shadow /mnt/backup/
root@datamounts:~#

3. Copying the files interactively (-i)

If you wish to copy the files from one place to another interactively then use the “-i” option in cp command, interactive option only works if the destination directory already has the same file, example is shown below,

root@datamounts:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'? y
root@datamounts:~#

In the above command one has to manually type ‘y’ to allow the copy operation

4. Verbose output during copy command (-v)

If you want the verbose output of cp command then use “-v” option, example is shown below

root@datamounts:~# cp -v /etc/fstab  /mnt/backup/
'/etc/fstab' -> '/mnt/backup/fstab'
root@datamounts:~#

In case you want to use both interactive mode and verbose mode then use the options “-iv”

root@datamounts:~# cp -iv /etc/fstab  /mnt/backup/
cp: overwrite '/mnt/backup/fstab'? y
'/etc/fstab' -> '/mnt/backup/fstab'
root@datamounts:~#

5. Copying a directory or folder (-r or -R)

To copy a directory from one place to another use -r or -R option in cp command. Let’s assume we want to copy the home directory of datamounts user to “/mn/backup”,

root@datamounts:~# cp -r /home/datamounts /mnt/backup/
root@datamounts:~#

In above command, -r option will copy the files and directory recursively.

Now verify the contents of datamounts directory on target place,

root@datamounts:~# ls -l /mnt/backup/datamounts/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:41 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:41 file_5.txt
root@datamounts:~#

 

Also Read: 30+ Commands to understand Firewalld in RHEL7 environment

6. Archive files and directory during copy (-a)

While copying a directory using cp command we generally use -r or -R option, but in place of -r option we can use ‘-a’ which will archive the files and directory during copy, example is shown below,

root@datamounts:~# cp -a /home/datamounts /mnt/backup/
root@datamounts:~# ls -l /mnt/backup/datamounts/
total 24
drwxr-xr-x 2 root root 4096 Feb  3 17:41 data
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_1.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_2.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_3.txt
-rw-r--r-- 1 root root    7 Feb  3 17:39 file_4.txt
-rw-r--r-- 1 root root    7 Feb  3 17:40 file_5txt
-rw-r--r-- 1 root root    0 Feb  3 17:39 file_5.txt
root@datamounts:~#

7. Copy only when source file is newer than the target file (-u)

There can be some scenarios where you want copy the files only if the source files are newer than the destination ones. This can be easily achieved using “-u” option in the cp command.

In the Example:6  we have copied the datamounts home directory to /mnt/backup folder, in the datamounts home folder we have 5 txt files, let’s edit couple of them and then copy all the txt files using “cp -u”.

root@datamounts:~# cd /home/datamounts/
root@datamounts:/home/datamounts# echo "LinuxRocks" >> file_1.txt
root@datamounts:/home/datamounts# echo "LinuxRocks" >> file_4.txt
root@datamounts:/home/datamounts# cp -v -u  file_*.txt /mnt/backup/datamounts/
'file_1.txt' -> '/mnt/backup/datamounts/file_1.txt'
'file_4.txt' -> '/mnt/backup/datamounts/file_4.txt'
root@datamounts:/home/datamounts#

 

8. Do not overwrite the existing file while copying (-n)

There are some scenarios where you don’t want to overwrite the existing destination files while copying. This can be accomplished using the option ‘-n’ in ‘cp’ command

root@datamounts:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'?

As you can see in above command, it is prompting us to overwrite the existing file, if you use -n then it will not prompt for the overwrite and also will not overwrite the existing file.

root@datamounts:~# cp -n /etc/passwd /mnt/backup/
root@datamounts:~#

9. Creating symbolic links using cp command (-s)

Let’s assume we want to create symbolic link of a file instead copying using cp command, for such scenarios use ‘-s’ option in cp command, example is shown below

root@datamounts:~# cp -s /home/datamounts/file_1.txt /mnt/backup/
root@datamounts:~# cd /mnt/backup/
root@datamounts:/mnt/backup# ls -l file_1.txt
lrwxrwxrwx 1 root root 27 Feb  5 18:37 file_1.txt -> /home/datamounts/file_1.txt
root@datamounts:/mnt/backup#

10. Creating Hard link using cp command (-l)

If you want to create hard link of a file instead copy using cp command, then use ‘-l’ option. example is shown below,

root@datamounts:~# cp -l /home/datamounts/devops.txt /mnt/backup/
root@datamounts:~#

As we know in hard link, source and linked file will have the same inode numbers, let’s verify this using following commands,

root@datamounts:~# ls -li /mnt/backup/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@datamounts:~# ls -li /home/datamounts/devops.txt
918196 -rw-r--r-- 2 root root 37 Feb  5 20:02 /home/datamounts/devops.txt
root@datamounts:

11. Copying attributes from source to destination (–attributes-only)

If you want to copy only the attributes from source to destination using cp command, then use option “–attributes-only

root@datamounts:/home/datamounts# cp --attributes-only /home/datamounts/distributions.txt /mnt/backup/
root@datamounts:/home/datamounts# ls -l /home/datamounts/distributions.txt
-rw-r--r-- 1 root root 41 Feb  5 19:31 /home/datamounts/distributions.txt
root@datamounts:/home/datamounts# ls -l /mnt/backup/distributions.txt
-rw-r--r-- 1 root root 0 Feb  5 19:34 /mnt/backup/distributions.txt
root@datamounts:/home/datamounts#

In the above command, we have copied the distribution.txt file from datamounts home directory to /mnt/backup folder, if you have noticed, only the attributes are copied, and content is skipped. Size of distribution.txt under /mn/backup folder is zero bytes.

12. Creating backup of existing destination file while copying (–backup)

Default behavior of cp command is to overwrite the file on destination if the same file exists, if you want to make a backup of existing destination file during the copy operation then use ‘–backup‘ option, example is shown below,

root@datamounts:~# cp --backup=simple -v /home/datamounts/distributions.txt /mnt/backup/distributions.txt
'/home/datamounts/distributions.txt' -> '/mnt/backup/distributions.txt' (backup: '/mnt/backup/distributions.txt~')
root@datamounts:~#

If you have noticed, backup has been created and appended tilde symbol at end of file. backup option accept following parameters

  • none, off  – never make backups
  • numbered, t – make numbered backups
  • existing, nil – numbered if numbered backups exist, simple otherwise
  • simple, never – always make simple backups

13. Preserve mode, ownership and timestamps while copying (-p)

If you want to preserve the file attributes like mode, ownership and timestamps while copying then use -p option in cp command, example is demonstrated below,

root@datamounts:~# cd /home/datamounts/
root@datamounts:/home/datamounts# cp -p devops.txt /mnt/backup/
root@datamounts:/home/datamounts# ls -l devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 devops.txt
root@datamounts:/home/datamounts# ls -l /mnt/backup/devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@datamounts:/home/datamounts#

14. Do not follow symbolic links in Source while copying (-P)

If you do not want to follow the symbolic links of source while copying then use -P option in cp command, example is shown below

root@datamounts:~# cd /home/datamounts/
root@datamounts:/home/datamounts# ls -l /opt/nix-release.txt
lrwxrwxrwx 1 root root 14 Feb  9 12:28 /opt/nix-release.txt -> os-release.txt
root@datamounts:/home/datamounts#
root@datamounts:/home/datamounts# cp -P os-release.txt /mnt/backup/
root@datamounts:/home/datamounts# ls -l /mnt/backup/os-release.txt
-rw-r--r-- 1 root root 35 Feb  9 12:29 /mnt/backup/os-release.txt
root@datamounts:/home/datamounts#

Note: Default behavior of cp command is to follow the symbolic links in source while copying.

15. Copy the files and directory forcefully using -f option

There can be some scenarios where existing destination file cannot be opened and removed. And if you have healthy file which can be copied in place of existing destination file, then use cp command along with -f option

root@datamounts:/home/datamounts# cp -f distributions.txt  /mnt/backup/
root@datamounts:/home/datamounts#

16. Copy sparse files using sparse option in cp command

Sparse is a regular file which contains long sequence of zero bytes that doesn’t consume any physical disk block. One of benefit of sparse file is that it does not consume much disk space and read operation on that file would be quite fast.

Let’s assume we have sparse cloud image named as “ubuntu-cloud.img”

root@datamounts:/home/datamounts# du -sh ubuntu-cloud.img
12M     ubuntu-cloud.img
root@datamounts:/home/datamounts# cp --sparse=always ubuntu-cloud.img /mnt/backup/
root@datamounts:/home/datamounts# du -sh /mnt/backup/ubuntu-cloud.img
0       /mnt/backup/ubuntu-cloud.img
root@datamounts:/home/datamounts#

Different options can be used while using sparse parameter in cp command,

  • sparse=auto
  • sparse-always
  • sparse=never

That’s all from this article, I hope it helps you to understand the cp command more effectively. Please do share your feedback and comments

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More