How to Copy Directory in Linux

How to copy directory in Linux

On Linux and Unix systems, copying files and directories are one of the countless everyday tasks you’ll perform regularly.

cp is a command for copying files and directories on any Unix and Linux system.

In this post, we will demonstrate how to employ the cp command and taking advantage of it.

How to copy Directory on Linux: Using cp command

The generic syntax for the cp command is as below:

cp [OPTIONS] SOURCE… DESTINATION

The SOURCE can include one or more directories and files as arguments. The DESTINATION argument must be an individual file or directory.

example:

cp fileA fileA.bkp

Both file_name and file_name.bkp are files. In this example, we are creating a copy from the fileA (making a backup).

cp -R source destination

Where source and destination are folders, we copy the “source” directory inside the “destination” folder. Note that in this example, we have used the -R argument. Why? It is because we are telling the cp command to copy this directory recursively. Thus, in the case of directories, the -R argument is necessary.

How to Copy Directory on Linux – Real Example 1

Let’s do a real example. For example, you decided to format your computer to install a new version of Linux Ubuntu. You would like to backup your home directory to your USB external drive and save all files from it.

cp -R /home/bitslovers /mnt/media/usb01

Here in this example, my home directory is bitslovers. My USB device is mounted on /mnt/media/usb01, so it is our destination folder.

How to copy multiple files in Linux

To copy more than one file at once, you need to specify them separated by space. The last parameter should be your destination directory. For example:

cp file1 file2 file3 … fileX destination-folder/

Copy only the content with cp command

But, sometimes, you need only copy the content of the directory only, not the folder itself, to inside the destination. First, consider that scenario: You need to move all your photos from your USB device to a specific folder inside your computer that you are splitting by year.

So, the folder (source) that contains the photos is on /mnt/media/usb01/DCM0001. We want to copy all files from DCM0001 to /home/bitslovers/Pictures/2021 (destination). Our goal is not to copy the DCM0001 folder itself. We intend to copy only the files from it. 

Here is the command:

