×
Copy Directory/Folder in Linux via Command Line

There are various commands in Linux operating systems to copy a folder. The cp command helps you to do so. To organize files on your server, you will need to be copying. With cp command, you can copy a directory and an entire subdirectory with its content and everything beneath it. cp and rsync are one of the most popular commands for copying files and directory.

In this tutorial, we will explain how to copy the folder in Linux operating system.

Basic Syntax of cp Command

cp command is used to copy files or directories in Linux. It creates an exact copy of a file on a disk with different name.

basic syntax of cp command is shown below:

cp [OPTION] Source Destination
cp [OPTION] Source-1 Source-2 Source-3 Destination

You should see all the options available with cp command by running the following command:

cp --help

You should see the following screen:

cp-help

Copying Directories with cp Command

If you want to copy directory, including all its files and subdirectories, use -R or -r option with cp command.

For example, copy /etc directory to the /opt directory with the following command:

cp -R /etc /opt/

The above command will create a destination directory and copy all files and subdirectories recursively to the /opt directory.

If the destination directory already exists and you want to copy only the files and subdirectories but not the target directories, run the cp command with -T option as shown below:

cp -RT /etc /opt/

If you want to preserve the specified attributes such as, ownership, timestamps, context and links, run the cp command with -a option as shown below:

cp -aR /etc /opt/

If you want to display output during the copying process, use -v option with cp command:

cp -avR /etc /opt/

You should see the output as shown below:

cp-verbose

If you want to copy only the content of /etc directory to /opt, you will need to specify the star wildcard as shown below:

cp -avR /etc/* /opt/

If you want to copy multiple directories like, /etc and /var to /opt directory run the following command:

cp -avR /etc /var /opt/

Note : In order to copy directories, you must have read permissions on source directory and write permissions on the destination directory.

Copying Directories with rsync Command

rsync is an advanced file copying tool that allows you to copy directory across local the remote location.

The basic syntax of rsync command is shown below:

rsync [OPTION] SOURCE DESTINATION

You can see all the options available with rsync command as shown below:

rsync --help

You should see the following screen:

rsync-help

To copy /etc directory to /opt, run the following command:

rsync -a /etc /opt

In the above command -a option will copy a directory with all its permission and other information including recursive copy. If the destination directory exists rsync will overwrite it.

If you want to copy only the contents of the /etc directory then put a trailing slash / at the end of /etc:

rsync -a /etc/ /opt

You can use rsync with -v option to display the verbose output as shown below:

rsync -av /etc /opt

You should see the following screen:

rsync-verbose

If you want to display progress during the copying process, run the rsync with -P option as shown below:

rsync -avP /etc /opt

You can also use -z option with rsync command to compress file data during the transfer:

rsync -avz /etc /opt

If you want to exclude a specific directory from the source, run the following command:

rsync -a --exclude 'directoryname' /opt/

To excluse multiple directories, run the following command:

rsync -a --exclude {'directory1', 'directory2', 'directory3'} /opt/

If you want to copy multiple directories to /opt, run the following command:

rsync -a /etc /usr /opt/

Conclusion

In the above tutorial, we learned how to copy a directory with cp and rsync command. For more information, you can visit the Rsync official documentation at Rsync Doc.