How to Copy Directories in Linux: SCP, Rsync, and CP Commands

Need to copy a directory from one Linux system to another? With tips and tricks for getting the job done quickly and efficiently, this guide will show you how to use the SCP, Rsync, and CP commands.

Brien Posey

May 15, 2023

7 Min Read
Linux with binary code background
Alamy


Table of Contents

What Is the Linux File System?


The Linux file system is a system for storing files and folders on a Linux machine. Like other file systems, the Linux file system is applied to physical or virtual disks and serves as an organized system for file storage.

Generally, a file system is a mechanism that sets rules for file storage. For example, the file system determines the maximum length of a filename, the characters that can be included in a filename, the maximum folder depth, and more.

How Do You Copy a Directory in Linux?

There are several ways to copy files and directories in Linux. Below are three commonly used options: the CP command, the SCP command, and the Rsync command.

1. How to Copy a Directory in Linux With the CP Command

The Linux CP command (Copy command) is probably the best-known utility for copying files. It simply requires you to enter the source and destination paths. If your goal is to copy an entire directory, you would provide the source directory and the destination directory, along with the -R (recursive) parameter. This means that all the files and subdirectories within the source directory will be copied to the destination directory.

Related:Free Guide: Get Started with Ubuntu

Here is what such an operation might look like:

Cp -R Source_Directory Destination_Directory

In this example, Source_Directory and Destination_Directory are placeholders for the names of the actual source and destination directories. To execute this operation, you'd need to replace Source_Directory with the actual directory you want to copy from and Destination_Directory with the location where you want to paste the copied directory.

For instance, if you want to copy a directory named Documents to a directory named Backup, the command might look like this:

Cp -R /home/user/Documents /media/external_drive/Backup

In this example, /home/user/Documents represents the source directory, and /media/external_drive/Backup represents the destination directory.

The CP command in Linux can be also used to copy individual files by specifying a filename instead of a directory name. In such cases, the operation would not require the -R parameter.

For example, to copy a file named filename.ext from your Documents directory to your Backup directory, you would use the following CP command:

Cp /home/user/Documents/filename.ext /media/external_drive/Backup

Whether you need to copy a single file or an entire directory, the CP command provides a straightforward, flexible, and user-friendly solution to efficiently get the job done.

2. How to Copy a Directory in Linux With the SCP Command

The Linux SCP command (Secure Copy Protocol command) is used for copying data between your local system and a remote system. Data can flow in either direction, meaning that you can copy a directory from a remote system to your local system or you can copy a directory from your local system to a remote system.

The SCP command is straightforward to use. If you want to copy files from a remote system, you can use a command like this:

Scp username@remotesystem:filename.ext /local/location

In this example, username@remotesystem represents a user account on the system from which you are copying data. Filename.ext is a placeholder for the name of the file that you want to copy. If you want to copy an entire directory, list the directory name instead. /location/local represents a local directory name where the files are to be copied.

If your goal is to copy a file to the remote system, the SCP command in Linux is essentially reversed. The command would look something like this:

Scp filename.ext username@remotesystem:/remote/location

3. How to Copy a Directory in Linux With the Rsync Command

The Linux Rsync command (Remote Sync command) can be used to copy files and directories, but it is primarily used for performing backups or migrations.

At its simplest, you need only to supply the source and destination path and one or more parameters. For example, the -a switch is commonly used because it preserves file ownership and permission information.


To copy a file using Rsync, you would use a command like this:

Rsync -a /source/path/filename.ext /destination/path

In this example, /source/path represents the file’s current location, and /destination/path represents the location that the file is copied to. Filename.ext represents the name of the file to be copied.

The Rsync command in Linux can be used to copy entire directories by supplying the directory name rather than a filename.

comparison chart of Linux File and Directory Copy Commands: CP, SCP, RSYNC

What Are Some Other Commands That Can Assist in the Copy Process?

Here are five commands that can help you in the copy process.

1. Find Command

The Find command can be used to look for a specific file. For example, if you want to see if a file named Readme.txt existed in a folder called Temp, you would use this command:

Find ./Temp readme.txt

2. Tree Command

The tree command is used to view files and subfolders within a specific folder. Additionally, the tree command will display the files residing in each subfolder.

To see a tree view of the contents of a folder named Temp, you would enter:

Tree -a ./temp

The tree utility is not installed by default typically, so you will need to install it by using the Install Tree command or the Apt Install Tree command depending on the Linux distribution you use.

3. Ls Command

The ls command is used to list the files in the current directory. Numerous parameters can be used in conjunction with the ls command as a way of filtering the command’s output. However, the ls command can also be used by itself without any parameters.

One of the most useful parameters is -a, which causes hidden files to be displayed in the output. Here is what the command would look like:

Ls -a

4. MV Command

The mv command can be used to rename or move a file or folder. To use it, you need only to specify the source and destination. For example, if you wanted to rename readme.txt to read.txt, you would use this command:

Mv readme.txt read.txt


5. RM Command

The rm command deletes a specified file or folder. To use this command, you need only to specify the name of the file or directory that you want to remove. For example, if you wanted to delete the readme.txt file, you would type:

Rm readme.txt

FAQs

Here are answers to frequently asked questions about copying directories in Linux.

What Is Linux?

Linux is a UNIX-like, open source operating system. Unlike Windows, a competing operating system from Microsoft, numerous different vendors offer Linux distributions. Another key difference between Windows and Linux is that many Linux distributions can be licensed free of cost.

What Is a Directory?

A directory is just another name for a file system folder. It is essentially just a container within the file system that can be used to store files that are somehow related to one another (such as a collection of documents or a group of files that make up an application). Directories can also store other directories, which are known as subdirectories.

How Do You Copy Hidden Files in Linux?

If you want to copy hidden files in Linux, use the CP command as you normally would. The trick to making the CP command copy hidden files is to append the -a switch.

What Are Some Tips for Copying Directories in Linux?

Aside from what we have already covered, here are a couple of additional considerations for copying files and directories in Linux.

First, be careful when copying anything that is owned by Root, because you can cause problems if you accidentally overwrite a critical file. If you do need to copy a file or folder that is owned by Root, you will need to use the Sudo command.

Second, note the amount of data that needs to be copied. If the copy process involves large amounts of data, you might consider using the TAR command, as it sometimes copies data more quickly than other commands.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like