How to copy files and folders using Rsync

Step-by-step intructions for using Rsync to copy files and folders.

Copy file (local to local)

On a local machine, use Rsync to copy a file from one location to another.

Let's create some files in our home directory:

$ touch Rsync.md Rsync1.md Rsync2.md

The touch command will create the 3 listed files. To confirm, use ls:

$ ls -l

The output should list all the 3 files created in the home directory (along with other foldes we may have):

Rsync copy file local to local, Step 1, touch and ls commands

Then, use realpath to get the absolute path of our created files:

$ realpath Rsync.md

The output should be:

home/Ubuntu/Rsync.md

Rsync copy file local to local, Step 2, realpath command

Let's copy the Rsync.md file over to our Documents/ folder:

$ rsync /home/ubuntu/Rsync.md /home/ubuntu/Documents/

Navigate to Documents/ folder to confirm that the new file has been copied:

$ cd Documents/

Use ls to confirm that Rsync copied the files over to the Documents/ folder:

$ ls

Rsync copy file local to local, Step 3, file copied

Copy multiple files (local to local)

It's the same process if you want to copy multiple files:

$ rsync Rsync1.md Rsync2.md /home/ubuntu/Documents/

Rsync copy multiple files local to local

Copy directory (local to local)

Copying a directory is the same as copying a file.

Let's create a new directory:

$ mkdir directory2
$ cd directory2

Create some files inside this new created directory:

$ touch Rsync{1..200}.md

These 200 files are now created inside the new directory directory2:

Rsync copy folder local to local, step 1, create files

Let's go to the destination folder, Downloads/, where we want to copy our new directory directory2:

$ cd Downloads
$ ls -l

The Downloads/ folder should be empty:

Rsync copy folder local to local, step 2, go to destination folder

Now let's copy the directory2/ folder over to Downloads/ folder:

$ rsync -r /home/ubuntu/directory2 /home/ubuntu/Downloads/

The folder is now copied with all its files:

Rsync copy folder local to local, step 3, rsync done