How to Copy a Directory on Linux

In this tutorial, you will learn how to copy a directory on a Linux-based system.

Linux copy directory using terminal

There are many reasons to copy a directory on your Linux operating system, whether to create a backup of your files or to copy them to a mounted shared directory.

The easiest way to copy a directory on your Linux system is to utilize the cp command. While we have a more in-depth article that explores this tool, let us purely explore how you can use this tool to copy directories.

Best of all, the steps we are showing you should work with any Linux-based operating system, such as Ubuntu and Debian.

If you want to copy a directory over a network connection, you should use a tool such as the scp command or a more feature-packed tool such as rsync.

Simple Steps to Copying a Directory on Linux from the Terminal

Over the following sections, we will walk you through copying a directory on your Linux system.

Even if you are relatively new to using the terminal, you should find this whole process relatively easy to follow.

Basic Syntax of Copying a Directory on Linux

Before we show you how to copy a directory from within the Linux terminal, let us explore the basic syntax of how you will do this.

Below, you can see that we use the “cp” command, followed by the “-r” option, which tells the tool to copy recursively. You then must specify the directory that you want to copy and, lastly, where you want that directory to be copied.

cp -r <SOURCE> <DESTINATION>/

If you need help copying a directory, continue to the next section, where we will explore a basic example.

Example of how to use the cp Command to Copy a Directory

1. On Linux, to copy a directory from the terminal, you will obviously need to have the terminal open.

If you use an operating system with a desktop such as Ubuntu, you can typically open the terminal by pressing CTRL + ALT + T on your keyboard.

2. Before we get started, let’s check the contents of the directory that we intend to copy on our Linux system. This will allow us to verify that all of our data was copied correctly.

For this, we use the ls command to list the files and directories in the “/home/pimyubu/example” directory.

ls -l /home/pimyubu/example

Below, we have three files and one directory located within this folder.

total 16
drwxr-xr-x 3 pimyubu pimyubu 4096 May 28 15:31 data
-rw-r--r-- 1 pimyubu pimyubu   52 May 21 21:11 example1.txt
-rw-r--r-- 1 pimyubu pimyubu   52 May 28 15:30 example2.txt
-rw-r--r-- 1 pimyubu pimyubu   52 May 28 15:30 example3.txt

3. Here, we will use the cp command on Linux to copy our directory from “/home/pimyubu/example” to “/home/pimyubu/copyexample“.

Since we are using the “-r” option, the directory and its contents will be copied to the destination. If you are copying to a place your current user doesn’t own, you must add “sudo” to the start of this command.

cp -r /home/pimyubu/example /home/pimyubu/copyexample

4. We can verify that all our files and directories have been copied by using the ls command again. This time, however, we will use this command in the directory to which we copied everything.

ls /home/pi/copyexample

Below, you can see that all of the data was copied. You can tell these files were copied simultaneously by the modified date.

total 16
drwxr-xr-x 3 pi pi 4096 May 28 15:52 data
-rw-r--r-- 1 pi pi   52 May 28 15:52 example1.txt
-rw-r--r-- 1 pi pi   52 May 28 15:52 example2.txt
-rw-r--r-- 1 pi pi   52 May 28 15:52 example3.txt

Conclusion

Hopefully at this point in the guide, you will have a good understanding of how you can easily copy a directory on Linux.

Copying a directory is relatively straightforward because of the cp command. However, you can also use other tools, such as rsync. Rsync will work better if you want to copy only changed files to your destination.

Please feel free to comment below if you have any questions about how to copy directories on your system.

We highly recommend checking out our many other Linux tutorials if you want to learn more about Linux.

Leave a Reply

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