How To Copy Directory on Linux [4 Best Methods]


To copy a directory on Linux, try any of the following methods:

  1. Use cp -r to recursively copy a directory and all its contents, and cp -rp also to preserve file permissions and attributes.
  2. rsync -av efficiently syncs files and directories across locations, preserving attributes and offering verbose output.
  3. By archiving a directory with tar -cvf and then extracting it with tar -xvf, you can copy directories while maintaining file permissions.
  4. File managers allow you to copy directories with simple drag-and-drop or copy-paste actions visually.

Feeling stuck with copying folders in Linux? Many users hit this issue, but the good news isย it’s simpler to solve than you might think. In this guide, I’ll explore not one but four easy methods to duplicate directories effortlessly.

I’ll navigate through straightforward command-line options like cp and rsync, and I’ll also introduce you to user-friendly graphical interfaces for those who prefer a visual approach. Along the way, I’ll share expert tips and tackle common challenges to ensure your copying tasks are smooth. By the end of this, you’ll not only master these methods but also be geared up to explore even more of what Linux has to offer.

How To Copy Directory on Linux?

To copy a directory on Linux, use the cp command with the -r option, which stands for recursive copying. This ensures that all subdirectories and files within the directory are also copied. For example, to copy a directory from /source_directory to /destination_directory, you would use the command cp -r /source_directory /destination_directory. This command works in the terminal and requires that you have read permissions for the source directory and write permissions for the destination directory.

That was the quick answer; for a more detailed one, here are 4 ways to copy a directory on Linux.

4 Ways To Copy Directory on Linux

1. cp Command

The cp command in Linux is used for copying files and directories. Using it with the right options allows you to duplicate a directory’s contents, including all subdirectories and files, preserving their structure and attributes. Follow these steps:

  1. Access your Linux terminal by using a shortcut, Ctrl+Alt+T.
open terminal
  1. To copy a directory, including all its contents, enter the following command:
<strong>cp -r source_directory_path destination_directory_path</strong>

This command recursively copies the directory and all its contents to a new location. -r tells cp to copy directories recursively.

copying directory to another directory
  1. If you also want to preserve the attributes (such as permissions, timestamps, and ownership), add the -p flag:
<strong>cp -rp source_directory_path destination_directory_path</strong>

This command ensures that the copied directory retains its original attributes.

copying directory with its attributes
  1. Ensure the directory has been copied as expected by listing the contents of the destination directory:
<strong>ls destination_directory_path</strong>

The output will be:

verifying in the destination directory

2. rsync Command

rsync is a utility for efficiently transferring and synchronizing files across computer systems, using a delta transfer algorithm. It’s particularly useful for copying large directories or for incremental backups. Here is the step-by-step guide:

  1. Access your Terminal window and copy a directory to another location, by running the command:
<strong>rsync -av source_directory_path/ destination_directory_path/</strong>

-a enables archive mode to preserve permissions, timestamps, and other attributes.

-v increases verbosity to show the files being copied.

transfering contents of a directory to another
  1. Check the destination directory to ensure the copy operation was successful:
<strong>ls destination_directory_path</strong>

The output will look like this:

verifying the transfer of contents

3. tar Command

The tar command is traditionally used for creating archives. Interestingly, it can also be used to copy directories by archiving a directory then immediately extracting it to a new location, making it a handy tool for preserving file permissions and attributes. Here is how to do it:

  1. Open the Terminal.
  2. Execute the following command:
<strong>tar -cvf - source_directory_path | tar -xvf - -C destination_directory_path</strong>

The first tar -cvf – source_directory_path creates an archive and outputs it to stdout.

The second part, tar -xvf – -C destination_directory_path, reads from stdin to extract the archive to the destination directory.

copying directory content by creating an archive
  1. Use the ls command to check the contents of the destination directory.
verifying the transfer of contents using ls command

4. Using GUI Tools to Copy Directories

Graphical File Managers in Linux, provide a user-friendly way to manage files and directories, including copying them through a graphical interface. Follow these steps:

  1. Locate the directory you wish to copy.
  2. Right-click the directory and select Copy or use the keyboard shortcut, usually Ctrl+C.
copying directory using GUI
  1. Open the location where you want to copy the directory.
  2. Right-click in the destination location and select Paste or use the keyboard shortcut, usually Ctrl+V.
transfering directory to another directory using GUI
  1. The directory should now appear in the destination location.

5 Advanced Tips for Copying Directories on Linux

