Cyberithub

10 Best Examples of cp command in Linux

Advertisements

In this article, I will take you through the different usage of cp command in Linux. There can be various operations that can be performed in Linux OS using command line. Copying the contents from one location to another is one such operation. We generally use cp command in Linux to copy the contents from a source to destination in a Local System.

10 Best Examples of cp command in Linux 1

cp command in Linux

Also Read: How to use tar command in Solaris 11

1. Check cp command version

If you want to check cp command version, you need to use --version option with cp command in Linux as shown below. As you can see from output, current cp command version is 8.22. You can also check other useful information like Copyright Information in below Output.

[root@localhost ~]# cp --version
cp (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Torbjörn Granlund, David MacKenzie, and Jim Meyering.

NOTE:

Please note that I am running all the commands here through root user. You can use any other user if you want to run cp command. It really does not matter as long as user has necessary permissions.

2. Copy File attributes only 

If you only want to copy file attributes from one file to another then you need to use --attributes-only option with cp command in Linux as shown below. This option won't allow to copy any data from source to destination. Here I am trying to copy the attributes of file.txt to another file2.txt file.

[root@localhost example]# cp --attributes-only file.txt file2.txt
[root@localhost example]# ls -lrt
total 4
-rw-r--r-- 1 root root 41 Dec 29 17:36 file.txt
-rw-r--r-- 1 root root 0 Dec 29 17:45 file2.txt

--attributes-only: don't copy the file data, just the attributes

3. Create Hard Link File instead of Copying

If you want to create hard link of a file instead of copying then you need to use -l option with cp command in Linux as shown below. This option allows you to create a hard link of source file instead of copying the source to destination.

[root@localhost example]# cp -l file.txt file2.txt
[root@localhost example]# ls -lrt
total 8
-rw-r--r-- 2 root root 41 Dec 29 17:36 file.txt
-rw-r--r-- 2 root root 41 Dec 29 17:36 file2.txt

-l (hard link): hard link files instead of copying

4. Create Symbolic Links instead of Copying

If you to create a Symbolic Link instead of Copying the files, then you can use -s option with cp command in Linux as shown below. This option allows you to create a symbolic link of source file instead of copying it in the destination path.

[root@localhost example]# cp -s file.txt file2.txt
[root@localhost example]# ls -lrt
total 4
-rw-r--r-- 1 root root 41 Dec 29 17:36 file.txt
lrwxrwxrwx 1 root root 8 Dec 29 17:53 file2.txt -> file.txt

-s (symbolic Links): make symbolic links instead of copying

5. Preserve all the File attributes

If you want to save all the file permission while copying the source file, then you need to use -p option with cp command in Linux as shown below. This option will preserve read, write and execute permission for user, group and others and transfer the same permission to destination file without changing anything. This option will be much useful when you are copying a larger number of files and permission is an important factor.

[root@localhost example]# cp -p file.txt file2.txt
[root@localhost example]# ls -lrt
total 8
-rw-r--r-- 1 root root 41 Dec 29 17:36 file.txt
-rw-r--r-- 1 root root 41 Dec 29 17:36 file2.txt

-p: preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all.

6. Copy Directories Recursively

If you want to copy a directory and all its contents, then you need to use -r option to recursively copy all the files from source directory along with the source directory to the destination. Here I am copying example directory and all its contents to /opt path.

[root@localhost ~]# cp -rv example/ /opt/
'example/' -> '/opt/example'
'example/file.txt' -> '/opt/example/file.txt'

7. Copy files into target directory

If you want to copy files into target directory, then you need to use -t option as shown below. This option indicates the target directory where you want to copy your files. Here I am trying to copy file.txt in example target directory.

[root@localhost ~]# cp -t example/ file.txt
[root@localhost ~]# ls -lrt example/
total 4
-rw-r--r--. 1 root root 3893 Mar 25 23:22 file.txt

8. Visualize Progress while Copying the Contents

If you want to see the progress of cp command operation, then you need to use -v verbose option with cp command in Linux as shown below. This option will give us an idea about the progress of the operation currently happening through cp command.

[root@localhost ~]# cp -v file.txt example/
'file.txt' -> 'example/file.txt'

9. Update the File in Destination

If you only want to copy file when the content is added or different than the one already available in destination, then you need to use -u option with cp command in Linux. This will be particularly useful when you are taking backup of some files and copying it on daily basis. You can easily use this option to copy only those contents which are newly generated and are not available in destination path.

[root@localhost ~]# cp -u file.txt example/

10. Check Other Options with --help

If you want to check all the other options that can be used be with cp command in Linux, you can check it by using --help option as shown below.

[root@localhost ~]# cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
-a, --archive same as -dR --preserve=all
--attributes-only don't copy the file data, just the attributes
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
--copy-contents copy contents of special files when recursive

 

 

Reference: How to manage Docker Container

Leave a Comment