BASH Programming

Copying Files and Directories in Linux

In this guide, we will learn about copying files and directories in Linux.

Prerequisites:

To perform the steps that are demonstrated in this guide, you need the following components:

Copying Files and Directories

Copying is one of the most fundamental operations that you have to perform when operating a computer. In Linux, there are multiple tools that are available for copying files and directories:

  • cp: The default tool for copying files and directories. It’s available on all Linux distros.
  • rsync: A powerful tool that is primarily used for file synchronization. However, we can also use it for copying files and directories.

For demonstration purposes, we configured two directories: /tmp/source and /tmp/destination.

$ tree /tmp/source

$ tree /tmp/destination

Copying Files and Directories Using Cp

The command structure of cp is as follows:

$ cp <options> <source> <destination>

For all the available options, check out the man page:

$ man cp

Copying a File

The following command copies the “1.txt” from /tmp/source to /tmp/destination:

$ cp -v /tmp/source/1.txt /tmp/destination

$ tree /tmp/destination

If you specify a different file name as the destination, cp renames it accordingly:

$ cp -v /tmp/source/1.txt /tmp/destination/test.txt

$ tree /tmp/destination

Copying Multiple Files

The following command copies all the text files under /tmp/source to /tmp/destination:

$ cp -v /tmp/source/1.txt /tmp/source/2.txt /tmp/source/3.txt /tmp/source/4.txt /tmp/source/5.txt /tmp/destination

$ tree /tmp/destination

If the source files are named in a pattern, cp can work with that:

$ cp -v /tmp/source/*.txt /tmp/destination

$ tree /tmp/destination

Copying a Directory

In the next example, we will copy the “subdir1” to /tmp/destination:

$ cp -v -r /tmp/source/subdir1 /tmp/destination

$ tree /tmp/destination

Here, the “-r” flag tells the cp command to copy the directory and all its content recursively to the destination.

If a different directory name is specified in the destination, cp renames it accordingly:

$ cp -v -r /tmp/source/subdir1 /tmp/destination/yellow

$ tree /tmp/destination

If the destination directory already exists, cp copies the source directory and everything it contains. However, we can specify to only copy the files and sub-directories, not the target directory:

$ cp -v -rT /tmp/source/subdir1 /tmp/destination/yellow

$ tree /tmp/destination

Copying Files and Directories Using Rsync

The primary usage of rsync is synchronizing the files between the local/remote servers. It comes with numerous additional features. However, we can also use it for “syncing” files from one directory to another (copying in other words).

The rsync command structure is as follows:

$ rsync <option> <source> <destination>

Check out the man page for all the available options:

$ man rsync

Copying Files

The following command copies the “1.txt” from /tmp/source to /tmp/destination:

$ rsync -a /tmp/source/1.txt /tmp/destination && tree /tmp/destination

Here, the “-a” parameter tells rsync to operate in archive mode.

We can also copy the file with a different name in the destination:

$ rsync -a -v /tmp/source/1.txt /tmp/destination/different.txt && tree /tmp/destination

To copy multiple files, specify the files one by one or describe the pattern to match:

$ rsync -a -v /tmp/source/1.txt /tmp/source/2.txt /tmp/destination && tree /tmp/destination

$ rsync -a -v /tmp/source/*.txt /tmp/destination && tree /tmp/destination

Copying Directories

Unlike cp, to copy the directories with its files and sub-directories, there’s no change of syntax with rsync. When working in archive mode, rsync automatically copies all the contents recursively.

One key difference here is the trailing slash (/).

  • If a slash is present in the source directory, rsync copies the contents of the source directory to the destination.
  • If a slash isn’t present in the source directory, rsync copies the source directory inside the destination directory.

The following commands demonstrate the difference perfectly:

$ rsync -a -v /tmp/source /tmp/destination

$ tree /tmp/destination

$ rsync -a -v /tmp/source/ /tmp/destination

$ tree /tmp/destination

Copying Files to Remote Machine Using Scp

The scp command is a specialized tool that copies the files and directories over SSH. It requires having the OpenSSH server installed on the remote host.

The fundamentals of the scp command are the same as the cp command. Learn more about the scp command.

Copying Files

In the following command, we copy the Ubuntu 22.04 ISO to the /tmp directory on a remote host:

$ scp ubuntu-22.04-desktop-amd64.iso root@192.168.99.15:/tmp

To copy a file from a remote host, use the following command:

$ scp root@192.168.99.15:/tmp/ubuntu-22.04-desktop-amd64.iso.

Copying Directories

To copy a directory to a remote host, we use the following scp command:

$ scp -r test_dir root@192.168.99.15:/tmp

Here, the “r” parameter is to operate in recursive mode (needed to copy the directory).

Conclusion

We showcased the various ways of copying the files in Linux. We demonstrated how to use the cp and the rsync commands to copy the files and directories locally. We also showcased how to use the scp command to copy the files and directories to a remote host.

For more advanced copying and backup configurations, rsync is a better option. It can make a system backup, sync with multiple remote hosts, update new files, and more.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.