Computer terminal

How to copy directory in Linux

This article explains how to copy a directory in the Linux terminal. That's a very simple operation. In most cases, you can use the regular "cp" command. But it may require using some additional flags if you want to copy the whole directory.

Also, at the end of this article, we will describe how the "rsync" command works. It's a more advanced file-copying tool and it can work with remote destinations.

Now, please review the following samples. You can choose the solution that's the most suitable in your specific case.

Copying a single directory

The "cp" tool is available on any Linux system. It's a perfect choice if you have to copy just a single directory. The syntax is provided below.

cp -r <source_path> <destination_path>

As you see there, we've added the "r" flag. It specifies that the "cp" tool must copy the folder recursively, including all nested files and folders. Also, after that, you have to specify both source and destination location.

Now, the usage will be like the following:

cp -r /home/user/gallery /home/user/files

After you've executed the above command, you may want to verify that the files were correctly copied. To do that, you have to navigate to the destination location.

cd /home/user/files

Then, you may run the "ls" command to list all entries inside of that directory.

ls -la

The output may be like the following:

total 12
drwxr-xr-x  3 user user 4096 Dec 19 15:00 .
drwxr-xr-x 41 user user 4096 Dec 19 14:57 ..
drwxr-xr-x  2 user user 4096 Dec 19 15:00 gallery
user@user-pc:~/files$

We clearly see that the directory "gallery" is inside the "files" directory. It means that the "cp" command succeeded and the folder was successfully copied to the new location.

Additional options

It is worth mentioning that the cp tool can accept various additional flags. Below, we provide descriptions for the most popular options.

How to not overwrite the existing files

If there is a chance that the files already exist in the destination folder, you may want to not modify them. To solve that issue you have to use the "-n" flag. If it's set, the files will not be overwritten.

The syntax is like the following:

cp -n -r <source_path> <destination_path>

Interactive mode

Also, if you do decide to overwrite the destination files, there is a possibility that some files must not be modified at all. In other words, only part of the data must be overwritten, other files have to be kept as is.

In that case, you can use the interactive mode. If it's enabled, the cp utility will prompt if you'd like to copy some file or if it has to be skipped.

You have to use the "-i" flag to enable this mode. The usage is like following:

cp -i -r <source_path> <destination_path>

Verbose mode

To obtain a detailed log, you can use the "-v" flag. If it's set, the cp tool will output the list of copied files. For example, this can be helpful if the source folder is large and you'd like to see some status.

Please see the specification below, we've added the "-v" flag there.

cp -v -r <source_path> <destination_path>

How to preserve attributes

You can use the "-a" flag to preserve additional information when copying. This will allow us to also transfer additional information like the mode, ownership, timestamp, etc. This can be useful when archiving files or directories.

Please see our example below:

cp -a -r <source_path> <destination_path>

Updating the files

Also, the "cp" utility allows you to update the destination folder. It means that the file will be copied either if it's newer or if the destination file does not exist.

That mode can be enabled with the help of the "-u" flag. See the specification below:

cp -u -r <source_path> <destination_path>

Copying content from a single directory

You may also copy only the content of the specific directory. To do that, you have to add the "*" symbol after the forward slash. Please see our example below

