Thursday, 28 March 2019

16 Useful ‘cp’ Command Examples for Linux Beginners

cp Command, Linux Tutorial and Material, LPI Study Material, LPI Guides

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,

Example: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@lpicentral:~# cp /etc/passwd /mnt/backup/
root@lpicentral:~#

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

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

Example: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@lpicentral:~# cp /etc/passwd /etc/group /etc/shadow /mnt/backup/
root@lpicentral:~#

Example: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@lpicentral:~# cp -i /etc/passwd /mnt/backup/
cp: overwrite '/mnt/backup/passwd'? y
root@lpicentral:~#

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

Example: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@lpicentral:~# cp -v /etc/fstab  /mnt/backup/
'/etc/fstab' -> '/mnt/backup/fstab'
root@lpicentral:~#

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

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

Example: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 lpicentral user to “/mn/backup”,

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

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

Now verify the contents of lpicentral directory on target place,

root@lpicentral:~# ls -l /mnt/backup/lpicentral/
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@lpicentral:~#

Example: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@lpicentral:~# cp -a /home/lpicentral /mnt/backup/
root@lpicentral:~# ls -l /mnt/backup/lpicentral/
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@lpicentral:~#

Example: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 lpicentral home directory to /mnt/backup folder, in the lpicentral home folder we have 5 txt files, let’s edit couple of them and then copy all the txt files using “cp -u”.

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

Example: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@lpicentral:~# 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@lpicentral:~# cp -n /etc/passwd /mnt/backup/
root@lpicentral:~#

Example: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@lpicentral:~# cp -s /home/lpicentral/file_1.txt /mnt/backup/
root@lpicentral:~# cd /mnt/backup/
root@lpicentral:/mnt/backup# ls -l file_1.txt
lrwxrwxrwx 1 root root 27 Feb  5 18:37 file_1.txt -> /home/lpicentral/file_1.txt
root@lpicentral:/mnt/backup#

Example: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@lpicentral:~# cp -l /home/lpicentral/devops.txt /mnt/backup/
root@lpicentral:~#

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

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

Example: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@lpicentral:/home/lpicentral# cp --attributes-only /home/lpicentral/distributions.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l /home/lpicentral/distributions.txt
-rw-r--r-- 1 root root 41 Feb  5 19:31 /home/lpicentral/distributions.txt
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/distributions.txt
-rw-r--r-- 1 root root 0 Feb  5 19:34 /mnt/backup/distributions.txt
root@lpicentral:/home/lpicentral#

In the above command, we have copied the distribution.txt file from lpicentral 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.

Example: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@lpicentral:~# cp --backup=simple -v /home/lpicentral/distributions.txt /mnt/backup/distributions.txt
'/home/lpicentral/distributions.txt' -> '/mnt/backup/distributions.txt' (backup: '/mnt/backup/distributions.txt~')
root@lpicentral:~#

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

Example: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@lpicentral:~# cd /home/lpicentral/
root@lpicentral:/home/lpicentral# cp -p devops.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 devops.txt
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/devops.txt
-rw-r--r-- 1 root root 37 Feb  5 20:02 /mnt/backup/devops.txt
root@lpicentral:/home/lpicentral#

Example: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@lpicentral:~# cd /home/lpicentral/
root@lpicentral:/home/lpicentral# ls -l /opt/nix-release.txt
lrwxrwxrwx 1 root root 14 Feb  9 12:28 /opt/nix-release.txt -> os-release.txt
root@lpicentral:/home/lpicentral#
root@lpicentral:/home/lpicentral# cp -P os-release.txt /mnt/backup/
root@lpicentral:/home/lpicentral# ls -l /mnt/backup/os-release.txt
-rw-r--r-- 1 root root 35 Feb  9 12:29 /mnt/backup/os-release.txt
root@lpicentral:/home/lpicentral#

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

Example: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@lpicentral:/home/lpicentral# cp -f distributions.txt  /mnt/backup/
root@lpicentral:/home/lpicentral#

Example: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@lpicentral:/home/lpicentral# du -sh ubuntu-cloud.img
12M     ubuntu-cloud.img
root@lpicentral:/home/lpicentral# cp --sparse=always ubuntu-cloud.img /mnt/backup/
root@lpicentral:/home/lpicentral# du -sh /mnt/backup/ubuntu-cloud.img
0       /mnt/backup/ubuntu-cloud.img
root@lpicentral:/home/lpicentral#

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

◈ sparse=auto
◈ sparse-always
◈ sparse=never

Related Posts

0 comments:

Post a Comment