You are currently viewing How to Copy Directories in Linux

How to Copy Directories in Linux

Linux is one of the most powerful and democratized kernel for Operating Systems, which offers a variety of methods and ways to perform tasks efficiently. One of those common operations that one needs to execute very often is to copy directories. Whether you are backing up data or duplicating your project or transferring files from one location to another, knowing how to copy a directory in Linux using a terminal is an essential thing. In this blog, let me share some of the ways that I know to achieve the task of copying directories. 

Method 1: Using the cp Command

The easiest, straightforward and common way to copy directories in Linux is through the `cp` command. To copy a directory and its contents, you can use the following syntax:

Bash
cp -r source_directory destination_directory

Explanation:

  • r – stands for recursive, allowing the cp command to copy the entire directory and its subdirectories.
  • Source_directory – is the directory you want to copy.
  • Destination_directory – is the location where you want to paste the copied directory.

Example:

Bash
cp -r /home/user/documents/project /backup

Method 2: Using rsync for Efficient Copying

The rsync command is a powerful tool for copying files and folders with advanced features like incremental transfer and bandwidth optimization. To copy a directory using rsync, the basic syntax is:

Bash
rsync -av source_directory/ destination_directory

Explanation:

  • a – preserves the file permissions and other attributes during the copy.
  • v – enables verbose mode, providing detailed information about the copying process.
  • Source_directory – the directory to be copied.
  • Destination_directory –  the target location.

Example:

Bash
rsync -av /home/user/documents/project/ /backup

This command efficiently copies the contents of the “project” directory to the “/backup” directory. By efficiently, we mean that only the contents that changed will be synced. This is mostly useful when doing a sync in your backup folder. 

Method 3: Using Tar and Pipe for Compression

If you want to copy a directory and compress it in the process, you can use the tar command along with pipes. Here’s a basic example:

Bash
tar czvf destination_directory/backup.tar.gz -C source_directory/ .

Explanation:

  • c – stands for create a new archive.
  • z – enables gzip compression.
  • v – enables verbose mode to display the files being archived.
  • f – specifies the filename of the archive.
  • -C – changes to the specified directory before performing any operations.
  • source_directory – the directory you want to copy.
  • destination_directory/backup.tar.gz – the target location and filename of the compressed archive.

Example:

Bash
tar czvf /backup/backup.tar.gz -C /home/user/documents/project/ .

This command creates a compressed archive of the “project” directory and saves it to the “/backup” directory.

Method 4: Using cp with Find Command for Selective Copying

In some cases, you might want to copy only specific files based on certain criteria. The combination of the cp command and the find command can be useful for such scenarios:

Bash
find source_directory/ -type f -exec cp {} destination_directory/ \;

Explanation:

  • find – searches for files in the specified directory.
  • source_directory/ – is the directory to search for files.
  • -type f – specifies that only files should be considered, not directories.
  • -exec cp {} destination_directory/ \; – executes the cp command for each found file, copying it to the destination.

Example:

Bash
find /home/user/documents/project/ -type f -exec cp {} /backup/ \;

This command copies all files from the “project” directory to the “/backup” directory, excluding subdirectories.

Conclusion:

Copying folders in Linux can be done through many methods, each of them offering their unique advantages based on the user’s requirements and needs. Whether you prefer the simple`cp` command or the advanced features of rsync and tar, understanding these methods enables you to efficiently manage your data. Please Choose the method that best suits your needs.

Leave a Reply