Difference Between cp -r and cp -a in Linux

The “cp -r” and “cp -a” are Linux commands utilized for recursively copying directories and their contents. The “cp -a” preserves all file attributes, including ownership, permissions, timestamps, and symbolic links, while “cp -r” only preserves the ownership and permissions of the files.

This article will provide the usage of “cp -r” and “cp -a” commands and their key differences in Linux.

Working of cp -r Command 

In the “cp -r” command, the -r option stands for “recursive,” which means that it copies the contents of the specified directory and any subdirectories and their contents.

Example

A directory named “Folder” is considered in the home directory that contains a file named “file.txt” and a subdirectory named “dir”. To check the attributes of the file, use the “ls -la” command as below:

$ ls -la file.txt

The “file.txt” has the “-rw-rw-r–” permissions and “Dec 12 04:36” timestamp.

The “dir” contains a file named “sub_file.txt” having file attributes that can be checked via the “ls -la” command:

$ ls -la dir/sub_file.txt

The “dir/sub_file.txt” has the “-rw-rw-r–” permissions and “Dec 12 04:36” timestamp.

Copy the Entire Directory to a New Directory

To copy the entire “Folder” directory to a new directory named “backup” in the home directory, use the “cp -r” command. To do this, run the following command:

$ cp -r Folder ~/backup/

After running the above command, check the attributes of the copied directory and files in the “backup” directory and see that some attributes are preserved:

$ ls -la ~/backup/Folder

The output shows that the “cp -r” command does not preserve all file attributes, such as permissions and timestamps. In those cases, the user may need to use the “cp -a” command that supports preserving those attributes.

Working of cp -a Command 

In the “cp -a” command, the “-a” option stands for “archive,” which is an efficient way of specifying the number of other options that are commonly used when making backups or preserving file attributes.

Example:

The “cp -a” command in Linux is a versatile tool that can preserve various file attributes such as timestamps, symbolic links, file ownership, and permissions. Here is an example demonstrating how cp -a preserves all these file attributes while copying a directory. 

A directory named “Folder” in the home directory containing a file named “file.txt”. The file.txt has the following ownership and permissions:

$ ls -la ~/Folder

The “Folder” has the “-rw-rw-r–” permissions and “Dec 12 04:36” timestamp.

Using cp -a Command to Copy Folder Directory 

To copy the entire Folder directory to a new directory named “backup” in the home directory, use the “cp -a” command. To do this, run the following command:

$ cp -a Folder ~/backup/

After running the above command, check the attributes of the copied directory in the backup directory are preserved:

$ ls -la ~/backup/

The cp -a command preserves the following attributes: timestamps (access time, modification time, and change time of the copied file and directory are the same as the original file and directory), ownership, permissions.

Which One is Better For Copying?

The “cp -a” command is similar to the “cp -r” command. Still, it includes some additional features to ensure that the copied files are exact duplicates of the original files, including all of their attributes. This makes it a valuable command for creating backups or transferring files between different systems while maintaining the same attributes.

Conclusion

The “cp -a” is a more comprehensive command for preserving file attributes when copying directories and their contents. In contrast, the “cp -r” is a simpler command that preserves ownership and permissions. Both these commands have pros and cons depending on the specific needs and the nature of the files being copied.This article has explained the working and difference between the two “cp” command variants, i.e., “cp -r” and “cp -a”.