cp command in order to copy files, folders, and directories. But sometimes we may have some issues if we want to copy a folder or directory. In this tutorial, we will learn how to copy folders and directories with the cp command and provides solutions for problems.
cp command has following general syntax which can be used for all different kind of copy operations.
cp OPTIONS SOURCE DESTINATION
- OPTIONS are used to set different behavior to the `cp` command like recursive
- `SOURCE` is used to specify the source directory. The source can be a relative or absolute path
- `DESTINATION` is used to specify the destination directory. The source can be a relative or absolute path
Copy Empty Directory
We will start with a simple example of
cp command. We will simply copy an empty directory by providing
-r providing an option to the
cp command. If there is some content in the directory the
cp command will not work without any option.
We can see that there is an error like
cp: -r not specified; omitting directory 'backup/'
So we will provide the
-r option for recursivity.
$ cp backup backup2
Copy Directory and All Contents
We can copy all subfolders, directories and contents with by using
-r option like below. In this example, we will copy the folder named
/home/ismail/nmap . The newly created directory named will be
$ cp -r nmap /home/ismail/nmap
Copy Directory and All Contents While Preserving Attributes
cp command also provides
-a option which can be used to preserve attributes of the copied files and folders. Attributes hold special information like readonly, selinux etc .
-a option also provides the recursivity, so we do not need to provide
-r option to copy sub files and folders.
$ cp -a nmap nmap-backup
If we want to list operations about copy we can use
-v option is named as
verbose mode. We can also call it debug mode. In verbose mode command will print all copy operations to the standard output which is generally our terminal.
$ cp -r -v nmap nmap-backup
Copy To The Remote Server with Rsync Command
rsync is an alternative method to copy files and folders in to remote servers. As
cp is used to copy locally
rsync provides remote or network wide copy operations. We can use
-avz options in order to copy from local to remote or from remote to local like below. In this example, we will copy local files to the remote server with IP address 192.168.142.144 .
$ rsync -avz nmap 192.168.142.144:/home/ismail/backup/
Copy To The Remote Server with Scp Command
scp command has similar functionality to the
scp copies files and folders over the network to the remote or local systems.
scp mainly uses SSH protocol to copy over network. We can use very similar options of the
cp command. In order to copy local to remote or remote to local in a recursive manner, we can use
-r option with the
scp command too. In this example, we will copy files from
nmap directory to the remote IP address
$ scp -r nmap 192.168.142.144:/home/ismail/backup/
We can see that during secure copy operation the files size, complete percentage, estimated remaining time information also provided.