How to Copy Directory: Linux Version

How to Copy Directory: Linux Version

4 mins read359 Views Comment
Updated on Aug 21, 2024 16:58 IST

If you are new to the Linux operating system, the command-line interface can be a little daunting at first. However, once you start to get the hang of it, you will realize how powerful and flexible it is.

2023_04_Understanding-Break-Statement-in-C-56.jpg

In this guide, we will cover the basics to copy directory in Linux using some simple command-line tools. Whether you need to make a backup of an important directory or want to move files to a new location, knowing how to copy directories in Linux is an essential skill for any Linux user. So, without further ado, let’s get started!

Table of Contents

Methods to Copy Directory: Linux

Method 1: Using the cp Command

The “cp” command stands for “copy,” and it is the most basic command for copying files and directories. Here is the syntax:

2023_04_image-103.jpg

Where:

  • -R stands for “recursive,” which means that the cp command will copy all files and subdirectories in the directory you’re copying.
  • source_directory is the name of the directory you want to copy.
  • destination_directory is the name of the directory you want to copy it to. As an example, let’s say that you want to copy the “./Music” directory into a directory named “./Desktop”. Here’s how you will do it:
    • Open your terminal window.
    • Type the following command:

 
Copy code
  • Press the Enter key
2023_04_copy-directory-in-Linux-1.jpg

You have successfully copied one directory into another directory on Linux.

Explore free Linux courses

Method 2: Using the rsync Command

The “rsync” command is similar to “cp,” but it has some additional features that make it useful for more advanced copying tasks. One of the most significant benefits of “rsync” is that it can copy only the files that have changed since the last time you ran the command, which can save you a lot of time if you’re copying large directories. Here’s the syntax:

2023_04_image-105.jpg

Where:
-avz stands for “archive, verbose, and compress,” respectively. These options ensure that all of the files and subdirectories in the directory you’re copying are preserved, and that you can see the progress of the copying process in the terminal.
source_directory is the name of the directory you want to copy.
destination_directory is the name of the directory you want to copy it to.
As an example, let’s say that you want to copy the “./Videos” directory into a directory named “./Desktop”. Here’s how you will do it:

  • Open your terminal window.
  • Type the following command:

 
Copy code
  • Press the Enter key
2023_04_copy-directory-in-Linux-2.jpg

Awesome, you have again successfully copied one directory into another directory on Linux!
Now that you know how to use “cp” and “rsync” to copy directories in Linux, let’s talk about some additional tips and tricks you can use to make your copying tasks even more efficient.

Recommended online courses

Best-suited Linux courses for you

Learn Linux with these high-rated online courses

– / –
200 hours
2 K
20 hours
– / –
3 months
– / –
6 months
Free
8 hours
– / –
40 hours
– / –
3 months
10 K
60 hours
– / –
3 months

Copy Directory in Linux with Many Files

If you’re copying a directory with a large number of files, you may want to consider using the tar command to compress the directory before copying it. Here’s the syntax:

2023_04_copy-directory-in-Linux-last.jpg

Where:

-czvf stands for “create, compress, verbose, and file,” respectively. This command will create a compressed archive of the directory you want to copy, which you can then copy to your desired location using “cp” or “rsync.”

Copying a Directory from One Remote Server to Another

If you want to copy a directory from one remote server to another, you can use the scp command. “scp” stands for “secure copy,” and it allows you to securely copy files and directories between two remote servers. Here’s the syntax:


 
Copy code

Where:

  • username is your username on the remote server.
  • source is the IP address or hostname of the remote server you’re copying from.
  • /path/to/source_directory is the path to the directory you want to copy.
  • destination is the IP address or hostname of the remote server you’re copying to.
  • /path/to/new_directory is the path to the location where you want to copy the directory on the remote server.

Copying a Directory but Excluding Certain Files

If you want to copy a directory but exclude certain files or directories within it, you can use the rsync command with the –exclude option.
For example, let’s say you have a directory called “my_folder” that contains a subdirectory called “my_secret_files.” To copy “my_folder” but exclude “my_secret_files,” you can type the following command:


 
Copy code

Where:
–excludeusername tells “rsync” to exclude any files or directories that match the pattern provided. In this case, we’re excluding any file or directory with the name “my_secret_files.”

Understanding ps Command in Linux
Understanding ps Command in Linux
When working with Linux systems, it’s important to have a good understanding of the processes running on your system. This is where the ps command comes in handy. From viewing...read more
How to Install Linux using Virtualbox on Windows?
How to Install Linux using Virtualbox on Windows?
Are you interested in running Linux on your Windows computer, but don’t want to go through the hassle of creating a dual-boot setup, or completely overwriting your system, or buying...read more
How to Transfer Files Using SCP Command in Linux
How to Transfer Files Using SCP Command in Linux
Learn how to transfer files between Linux machines using the SCP command. Our step-by-step guide will teach you how to securely copy files using SSH, set permissions, and troubleshoot common...read more

Endnotes

In conclusion, copying directories in Linux is a simple task that can be accomplished with either the “cp” or “rsync” command. “cp” is the more basic option, while “rsync” provides additional features such as the ability to only copy changed files and the ability to copy files over a network. By using these tools and some additional tips and tricks, you can efficiently copy directories in Linux for whatever purpose you need.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio