Categories
Linux

How to Copy a Directory and its Content in Linux

Use the ‘cp’ command to copy and paste entire directories from one place to another

No matter which Operating System you are running, copying one file or a folder from one location to another is the most basic task you perform daily. On a busy day at work, during a project assignment at school, or during the ongoing development of a project, copying files from location A to location B is inevitable.

Advertisements

Copying of files or folders is usually done using the GUI. Linux takes care of your habit to work on the terminal by providing a command-line utility that allows you to copy files or folders from one location to another. Using the cp command with a variety of options gives you the freedom to copy files and folders in multiple ways.

Let me walk you through this article where you will learn about the cp command and the ways to copy directories along with their content.

Options available with cp command

These are the most common options used with the cp command in the context of copying a directory and all its content.

OptionsDescription
-vverbose mode (shows progress)
-r/Rcopy directories recursively
-ndo not overwrite an existing file or folder
-iprompt before overwrite

Copy a directory from one location to another

Advertisements

Let’s start with the most basic use of the cp command. We will use this command with the option -r.

Using the -r option ensures that the sub-folders and files inside the directory you are copying, will also be copied.

Syntax:

cp -r [source_location] [target_location]

Example:

I have two directories inside my current working directory called ‘project’ and ‘workspace’.

Advertisements

In this example, I’ll use the cp -r command to copy the directory ‘project’ along with all its content to a new location i.e. ‘/home/gaurav/workspace’. This simply means that I’m copying the directory named ‘project’ to a directory named ‘workspace’.

These are the contents of the directory ‘project’. Using the ls command to display its contents.

gaurav@ubuntu:~/project$ ls -al total 288 drwxr-xr-x 6 gaurav gaurav 4096 Sep 17 18:26 . drwxr-xr-x 88 gaurav gaurav 266240 Sep 17 18:24 .. drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:25 dem1, drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:25 dem2 drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:25 dem3 drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:25 dem4 -rw-r--r-- 1 gaurav gaurav 24 Sep 17 18:26 temp.c gaurav@ubuntu:~/project$

Now, using the cp -r command we can copy the directory ‘project’ to any desired location.

gaurav@ubuntu:~$ cp -r /home/gaurav/project /home/gaurav/workspace gaurav@ubuntu:~$

Output:

gaurav@ubuntu:~/workspace$ ls -al total 408 drwxrwxr-x 4 gaurav gaurav 4096 Sep 17 18:27 . drwxr-xr-x 88 gaurav gaurav 266240 Sep 17 18:24 .. drwxrwxr-x 3 gaurav gaurav 4096 Mar 22 2018 .metadata drwxr-xr-x 6 gaurav gaurav 4096 Sep 17 18:27 project -rw-r--r-- 1 gaurav gaurav 1535 Sep 16 17:13 source.c gaurav@ubuntu:~/workspace$

In the output above, we can see that the directory ‘project’ is copied from the original location to this new location ‘/home/gaurav/workspace’. Now, let us open the directory ‘project’ and check if all the contents inside it are also copied.

gaurav@ubuntu:~$ cd ./workspace/project gaurav@ubuntu:~/workspace/project$
Advertisements

Note: I have used ./ here instead of entering the complete path. It simply means that it is my home directory path and workspace is located in my home or current working directory. For more clarification on this, you can check out this article.

Output:

gaurav@ubuntu:~/workspace/project$ ls -al total 28 drwxr-xr-x 6 gaurav gaurav 4096 Sep 17 18:27 . drwxrwxr-x 4 gaurav gaurav 4096 Sep 17 18:27 .. drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:27 dem1, drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:27 dem2 drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:27 dem3 drwxr-xr-x 2 gaurav gaurav 4096 Sep 17 18:27 dem4 -rw-r--r-- 1 gaurav gaurav 24 Sep 17 18:27 temp.c gaurav@ubuntu:~/workspace/project$

From this output, we can conclude that all the contents of the directory ‘project’ are also moved to the new location.


