Linux cp Directory and Content

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Linux provides  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.

Syntax

cp command has following general syntax which can be used for all different kind of copy operations.

  • 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

So we will provide the -r option for recursivity.

Copy Empty Directory

Copy Empty Directory

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 nmap into /home/ismail/nmap . The newly created directory named will be nmap too.

Copy Directory and All Contents While Preserving Attributes

Linux 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.

LEARN MORE  Linux Rsync Resume Partial File Transfers

Verbose Mode

If we want to list operations about copy we can use -v option. -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.

Verbose Mode

Verbose Mode

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 .

Copy To The Remote Server with Rsync Command

Copy To The Remote Server with Rsync Command

Copy To The Remote Server with Scp Command

scp command has similar functionality to the rsync command. 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 192.168.142.144 /home/ismail/backup directory.

Copy To The Remote Server with Scp Command

Copy To The Remote Server with Scp Command

We can see that during secure copy operation the files size, complete percentage, estimated remaining time information also provided.

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)