When working with Linux, copying folders can sometimes feel like a puzzle. But with a few clever tricks, it can become much simpler and quicker. Here are five smart ways to copy directories, making this task a breeze.

  • ๐Ÿš€ Maximize rsync for Network Transfers: When copying directories over a network, use rsync with the –compress option to reduce bandwidth usage and the –partial option to resume interrupted transfers. This approach is efficient for both bandwidth and time, especially for large data sets.
  • ๐Ÿ” Selective Copying with find and cpio: To copy specific files within directories, use find to locate these files and pipe the results to cpio. This method allows for highly selective copying, useful for archiving or backing up specific file types or modified files.
  • ๐Ÿ”„ Secure Remote Copying with scp and Port Forwarding: Enhance security when copying directories to remote servers by combining scp with SSH port forwarding. This setup is particularly beneficial for transferring data securely between servers located behind firewalls or in different network segments.
  • ๐Ÿ›  Automate with Bash Scripts: For frequent directory copying tasks, create Bash scripts incorporating loops, conditional statements, and prompts for user input. Scripting automates the process, reduces the potential for errors, and saves time, making it ideal for routine backups or data migration tasks.
  • ๐Ÿ” Use tar for Permission Preservation: When moving directories between systems, employ tar to ensure that file permissions, ownerships, and symbolic links are preserved accurately. This method is crucial for maintaining system security and functionality, especially when setting up environments or migrating servers.

Common Issues and Solutions in Copying Directories on Linux

Facing issues when copying directories on Linux can be frustrating, but with the right knowledge, these problems are easily solvable. Here are the five most common problems that you might encounter when copying directories and their possible solutions:

  • ๐Ÿ”’ Permissions Denied: If you’re unable to copy a directory due to permission errors, use sudo before your command or change the directory’s permissions with chmod to ensure you have the right access levels.
  • ๐Ÿ“ฆ Running Out of Space: Encountering a “No space left on device” error means your target disk is full. Check disk usage with df -h and clean up or move your copy to a disk with more space.
  • ๐Ÿ”— Ignoring Symbolic Links: If symbolic links within the directory aren’t being copied, ensure you’re using cp -a or rsync -a to preserve links, devices, and other special files along with the standard files.
  • ๐Ÿ“‚ Directory Not Found: This error usually means the path is incorrect. Double-check your source and destination paths for typos. Using tab completion in the terminal can help avoid such mistakes.
  • ๐Ÿ’พ Preserving File Attributes: To maintain the original file permissions, ownership, and timestamps, use the -p option with cp (cp -rp) or the -a option with rsync (rsync -av).

In a Nutshell

Navigating through the methods to copy directories in Linux, I’ve covered everything from basic cp and rsync commands to the use of tar and GUI tools, aiming to make the process as straightforward as possible. Alongside, I also addressed common issues you might face, providing solutions to overcome them. These steps, enriched with advanced tips, are designed to enhance your efficiency and tackle tasks with confidence.

Expanding your Linux skill set doesn’t stop here. Exploring system security, task automation with cron jobs, and network management can further elevate your proficiency. Each of these areas offers valuable knowledge, building upon what you have explored and preparing you for a wide array of challenges, ensuring you’re well-equipped for the dynamic world of Linux.

Frequently Asked Questions

  1. Can I copy directories between different Linux distributions using these methods?

    Absolutely. The methods described, such as using cp, rsync, and tar, rely on universal commands available in virtually all Linux distributions, making it feasible to copy directories across any Linux environment without compatibility issues.

  2. How can I copy a directory excluding certain files or subdirectories?

    To exclude specific files or subdirectories when copying a directory, utilize the rsync command coupled with the –exclude option. This allows for selective copying, providing flexibility and control over exactly what gets duplicated in the process.

  3. Is there a way to speed up the copying of very large directories?

    Indeed, to enhance the speed of copying large directories, especially over networks, consider employing rsync with the –compress option. For local copies, tweaking cp with ionice can optimize system resource usage, significantly speeding up the copying process.

  4. How do I handle copying directories with read-only files?

    When dealing with read-only files within directories you wish to copy, altering file permissions temporarily using chmod or executing the copy command with sudo can dodge permission restrictions, allowing the copy operation to proceed smoothly.



Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.



Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, heโ€™s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Install ADB on Linux [2 Best Methods]

Next Post

9 Best Methods to Bash Split String

Leave a Reply

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

Read next