directory copy in linux

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


If you are a system administrator or a programmer copying directories in Linux is a very frequent and important task.

There could be various reasons to copy, it may be just to maintain the folder structure in a logical way or for collecting backup, etc.

In this tutorial, we are showing how to use the cp command in Linux to copy directories and folders.

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 along with source and destination folders. The option -r stands for recursive.

#cp -r <source_directory> <destination_directory> 

For example, if you need to copy 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. 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 those 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 that 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/directories copying on a local Linux machine from one location to another and the same when two machines are connected to each other via an IP network.