cp -r /home/user/gallery/* /home/user/files

To confirm that the files were copied, you may navigate to the destination folder and then check if the files are there. The following command will change the current directory:

cd /home/user/files

Now, you can see the list of files:

ls -la

The output may be like the following:

total 8
drwxr-xr-x  2 user user 4096 Dec 27 16:52 .
drwxr-xr-x 41 user user 4096 Dec 26 16:07 ..
-rw-r--r--  1 user user    0 Dec 27 16:52 sample.txt

It is worth mentioning that if you use the "*" symbol, then the "cp" command will copy only regular files. However, if you'd like to copy the hidden files, you may directly specify those names in the Linux terminal, like it's shown below.

cp -r /home/user/gallery/.sample.txt /home/user/files

Copying multiple directories

The "cp" utility is very flexible. It allows copying multiple directories into the single destination. Following is the specification that shows how to do that in the Linux command line.

cp -r <source_path1> <source_path2> <source_pathN> <destination_path>

As an example, we'll try to copy two directories into the destination folder. Please see the following sample. The "gallery" and "photos" directories will be copied into the "files" directory.

cp -r /home/user/gallery /home/user/photos /home/user/files

Now, you can check if your content was moved. To do that, please navigate to the destination location and see what's inside of that directory.

cd /home/user/files
ls -la

You may see the result like the following:

total 16
drwxr-xr-x  4 user user 4096 Jan  5 20:39 .
drwxr-xr-x 42 user user 4096 Jan  5 20:38 ..
drwxr-xr-x  2 user user 4096 Jan  5 20:39 gallery
drwxr-xr-x  2 user user 4096 Jan  5 20:39 photos

Copying multiple files into a single directory

If you just need to copy a few single files into the desired location, then the "cp" utility may help you as well. You can also specify the desired files in a single command.

The usage may be like the following. Please pay attention, that the last argument is the path to the destination folder.

cp /home/user/gallery/image1.jpg /home/user/gallery/image2.jpg /home/user/files

After running the "cp" command, you can navigate to the destination folder and see if the files were copied.

cd /home/user/files
ls -la

The output may be like the following:

total 88
drwxr-xr-x  2 user user  4096 Jan  5 21:55 .
drwxr-xr-x 42 user user  4096 Jan  5 21:36 ..
-rw-r--r--  1 user user 39913 Jan  5 21:55 image1.jpg
-rw-r--r--  1 user user 39913 Jan  5 21:55 image2.jpg

Copying directories with help of rsync utility

There is also an alternative way of copying the directories locally on your computer. You can use the open-source rsync tool for that purpose. That's an advanced command line application that allows you to copy files or folders in an incremental way.

However, this tool may not be installed on your system. As a result, you may need to get it on your computer. Following are the instructions for the various distributions of the Linux operating system.

Installing the rsync utility

To install the rsync tool on the Ubuntu Linux, you may run such a command:

sudo apt install rsync

For the Fedora or CentOS Linux systems, you may use the "dnf" tool. Please see our sample below.

sudo dnf install rsync

Also, to install the rsync tool on the OpenSUSE system, you may want to use the "zypper" package manager. The usage is shown below.

sudo zypper in rsync

Using the rsync utility

To copy directories locally, you have to run the "rsync" tool with the proper flags and arguments. Please review the following specifications, it shows how the tool may be used.

rsync -av <source_path> <destination_path>

The "-a" flag is added there to enable the archive mode. Also, "-v" flag was added to enable the verbose mode. But the "rsync" tool provides more other options. You can view the official documentation to see how this tool can be configured.

For example, to see the man pages you can run such a command in the console:

man rsync

Now, you can see how to copy the folder in the terminal. Following is our sample.

rsync -av /home/user/gallery /home/user/files

Copying directory to the remote destination

You may also want to copy the folder to the remote destination. That's also possible in the Linux terminal. Such a case may occur, for example, when you have a web server and need to update some files there. Please review the following sections to see how it can be done.

Using the rsync tool

The "rsync" utility can help in copying a directory to the remote location as well. You just have to provide the address of the remote computer in a specific format. Please review the specification below.

rsync -av <source_path> <remote_user>@<remote_host>:<remote_path>

Following is the example that shows how to transfer the "gallery" folder to the other computer over the network.

rsync -av /home/user/gallery root@192.168.0.0:/home/work/pictures

Using the scp tool

The "scp" tool uses the SSH protocol to copy files or folders to the remote destination. You also may use it to transfer your files to the desired server. Below, you can see the specification of the scp command that will transfer content from local drive to the remote host.

scp -r <source_path> <remote_user>@<remote_host>:<remote_path>

Following is the example of using the scp command:

scp -r /home/user/gallery root@192.168.0.0:/home/work/pictures

Conclusion

The Linux operating system provides various tools that you can run to copy directories locally on your computer. The most common is the "cp" command. It is simple to use and provides various helpful options.

Also, you may use the more advanced "rsync" utility. It may be useful in transferring the archives or making some backups of your files. Also, it uses the algorithm that allows copying files incrementally.

Finally, this article provides several examples of how to transfer directory to the remote destination. You can use either the "rsync" or the "scp" tool for that purpose.

It is worth mentioning that there are much more helpful commands available in Linux. However, there may be other ways of transferring the directories between hosts. For more information, you may want to read the official Linux documentation or tutorials.

Related Articles

Typing on a keyboard

How to unzip a file in Linux terminal

Magnifying glass

How to find all files containing a specific text on Linux

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *