Jump to content

Copying Files and Directories in Linux


Linux Hint

Recommended Posts

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

word-image-28830-1.png

$ tree /tmp/destination

word-image-28830-2.png

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

word-image-28830-3.png

Copying a File

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

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

word-image-28830-4.png

$ tree /tmp/destination

word-image-28830-5.png

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

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

word-image-28830-6.png

$ tree /tmp/destination

word-image-28830-7.png

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

word-image-28830-8.png

$ tree /tmp/destination

word-image-28830-9.png

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

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

word-image-28830-10.png

$ tree /tmp/destination

word-image-28830-11.png

Copying a Directory

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

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

word-image-28830-12.png

$ tree /tmp/destination

word-image-28830-13.png

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

word-image-28830-14.png

$ tree /tmp/destination

word-image-28830-15.png

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

word-image-28830-16.png

$ tree /tmp/destination

word-image-28830-17.png

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

word-image-28830-18.png

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

word-image-28830-19.png

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

word-image-28830-20.png

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

word-image-28830-21.png

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

word-image-28830-22.png

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

word-image-28830-23.png

$ tree /tmp/destination

word-image-28830-24.png

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

word-image-28830-25.png

$ tree /tmp/destination

word-image-28830-26.png

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

word-image-28830-27.png

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

word-image-28830-28.png

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.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...