Copying multiple directories from one location to another

To copy multiple directories, cp command can be used in the same way as mentioned above. The only change here will be that you will need to enter the multiple source paths of the multiple directories to be copied.

Syntax:

cp -r [source_path_1] [source_path_n] [destination_path]
Advertisements

Let us check this command with an example.

Example:

gaurav@ubuntu:~/workspace$ cp -r ./snap ./project /home/gaurav/tomcat

Here, I have copied two directories ‘snap’ and ‘project’ from my current working directory to a new location ‘/home/gaurav/tomcat’.

Now let us check if the directories are copied with their content to the new location.

gaurav@ubuntu:~/tomcat$ ls -al total 9316 drwxrwxr-x 5 gaurav gaurav 4096 Sep 19 12:16 . drwxr-xr-x 88 gaurav gaurav 266240 Sep 19 12:15 .. drwxr-xr-x 6 gaurav gaurav 4096 Sep 19 12:16 project drwxr-xr-x 7 gaurav gaurav 4096 Sep 19 12:16 snap

Checking if the content of these directories are also copied.

gaurav@ubuntu:~/tomcat/snap$ ls couchdb eclipse htop pycharm-community vim-editor gaurav@ubuntu:~/tomcat/snap$
gaurav@ubuntu:~/tomcat/project$ ls dem1, dem2 dem3 dem4 temp.c gaurav@ubuntu:~/tomcat/project$

Using cp command with verbose mode

Advertisements

Using the cp command with the option -v enables the verbose mode. This feature displays the files on the terminal which are being copied. The name of the File or Folder which is being copied gets displayed on your terminal.

Syntax:

cp -vr [source_directory] [target_location_path]

Example:

gaurav@ubuntu:~$ cp -vr ./workspace/apache ./space

In this example, the directory ‘apache’ is copied to a new folder ‘space’. I have used the -v option with -r, so that all the contents of the apache directory also gets copied.

Output:

'./workspace/apache' -> './space/apache' './workspace/apache/apache-tomcat-8.0.52.tar.gz' -> './space/apache/apache-tomcat-8.0.52.tar.gz' gaurav@ubuntu:~$
Advertisements

The output displays the directory which is being copied. The same process can be done in case of copying multiple files.


Avoid overwriting when using cp command

Sometimes while using the cp command to copy multiple files or folders, you may end up overwriting files that are already copied to the new location. To avoid this it is advisable to use the -i option with the cp command. This will prompt you before overwriting any file or folder.

Syntax:

cp -ri [source_directory_path] [target_location_path]

Note: Here, in the syntax I have used -r option as well. This is will copy the content of the directories also. It will also, check if any of these sub-folders and files inside the directory are being overwritten.

Example:

gaurav@ubuntu:~$ cp -ri ./workspace/snap ./tomcat cp: overwrite './tomcat/snap/pycharm-community/current'?
Advertisements

Here, I tried to copy a directory named ‘snap’ to a new location. But the directory name ‘snap’ already exists at the new location. Hence, I will be prompted by the terminal before overwriting the existing snap directory.

You can type ‘Yes‘ or ‘No‘ as the response to this prompt.

In case, if you do not use the -i option, the existing directory will be overwritten by the new directory.


Skip overwriting of files and directories with cp command

We can use the -n option to directly instruct the terminal to never overwrite and simply skip similar files and directories while using the cp command.

Syntax:

cp -nr [source_directory_path] [target_location_path]
Advertisements

Using this command you can be sure that your files and folders will not be overwritten.

Example:

gaurav@ubuntu:~$ cp -ri ./workspace/snap ./tomcat gaurav@ubuntu:~$

Here, the directory ‘snap’ already exists at the target location. Hence, using -n option will ensure not to overwrite this directory.

Unlike -i option, here you will not be prompted about the overwriting.


Conclusion

Using the cp command to copy directories and their content in Linux is an easy process. To learn more about the cp command with other available options, you can check out the manual page by typing man cp in your Linux terminal.