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,
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@linuxtechi:~# cp /etc/passwd /mnt/backup/ root@linuxtechi:~#
Use below command to verify whether it has been copied or not.
root@linuxtechi:~# ls -l /mnt/backup/ total 4 -rw-r--r-- 1 root root 2410 Feb 3 17:10 passwd root@linuxtechi:~#
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@linuxtechi:~# cp /etc/passwd /etc/group /etc/shadow /mnt/backup/ root@linuxtechi:~#
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@linuxtechi:~# cp -i /etc/passwd /mnt/backup/ cp: overwrite '/mnt/backup/passwd'? y root@linuxtechi:~#
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@linuxtechi:~# cp -v /etc/fstab /mnt/backup/ '/etc/fstab' -> '/mnt/backup/fstab' root@linuxtechi:~#
In case you want to use both interactive mode and verbose mode then use the options “-iv”
root@linuxtechi:~# cp -iv /etc/fstab /mnt/backup/ cp: overwrite '/mnt/backup/fstab'? y '/etc/fstab' -> '/mnt/backup/fstab' root@linuxtechi:~#
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 linuxtechi user to “/mn/backup”,
root@linuxtechi:~# cp -r /home/linuxtechi /mnt/backup/ root@linuxtechi:~#
In above command, -r option will copy the files and directory recursively.
Now verify the contents of linuxtechi directory on target place,
root@linuxtechi:~# ls -l /mnt/backup/linuxtechi/ 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@linuxtechi:~#
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@linuxtechi:~# cp -a /home/linuxtechi /mnt/backup/ root@linuxtechi:~# ls -l /mnt/backup/linuxtechi/ 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@linuxtechi:~#
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 linuxtechi home directory to /mnt/backup folder, in the linuxtechi home folder we have 5 txt files, let’s edit couple of them and then copy all the txt files using “cp -u”.
root@linuxtechi:~# cd /home/linuxtechi/ root@linuxtechi:/home/linuxtechi# echo "LinuxRocks" >> file_1.txt root@linuxtechi:/home/linuxtechi# echo "LinuxRocks" >> file_4.txt root@linuxtechi:/home/linuxtechi# cp -v -u file_*.txt /mnt/backup/linuxtechi/ 'file_1.txt' -> '/mnt/backup/linuxtechi/file_1.txt' 'file_4.txt' -> '/mnt/backup/linuxtechi/file_4.txt' root@linuxtechi:/home/linuxtechi#
Read Also : 14 SCP Command Examples to Securely Copy Files in Linux
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@linuxtechi:~# 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@linuxtechi:~# cp -n /etc/passwd /mnt/backup/ root@linuxtechi:~#
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@linuxtechi:~# cp -s /home/linuxtechi/file_1.txt /mnt/backup/ root@linuxtechi:~# cd /mnt/backup/ root@linuxtechi:/mnt/backup# ls -l file_1.txt lrwxrwxrwx 1 root root 27 Feb 5 18:37 file_1.txt -> /home/linuxtechi/file_1.txt root@linuxtechi:/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@linuxtechi:~# cp -l /home/linuxtechi/devops.txt /mnt/backup/ root@linuxtechi:~#
As we know in hard link, source and linked file will have the same inode numbers, let’s verify this using following commands,
root@linuxtechi:~# ls -li /mnt/backup/devops.txt 918196 -rw-r--r-- 2 root root 37 Feb 5 20:02 /mnt/backup/devops.txt root@linuxtechi:~# ls -li /home/linuxtechi/devops.txt 918196 -rw-r--r-- 2 root root 37 Feb 5 20:02 /home/linuxtechi/devops.txt root@linuxtechi:
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”
# cp --attributes-only /home/linuxtechi/distributions.txt /mnt/backup/ # ls -l /home/linuxtechi/distributions.txt -rw-r--r-- 1 root root 41 Feb 5 19:31 /home/linuxtechi/distributions.txt # ls -l /mnt/backup/distributions.txt -rw-r--r-- 1 root root 0 Feb 5 19:34 /mnt/backup/distributions.txt #
In the above command, we have copied the distribution.txt file from linuxtechi 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@linuxtechi:~# cp --backup=simple -v /home/linuxtechi/distributions.txt /mnt/backup/distributions.txt '/home/linuxtechi/distributions.txt' -> '/mnt/backup/distributions.txt' (backup: '/mnt/backup/distributions.txt~') root@linuxtechi:~#
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@linuxtechi:~# cd /home/linuxtechi/ root@linuxtechi:/home/linuxtechi# cp -p devops.txt /mnt/backup/ root@linuxtechi:/home/linuxtechi# ls -l devops.txt -rw-r--r-- 1 root root 37 Feb 5 20:02 devops.txt root@linuxtechi:/home/linuxtechi# ls -l /mnt/backup/devops.txt -rw-r--r-- 1 root root 37 Feb 5 20:02 /mnt/backup/devops.txt root@linuxtechi:/home/linuxtechi#
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@linuxtechi:~# cd /home/linuxtechi/ root@linuxtechi:/home/linuxtechi# ls -l /opt/nix-release.txt lrwxrwxrwx 1 root root 14 Feb 9 12:28 /opt/nix-release.txt -> os-release.txt root@linuxtechi:/home/linuxtechi# root@linuxtechi:/home/linuxtechi# cp -P os-release.txt /mnt/backup/ root@linuxtechi:/home/linuxtechi# ls -l /mnt/backup/os-release.txt -rw-r--r-- 1 root root 35 Feb 9 12:29 /mnt/backup/os-release.txt root@linuxtechi:/home/linuxtechi#
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@linuxtechi:/home/linuxtechi# cp -f distributions.txt /mnt/backup/ root@linuxtechi:/home/linuxtechi#
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@linuxtechi:/home/linuxtechi# du -sh ubuntu-cloud.img 12M ubuntu-cloud.img root@linuxtechi:/home/linuxtechi# cp --sparse=always ubuntu-cloud.img /mnt/backup/ root@linuxtechi:/home/linuxtechi# du -sh /mnt/backup/ubuntu-cloud.img 0 /mnt/backup/ubuntu-cloud.img root@linuxtechi:/home/linuxtechi#
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
Read Also : 17 useful rsync (remote sync) Command Examples in Linux
Thanks. This is very helpful
Good article,helpful.
how to copy only directory data not files. what should I follow
Hi Manbahadur,
If I understand your query correctly, You want to create same directory structure skipping the files. You can achieve this by running below rsync command
$ rsync -av -f”+ */” -f”- *” /home/linuxtechi/Documents /home/pkumar/Documents
In the above command, first one is the source path of directory and second one is the target path where we want to sync directory structure.
I’m looking for a way to take a directory listing and > to a cp or rsync maybe using awk and & so that if I have 100 files in a directory it will spawn 100 threads. I’ve done this with a bash script before… ls > quickcopy then vi and add cp or rsync commands and an & to background, chmod +x and let it fly but it would seem that there would be a way to just do this from command line. It would also be nice to wrap it in a nice -5 and a time command. I mean it seems like there would be a command like this already built for times when you are trying to make 20 copies of flash drives for your whole extended family of 500 old pictures. Or a server. Yeah. Or for files on a server. I did it when I was working in seismic… Same thing.