cp -R /mnt/media/usb01/DCM0001/* /home/bitslovers/Pictures/2021

Note: to copy the content of a directory recursively. You have to use the cp command with the -R parameter and define the source directory attended by a wildcard (*) character.

Using the option -T to copy files on Linux

Alternatively, to copy only the files and subdirectories but not the source directory, we can also use the -T option, so our example will be like this:

cp -RT /mnt/media/usb01/DCM0001 /home/bitslovers/Pictures/2021

What is the difference between using -T and not use? It’s simple but crucial: The downside of using the cp command with a wildcard (*) is that this method does not copy the hidden files and directories.

Note: on Linux, the hidden files and folders start with a dot. If you would like to go deep into the Linux world, we have a complete guide.

How to avoid file overwriting a file with the cp command?

Sometimes we want to copy a file, but we don’t want to override it if it already exists.

To achieve that, we can use the -n option with the cp command to skip and not overwriting an existing file.

So, it will only copy the source file if the destination directory does not have it with the same file name. In other words, if the file exists, then the command will run but won’t apply any changes:

cp -n /home/bitslovers/backup-apache2.sh /home/bitslovers/backup-scripts

In addition, for security, if you would like that cp command to always prompt for confirmation before overriding the files, you can use the option -i.

cp -i /home/bitslovers/backup-apache2.sh /home/bitslovers/backup-scripts

So, if the file backup-apache2.sh already exists on the destination folder, you will see a message like this:

cp: overwrite '/home/bitslovers/backup-apache2.sh'? n

How to Copy multiple directories with cp on Linux

To copy various directories on Linux, you have to apply the cp command and enter the different directories to be copied as well as the destination folder, for example:

$ cp -R <folder-1> <folder-2> ... <folder-n> <destination-folder>

Imagine one scenario as an example: we need to copy the “/etc” directory and all homes directories located in the “/home/bitslovers” directory.

To accomplish that, we would run the following command

cp -R /etc /home/bitslovers /mnt/media/DCM0001

Copy Preserve Permissions Linux

Sometimes we need to copy files to another location. Still, we need to keep the same attributes like modeownership, and timestamps. So the standard behavior, when you execute the copy command, the new file will have different attributes. For example, suppose you are copying one file using sudo. In that case, the new copy will be owned by the root user automatically. 

This scenario could be an issue if the previous owner needs to access this file because this new file belongs to the root user. So, to fix that issue, we can copy the file using “-p” as a parameter for the cp command.

  • -p same as –preserve=mode,ownership,timestamps

Let me show you an example:

sudo cp -p /home/bitslovers/.bashrc /home/bitslovers/backup

So, in this example above, the new file under the backup folder will be accessible for the user bitslovers.

Copy Files and Preserve Creation Date on Linux

If you need to preserve the timestamps (the creation date):

sudo cp --preserve=timestamps /home/bitslovers/.bashrc /home/bitslovers/backup

How to force copy in Linux

To force a copy on Linux, we need to append the option -f on the cp command, for example:

cp -f ~/.bashrc /tmp

Prompt Before Overwrite

When the cp no prompt before overwrite, you need to use the option -i.

for example:

cp -i /tmp/.bashrc /home/bitslovers/

If the file .bashrc already exists on the home directory, you will see a prompt:

cp: overwrite ‘/home/bitslovers/.bashrc’?

Type n or y to confirm.

How to copy link files on Linux

The cp command, by default, does not copy link files while executing the copy. So, you can add -d option to copy the link files like the example below:

cp -d /home/bitslovers/backup-apache2.sh /usr/bin/

How to move files from a specific extension to a particular folder?

Following, use xargs and cp to copy all the images to an external hard drive.

ls *.png | xargs -n1 -i cp {} /home/bitslovers/Pictures

The command xargs is a powerful combination with any Linux command. I want to recommend reading the post xargs examples to give you professional skills to handle complex scenarios. The limitation is your imagination!

How to copy symbolic links

Let’s suppose that you need to copy a directory, and we have a couple of symbolic links on it. And we need to copy all files from this directory to a new location, but not as symbolic links. So, we can use the -L option to copy all files without reference to the original files, removing the Symbolic Link.

cp -rL /tmp/docs/ /home/bitslovers/

Copy newest file in directory Linux

If you would like to copy only files that were changed and copy new files that don’t exist on the destination, you can use the option -u.

In other words, copy only when the SOURCE file is newer than the destination file or when the destination file is missing.

Example:

cp -ru /tmp/docs/ /home/bitslovers/

In the example above, we are copying all files from the docs folder, which is newer than those already existing on the bitslovers folder. Also, we are copy all files that do not exist yet in the bitslovers folder.

How to create a Hard Link

The cp command allows us to copy files creating Hard Links for each one. Thus, hard Link is an alternative for creating a backup automatically and replicating all changes in real-time to the backup file. And in my opinion, it is much better than the Symbolic Link. 

Let me explain why:

Hard Link behaves differently when the Link’s source (what is being linked to) is moved or removed. 

However, because symbolic links never update (they only include a string that is the target’s pathname), hard links always refer to the source, even if moved or removed. Therefore, I would say that Hard Link it’s an unbreakable version of Symbolic Link.

For instance, if we have a file blog-post.doc. If we make a hard link to the file and then delete the file, we can still access the file using Hard Link. However, if we make a Symbolic Link of the file and remove the file, we can’t access the file through it, and the Symbolic Link becomes dangling. 

Hard Link extends the reference number of a location while Symbolic Link works as a shortcut (like Windows).

Cool, right? Let me show you how to copy and create Hard Link:

cp -lr ~/Documents /tmp

In the example above, if you accidentally delete one file from the Documents or tmp folder, you can still access the file. And, any change on the files will be replication to each other.

How to copy Directory (s) to Remote Hosts on Linux

Sometimes, you may need to copy a directory and files to a server to install some application or make a backup.

We will need to transfer using a network, which could be the internet or a local network.

How to Copy Directory and files using rsync on Linux

Like the cp command, we need to specify the origin and destination. The rsync follows the same approach. Most importantly, always use -r, which means recursive for this command. It will guarantee that you copy all content followed by option -a, which means “ALL,” this will copy all non-regular files.

Before continuing, you can check if you have the rsync command installed so that you can use that command:

whereis rsync

You will see an output like this:

rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

If you don’t have installed, it’s simple to install it:

For Ubuntu or any Debian based Linux:

sudo apt-get install -y rsync

If you are running CentOS or Fedora or any RedHard based:

sudo yum install -y rsync

So, the basic syntax for rysnc command:

$ rsync -ar <origin-folder> <user>@<host>:<path>

Real Example – How to use rsync

Our scenario as an example: Let’s suppose that you need to back up the configuration files from your web server running apache2.

rsync -ar /etc/apache2 [email protected]:/home/bitslovers/apache2_backup

We are connected to the server (3.324.174.100). In this example that we have the webserver apache2 running. 

And, we are copying that folder “/etc/apache2” from that server to another remote host (3.324.174.201).

Note: You must have SSH access to the remote host (in my example, 3.324.174.201) and, besides privileges, write on the destination directory (on example /home/bitslovers). In other words, you have to have the credentials for the bitslovers user.

Like the cp command, you can choose to copy the “/etc/apache2” content instead of the directory itself by adding a wildcard character following the directory to be copied.

rsync -ar /etc/apache2/* [email protected]:/home/bitslovers/apache2/

How to copy directory and files using scp on Linux

The scp is very similar to the rsync command and the way you use it too.

So, to copy directories using it, we need also to use the “-r” option that means the “recursive” method, besides our origin and destination.

scp -r <folder> <user>@<host>:<path>

Let’s use the same example from our rsync command. We need to copy the “/etc/apache2” directory to a backup server at 3.324.174.201 in the “/home/bitslovers/apache2” folder.

the following command accomplishes that:

Real Example – How to use scp

scp -r /etc/apache2 [email protected]:/home/bitslovers/apache2

Alternatively, you can revert the scp command like this:

scp -r [email protected]:/home/bitslovers/apache2 /etc/apache2 

Revert this command sometimes is helpful because sometimes we are already connected to the destination server. We don’t want to disconnect. In other words, we revert the origin and destination if you are already connected to the destination server.

There is any difference between rsync and scp?

The significant difference between these commands is how they copy files:

scp essentially reads the source file and writes it to the destination. It does a traditional linear copy. It could be locally or over a network.

Otherwise, the rsync command also copies files locally or over a network. But it applies a unique delta transfer algorithm and some optimizations to gain the operation a lot quicker.

Conclusion

Copying files and directories on Linux with the cp, scp, and rsync command is straightforward. Also, we can combine the cp command with the Find command, so in that way, we can perform more elaborate copy operations.

Also, a good tip: To transfer or copy files, consider compressing the files to a zip or tar.gz format. So, you can save time for big files or multiple files.

To learn more about different options available to use with the commands, type man cp, man scpor man rsync in your terminal.

2 thoughts on “How to copy directory in Linux”

  1. Članci O Zdravlju Od Liječnika

    Hello! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m undoubtedly enjoying your blog and look forward to
    new updates.

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.