linux rsync

How to Copy Directory in Linux

In Linux, you may copy directories by using different ways. In this tutorial, I will explain how to copy directories in Linux.

The cp command: 

As the two-letter command suggests, cp stands for the copy.

The cp command will help you to achieve that. Using cp command, it is possible to copy a directory along with a whole subdirectory with its content and everything under it.

The other way of copying the directories and files in Linux is async command.

The cp and rsync are among the most well-known commands for copying files and directories.

How to use cp command?

The simple way of using the cp command without specifying any options:

$ cp source_directory destination_directory

For example:

$ cp direcorty1 directory2

Using cp with options:

cp -option  source destination

With multiple options:

cp -opt1 -opt2  src dest

Note: You may see all the available options by executing this command:

cp –help
Copy all files and subdirectories example

In the cp command example, we will copy all the files and subdirectories (including their contents) from the source directory to the destination directory.

In order to copy all contents, use the cp command as follows:

$ cp -R /home/docs/office/ /jam/home/

The above command will copy all the contents of the office directory to the home directory. In that case, if the destination directory does not exist, it will create a new directory.

The -R option is used to copy directories recursively.

Note: The letter R is case insensitive. So, you may also use -r in the above command.

Preserving attributes by -a option

In order to keep the attributes like ownership, timestamps etc. you may use the -a option.

The example command used both options i.e. –r and –a as follows:

$ cp -aR /home/docs/office /jam/home/

 

Note: Linux is case-sensitive and needs the / after every directory to be aware it isn’t a document. The ~ is a special character from the terminal which automatically adjusts into the current user’s home directory. Should you have to understand what directory you’re in, use the pwd command.

How to display the copying process

For displaying the copying process, you may use the –v option in the cp command. An example command is shown below:

$ cp -avr /home/docs/office /jam/home/
How to copy multiple directories?

If you require copying multiple directories by using the cp command then just separate directories with space as shown in the example command below:

