How to Copy Directories in Linux?

Posted in

How to Copy Directories in Linux?
maazbinasad

Maaz Bin Asad
Last updated on April 19, 2024

    If you are new to the Linux command line, you might find it quite intimidating at the beginning. However, you can realize its true power only after you get hands-on experience with it. Some common operations that you can perform using the Linux command line include manipulating file systems, traversing across directories, editing files, and installing packages. While many file managers are GUI-based and come with almost all the Linux distributions, it is always better to learn how you can use the Linux command-line interface (terminal) to access files and directories, manage file permissions, manage users, and accomplish several other sensitive tasks that require utmost attention. Hence, it’s important to learn the Linux command line if you plan to stick with Linux for a long time. In this guide, we will walk you through various commands that you can use to copy directories in the Linux distros. The most frequently used command to copy directories in Linux is the cp command. However, there's another command, called rsync , which also allows you to copy directories in Linux. We will discuss both commands with examples in the upcoming sections. Let’s discuss the cp command first.

    Copying Directories Using the “cp” Command

    The cp (copy) command is the most fundamental and frequently used command that allows us to copy the content, including directories and files, from one location to another in Linux.

    Syntax of cp Command

    The syntax of the cp command to copy directories is as follows:

    cp [options] {source-directory} {target-directory}

    To copy directories using the cp command, we have to use the -r or -R option with the command. The -r or -R options stands for recursive, which states that the command recursively copies all the files in a directory. The cp command with the -r or -R option will copy all the files and folders from the source directory, create a new folder if it does not already exist in the target location, and copy all the selected files. Let’s see a few examples to understand how we can use the cp command to copy directories.

    $ cp -r /home/jarvis/sample/ /home/jarvis/Documents/destination

    Output: Copy Directories 1

    You can see that originally, the destination folder did not exist inside the Documents folder. However, once we executed the cp command, the destination directory was created. Also, all the files and folders inside the sample directory were copied to the destination directory. Following are some points that you need to take care of while using the cp command to copy Linux directories:

    1. If we use the -a option along with the -r option, all the attributes, such as permissions, timestamps, etc., of the contents inside the source directory will be preserved.
    2. You won’t be able to create destination directories that are nested to each other. For example, let’s run the following command where dir1 and dir2 do not exist:
    $ cp -r /home/jarvis/sample/ /home/jarvis/Documents/dir4/dir3/dir2/dir1/

    Output: CD 2

    As you can see, the command failed to execute.

    1. We have already discussed that if the destination directory does not exist, the cp command will create it automatically. Let us consider the following command:
    $ cp -r source destination

    After executing the above command, if the destination directory does not exist, it will be created automatically. Also, the command will copy all the contents inside the source directory into the destination directory. However, it will not create the source subdirectory in the target directory. It simply means that there won’t be a folder called source inside the destination folder. The destination folder will be a replica of the source folder.

    Copying Directories using the “rsync” command

    Besides the cp command, there’s another command called rsync that you can use to copy directories in Linux. The essence of the rsync command is to perform remote sync. It means that this command helps you synchronize files, folders, or other data between different locations or even different machines. The rsync command uses the SSH protocol to copy data. It has several benefits over the cp command. One such benefit is that it uses compression and decompression when it performs the transmission of files. Thus, it saves bandwidth by transferring only the latest and newly added data. To use the rsync command, you need to install the rsync package on your Linux machine. On the off chance that it does not exist, you can easily download it. Refer to the command below to install rsync on an Ubuntu machine:

    $ sudo apt-get install rsync

    Output: CD 3

    Syntax of rsync Command

    The syntax of the rsync command to copy directories is as follows:

    $ rsync -a {source} {target}

    You need to use the -a (all) option to replicate or transfer copies of directories using this command. The -a option tells the command to copy all the content, including files, sub-directories, meta-data, etc., and preserve the permissions, timestamps, ownerships, symbolic links, etc. Let’s try to copy a directory using the rsync command.

    rsync -a /home/jarvis/sample/ /home/jarvis/Documents/destination

    Output: CD 4

    Make sure to take utmost care of the slashes when working with such commands. For instance, if you remove the last forward slash from the source directory, it will create a copy of the source directory inside the destination directory (i.e., as a subdirectory) as you can see in the screenshot below: CD 5

    You can see that the rsync command created a new folder "sample" as a subdirectory, and the "sample" folder is a replica of the source directory inside the destination folder. The best way to avoid this is to always use forward slashes at the end of both the source and destination directories.

    Wrapping it Up

    In this guide, we discussed how to copy directories in Linux using the command-line interface (terminal). We shared details of two different commands to copy directories - cp and rsync. Also, we discussed some practical examples to help you understand the syntax of both commands. Along with the commands for copying directories in Linux, this guide also highlighted the options that you need to use with cp and rsync for copying directories successfully. We hope that now it has become easier for you to copy directories seamlessly in your favorite Linux distribution using the command-line interface. People are also reading:

    Leave a Comment on this Post

    0 Comments