How to copy a directory in Linux? CP Linux command tutorial.


Whether you are a system administrator or a programmer who uses the Linux operating system, copying directories is an essential and frequent task.

The purpose of copying can be for a variety of reasons. For example, it may be necessary to maintain folder structure logically or to collect backups, etc.

This tutorial will demonstrate how to copy directories or folders in Linux using the cp command.

How to use the cp command to copy directories on Linux?

A Linux user uses the cp command to copy the files on the local machine. You can use the same command with the -r option to copy the directories from source to destination folders. The option -r stands for recursive.

#cp -r <source_directory> <destination_directory> 

For example, if you need to copy the source directory csps into a new directory csps_backup(already created).

#cp -r csps csps_backup

One execution above copies csps into csps_backup.

#cp -r csps csps_backup
# cd csps_backup/
# ls
csps

The above copies the source directory inside the destination folder. But if the destination folder does not exist. The command will create a new directory and copy all files in it.

 #cp -r csps csps_backup
# cd csps_backup/
 # ls -lrt
 total 4
 -rw-r--r-- 1 root root 5 Jun 29 01:04 file2.c
 -rw-r--r-- 1 root root 0 Jun 29 01:04 file1.c
 #

Copy files under a new directory.

# cp csps/* csps_backup/
 # cd csps_backup/
 # ls
 file1.c  file2.c

Copying multiple directories under the target folder

Copying multiple directories one by one is time-consuming. With the cp command, we can specify the list of directories that need to copy. Each item in the list is separated by space.

# cp -r <dir_1> <dir_2> ...<  dir_n>  <target_directory>

The above command will copy each source directory under the target_directory folder.

Copy files from each source directory.

# cp -r <dir_1/*> <dir_2/*> ...<  dir_n/*>  <target_directory>

Please be careful while using the above command. If the source directories have common files, those will be overwritten in the target backup.

How to Copy Directories to a remote Linux machine?

While working on Linux, you may need to copy directories from one machine to another. This may be because you need to transfer logs, binaries, etc. If both machines connect to each other over an IP network, you can use the SCP command to copy a local directory to the remote machine.

SCP stands for Secure Copy Protocol which transfers files/directories securely from one machine to another over an IP network. The sender runs an SCP client, and the destination runs the SCP server. Following is an example where a directory is copied to a remote network machine.

 #scp -r csps root@192.168.1.191:/home/
 root@192.168.1.191's password:
 file1.c                                                                                 100%    0     0.0KB/s   00:00
 file2.c                                                                                 100%    0     0.0KB/s   00:00
#

Conclusion: –

We have learned basic file/directory copying on a local Linux machine from one location to another and the same when two machines are connected via an IP network.