$ cp -avr /src1/* /src2/* /tar_dir
Using the * wildcard example

You may also copy all the files and folders from one directory to another by using the * wildcard. For example:

$ cp -v /home/welcome/Documents/* /backup/
The second way of copying in Linux: Using the rsync command

The rsync command is a file copying tool that you may use to copy the directory contents to the remote location as well.

Further Reading:  The echo Command in Linux

To see all available option with rsync command, execute this command:

$ rsync –help

This should output like this:

linux rsync

The syntax for using rsync command

The simple syntax for using the rsync command is:

rsync -ar  SOURCE DESTINATION

The example of rsync command

$ rsync -ar /home/src /media/dest

This command should copy the contents of the home/src folder to the media/dest folder.

Copy in archive mode example

The –a option tells to copy in the archive mode. This will copy the folder with all its permissions.

For example:

$ rsync -a /home/src /media/dest
Copy in compressed mode example

For copying the data by compressing the files, you may use the –z option. This is useful if you are copying to the remote locations. An example with a remote location is:

$ rsync -avz /home/src/ server1.yourservername.com:/home/media/dest/

Seeing the progress of copying by –p example

Use the –p option for seeing the progress of copying. An example:

$ rsync -avP /home/src/ server1.yourservername.com:/home/media/dest/
Copying multiple directories by rsync command

For copying multiple directories, separate the folder names by space:

$ rsync -a /dir1 /dir2 /dest/

FAQs about Copy Directory in Linux

Q1: How can I copy a directory in Linux using the cp command?A1: You can use the cp command by providing the source directory and the destination directory. For example:

$ cp source_directory destination_directory

Q2: What are the basic options available with the cp command for copying directories?

A2: The basic syntax is:

cp -option source destination

For example:

$ cp -R /home/docs/office/ /jam/home/

Q3: How can I copy all files and subdirectories, including their contents, from one directory to another using the cp command?

A3: Use the -R (or -r) option with the cp command to copy directories recursively. For instance:

$ cp -R /home/docs/office/ /jam/home/

Q4: What is the purpose of the -a option in the cp command, and how does it affect the copying process?

A4: The -a option preserves attributes like ownership and timestamps during the copying process. For example:

$ cp -aR /home/docs/office /jam/home/

Q5: How can I display the copying process using the cp command?

A5: Use the -v option with the cp command to display the copying process. For instance:

$ cp -avr /home/docs/office /jam/home/

Q6: Can I copy multiple directories simultaneously using the cp command?

Further Reading:  Why We Use Top Command in Linux?

A6: Yes, you can copy multiple directories by separating them with spaces. For example:

$ cp -avr /src1/* /src2/* /tar_dir

Q7: What is the second method to copy directories in Linux, and how does it differ from the cp command?

A7: The second method is using the rsync command. It is a file copying tool that allows copying directory contents to a remote location. For example:

$ rsync -ar /home/src /media/dest

Q8: How does the -z option in the rsync command contribute to the copying process?

A8: The -z option enables compression during the copying process, which can be useful when copying to remote locations. For instance:

$ rsync -avz /home/src/ server1.yourservername.com:/home/media/dest/

Q9: What is the purpose of the -p option in the rsync command, and how can it be used to monitor the copying progress?

A9: The -p option in the rsync command is used to see the progress of copying. For example:

$ rsync -avP /home/src/ server1.yourservername.com:/home/media/dest/

Q10: Can I copy multiple directories using the rsync command, and how should they be specified in the command?

A10: Yes, you can copy multiple directories by separating their names with spaces. For example:

$ rsync -a /dir1 /dir2 /dest/

Best practices for using Copy Directory commands

When copying directories in Linux using commands like cp and rsync, it’s important to follow best practices to ensure accuracy, efficiency, and avoid unintended consequences. Here are some best practices:

For cp Command:
Be Explicit with Options:

Clearly specify options to avoid unintended behaviors. For example, use -a to preserve attributes.

$ cp -aR /source /destination

Avoid Ambiguous Wildcards:

Be cautious when using wildcards (*) to prevent unexpected matches. Always double-check the paths.

$ cp -v /source/* /destination/

Use -i for Interactive Mode:

When copying to existing destinations, consider using the -i option to prompt for confirmation before overwriting.

$ cp -i /source/file.txt /destination/

For rsync Command:
Understand the -a Option:

Be aware that the -a option in rsync stands for archive mode, preserving permissions, ownership, and timestamps.

$ rsync -av /source/ /destination/

Consider -n for Dry Run:

Before executing a large copy operation, use the -n option for a dry run to see what would be copied without making changes.

$ rsync -an /source/ /destination/

Include –progress for Visibility:

To monitor the progress of the copy operation, include the –progress option.

$ rsync -av –progress /source/ /destination/

General Best Practices:

  1. Check Available Disk Space:
    • Before copying large directories, ensure that there is sufficient disk space on the destination.
  2. Backup Important Data:
    • For critical operations, consider making backups or using version control systems to avoid data loss.
  3. Use Absolute Paths:
    • Provide absolute paths to ensure accuracy, especially when working with scripts or automated processes.
  4. Test with Small Directories:
    • Before copying large datasets, perform tests with smaller directories to validate the commands and options.
  5. Check Permissions:
    • Ensure that the user running the command has the necessary permissions to read from the source and write to the destination.
  6. Be Mindful of Symlinks:
    • Understand how symbolic links are handled by the commands and whether you want them preserved or resolved.
  7. Consider Logging:
    • For critical operations, consider logging the copy process for future reference.
Further Reading:  What is grep Command in Linux?

Remember to adapt these best practices based on your specific use case and requirements. Always double-check commands, especially when dealing with important data or performing operations on production systems.

Let’s Wrap Up

To summarize, copying directories in Linux can be accomplished through various methods, primarily using the cp and rsync commands. The cp command is a straightforward tool for copying directories, and it can be used with different options to achieve specific results.

Copying with cp Command:

  • The basic syntax for copying with cp is: cp source_directory destination_directory.
  • The -R (or -r) option is used for recursive copying, including all subdirectories and their contents.
  • The -a option preserves attributes like ownership and timestamps during the copying process.
  • The -v option displays the copying process, providing visibility into the files being copied.

Example of cp Command:

$ cp -aR /home/docs/office/ /jam/home/

Copying Multiple Directories with cp:

$ cp -avr /src1/* /src2/* /tar_dir

Using Wildcard (*) with cp:

$ cp -v /home/welcome/Documents/* /backup/

 

Copying with rsync Command:

  • The rsync command is a powerful file-copying tool that works locally or remotely.
  • The basic syntax for using rsync is: rsync -ar SOURCE DESTINATION.
  • The -a option tells rsync to copy in archive mode, preserving permissions and other attributes.
  • The -z option enables compression during the copying process, useful for remote copying.
  • The -p option allows monitoring the progress of copying.

Example of rsync Command:

$ rsync -avz /home/src/ server1.yourservername.com:/home/media/dest/

Copying Multiple Directories with rsync:

$ rsync -a /dir1 /dir2 /dest/

In conclusion, the cp and rsync commands provide flexible and efficient ways to copy directories in Linux.

The choice between them depends on specific requirements, such as preserving attributes, monitoring progress, or copying to remote locations.

Understanding these commands enhances your ability to manage and manipulate directories in a Linux environment.