Copying Directories in Linux with cp and rsync

A directory is a structure in a file system that stores files and other directories. It organizes data into a hierarchical format, helping users and operating systems manage information efficiently. Each directory can include files and subdirectories, forming a branching tree-like layout that can extend to multiple levels of data.

This guide outlines how to copy a directory in Linux using both the cp and rsync commands.

Prerequisites

  • Access to a Linux-based environment with a non-root sudo user account.

Commands for Copying Directories in Linux

Below are the commands used to copy directories in Linux.

cp Command

The cp command copies files and directories in Linux. It is available in all Linux distributions and follows this syntax:

cp [OPTIONS]... [SOURCE] [DESTINATION]

  • OPTIONS: Flags that adjust the command’s behavior.
  • SOURCE: The file or directory you intend to copy.
  • DESTINATION: The target directory path or filename for renamed copies. The command creates the destination directory if it does not exist.

cp Command Options

  • -r or -R: Recursively copy directories and their contents.
  • -a: Recursively copy while preserving symbolic links, permissions, ownership, and timestamps.
  • -v: Show the copying process in detail.
  • -p: Maintain file attributes such as mode, ownership, and timestamps.

rsync Command

The rsync command synchronizes files and directories between two locations, locally or remotely. It can copy data between local paths or between local and remote systems. Rsync uses timestamps and file sizes to identify changes, making it ideal for backups and large data transfers.

Local Copy

rsync [OPTIONS]... [SOURCE_DIRECTORY]/ [DESTINATION_DIRECTORY]/

Local to Remote Copy

rsync [OPTIONS]... [SOURCE_DIRECTORY]/ [USER@REMOTE_HOST]:[DESTINATION_DIRECTORY]/

Remote to Local Copy

rsync [OPTIONS]... [USER@REMOTE_HOST]:[SOURCE_DIRECTORY]/ [DESTINATION_DIRECTORY]/

Syntax Explanation

  • OPTIONS: Flags that adjust behavior, e.g., -a (archive mode), -v (verbose), -r (recursive).
  • SOURCE_DIRECTORY/: The source directory. A trailing slash copies only its contents, not the directory itself.
  • DESTINATION_DIRECTORY/: The target directory for the copy, created automatically if absent.
  • USER@REMOTE_HOST: Defines the remote system (user, host, or IP) for remote transfers.

rsync Command Options

  • -a: Archive mode (recursive copy, symbolic links, permissions, timestamps, ownership preserved).
  • -v: Show detailed output during the copy.
  • -r: Recursively copy directories and their contents.
  • -z: Compress data during transfer to reduce bandwidth usage.

Copying a Directory with the cp Command

The following steps show how to create a directory structure, add files, and copy directories using cp with different options.

  1. Create a new directory in the current location:
  1. Move into the new directory:
  1. Create a file named file1.txt:
  1. Add content to file1.txt:

$ echo "This is a sample file in the main directory." > file1.txt

  1. Create a subdirectory named folder1 inside sample:
  1. Create a file in folder1:
  1. Add content to that file:

$ echo "This file is inside folder1." > folder1/file2.txt

  1. Copy folder1 into folder1-backup:

$ cp -r folder1 folder1-backup

The -r option copies all contents recursively.

  1. List all contents to confirm the copy:
  1. Go back to the parent directory:
  1. Copy the entire sample directory to sample-copy:

$ cp -r sample sample-copy

  1. Copy with -v and -p flags:

$ cp -rvp sample sample-copy-rvp

  1. Copy using the -a flag:

$ cp -a sample sample-copy-a

  1. Verify the copied directories and their structure:

$ ls -l sample sample-copy sample-copy-rvp sample-copy-a

The output shows that sample-copy has a newer timestamp because it was copied without the -p or -a flags. Copies made with -rvp and -a preserved timestamps and permissions.

Copying a Directory with Rsync

rsync is a versatile command-line tool for synchronizing files and directories. It is more advanced than cp and is particularly useful for backups, partial updates, and large-scale data transfers. Unlike cp, rsync copies only the differences between the source and destination, which reduces both time and bandwidth usage. Follow the steps below to copy directories with rsync.

rsync is pre-installed on most Linux systems. You can check if it’s available on your system with:

If rsync is missing, refer to its installation instructions.

Ensure you are in the directory containing the sample directory.

View the structure of sample:

Copy the sample directory to a new directory using rsync:

$ rsync -av sample/ sample-rsync/

This command copies the contents of sample to sample-rsync/ in archive mode, preserving file attributes and displaying detailed output.

Check the copied directory:

Add a new file to the sample directory:

$ echo "This is a new file." > sample/extra.txt

This creates a file that is not yet present in sample-rsync.

Preview changes without applying them using the --dry-run flag, which simulates the copy process:

$ rsync -av --dry-run sample/ sample-rsync/

Output:

sending incremental file list
./
extra.txt

sent 254 bytes  received 24 bytes  556.00 bytes/sec
total size is 123  speedup is 0.44 (DRY RUN)

Note: extra.txt appears in the output because it is new. rsync detects and lists changes, transferring only what’s needed.

Update sample-rsync and display transfer progress using --progress:

$ rsync -av --progress sample/ sample-rsync/

Output:

sending incremental file list
./
extra.txt
            20 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/7)

sent 314 bytes  received 40 bytes  708.00 bytes/sec
total size is 123  speedup is 0.35

From this output, you can see only extra.txt was copied.

Verify the updated directory:

Remove a file from sample:

Use --delete to remove files from sample-rsync that no longer exist in sample:

$ rsync -av --delete sample/ sample-rsync-copy/

Ensure both directories are synchronized:

$ ls -l sample sample-rsync

Output:

sample:
total 12
-rw-rw-r-- 1 user user   45 May  7 06:06 file1.txt
drwxrwxr-x 2 user user 4096 May  7 06:06 folder1
drwxrwxr-x 2 user user 4096 May  7 06:07 folder1-backup

sample-rsync:
total 12
-rw-rw-r-- 1 user user   45 May  7 06:06 file1.txt
drwxrwxr-x 2 user user 4096 May  7 06:06 folder1
drwxrwxr-x 2 user user 4096 May  7 06:07 folder1-backup

You can see that extra.txt has been removed, while the rest of the directory structure remains intact.

Conclusion

You have learned how to copy directories in Linux using both the cp command and rsync. Each has advantages depending on the use case. For more details, run man cp for the cp manual page and man rsync for the rsync manual page.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: