How to copy files and folders using Rsync
Step-by-step intructions for using Rsync to copy files and folders.
On this page
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):
Then, use
realpath
to get the absolute path of our created files:
$ realpath Rsync.md
The output should be:
home/Ubuntu/Rsync.md
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
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/
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
:
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:
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: