
A directory is a file system structure that contains files and other directories. Directories organize files in a hierarchical structure, which helps users and operating systems manage data. Each directory can contain both files and other directories, also called subdirectories, creating a tree-like structure with branches that lead to various levels of data.
This article explains how to copy a directory in Linux using the cp command and Rsync.
Before you begin, you need to:
The following are the commands for copying directories in Linux.
cp CommandThe copy command cp is used to copy files and directories in Linux. The copy command is available in all Linux distributions and follows the syntax below:
OPTIONS: Flags that modify the command's behavior.SOURCE: Specifies the file or directory you want to copy.DESTINATION: Directory path you want to copy the source to or a filename if you want to copy and rename a file. The cp command automatically creates a destination directory if it does not exist.cp Command OptionsThe following are the options you can use with the cp command.
To learn more about cp, check our detailed article on using the cp command.
rsync CommandThe rsync command synchronizes files and directories between two locations, either locally or remotely. Below is the syntax for different use cases. It can copy files locally and between a local and a remote system. Rsync compares timestamps and file sizes to determine what needs to be copied, making it suitable for backups and large-scale data transfers.
Local Copy
Local to Remote Copy
Remote to Local Copy
OPTIONS: Flags that modify the command's behavior, such as -a (enables archive mode to preserve permissions and attributes), -v (displays verbose output), or -r (performs recursive copy).SOURCE_DIRECTORY/: The directory from which to copy. The trailing slash (/) copies the directory's contents, not the directory itself.DESTINATION_DIRECTORY/: The target directory to copy contents. Rsync creates it if it doesn't exist.USER@REMOTE_HOST: Specifies the remote system with the username, hostname, or IP address for remote transfers.rsync Command OptionsYou can use the following options with the rsync command.
Check our detailed article on using rsync to learn more about the command.
cp CommandIn this section, you will create a directory structure, add files and content, and copy directories using the cp command and various flags.
Create a new directory in your present working directory.
Navigate to the created directory.
Create a file named file1.txt.
Add content to the file1.txt file.
Create a subdirectory such as folder1 inside the sample directory.
Create a file inside folder1.
Add content to the file.
Copy folder1 into the folder1-backup directory using the cp command.
The -r option enables recursive copying, which copies all the content of the sample directory.
List the content of the directory to verify the copy.
Navigate back to the parent directory.
Copy the entire sample directory into the sample-copy directory.
Copy the sample directory using the -v and -p flags.
This command recursively copies the sample directory to sample-copy-rvp, preserving file attributes and showing detailed output.
Copy using the sample directory using the -a flag.
This command copies the sample directory to sample-copy-a in the archive mode.
Verify that the copies exist and contain the correct structure with the command below.
This command lists each directory and its contents in a detailed format.
In the output, notice that sample-copy has a newer timestamp. This is because the copy command did not use the -p or -a flags. Both sample-copy-rvp and sample-copy-a preserved the original timestamps and permissions from the sample directory.
Rsync is a powerful command-line tool for synchronizing files and directories. It is more advanced than cp and useful for backups, partial updates, and large-scale data transfer. Unlike cp, rsync only copies differences between source and destination directories, which saves time and bandwidth. Follow the steps below to copy directories using Rsync.
Rsync is installed by default on most Linux distributions. Confirm if it is available on your instance.
If Rsync is missing, refer to the Rsync installation steps to install it.
Ensure you are in the directory that contains the sample directory.
View the contents of sample to confirm its structure.
Copy the sample directory to a new directory using Rsync.
This command copies the contents of the sample directory to sample-rsync/ in archive mode, preserving file attributes and showing verbose output.
Verify the contents after copying.
This command lists the contents of the sample-rsync directory.
Add a new file to the sample directory.
This creates a file that does not exist in the sample-rsync directory.
Preview changes without applying them using the --dry-run flag. The --dry-run flag in rsync simulates what will happen during a copy without making any changes to files or directories.
Output:
extra.txt appears in the output because it is new and not in the sample-rsync directory. Rsync identifies and lists the changes, in this case, just the extra.txt file, and copies the changes, making the transfer efficient.
Use rsync to update the sample-rsync directory and view real-time file transfer progress using the --progress flag.
Output:
From the output, you can see that Rsync only copies the newly created extra.txt file
Check if the new file appears in the copied directory.
Delete a file from the sample directory.
Use the --delete flag to remove files in sample-rsync that no longer exist in sample. Rsync updates the destination by deleting the file.
Confirm that both directories are in sync by running the following command.
Output:
You can see that extra.txt has been removed from both directories, and the rest of the structure remains unchanged.
You have copied directories in Linux using the cp command and Rsync. You can use these commands based on your use cases. For more command options, run the man cp command to view the cp manual page and the man rsync command to view the Rsync manual page.
0 Comments
Be the first to comment and share your perspective with the community.