Rsync all files linux

How to use rsync to copy files

Rsync is a command-line tool in Linux that is used to copy files from a source location to a destination location. You can copy files, directories, and entire file system and keep in sync the files between different directories. It does more than just copying the files. It reduces the amount of data sent over the network by only sending the files which are new or updated. That’s why is it considered a great utility for file copying and backing up. Rsync also supports the copying of files to a remote system over SSH.

In this article, we will explain how to use rsync to copy files with some practical examples. We will also explain the use of rsync in the copying of a single file, multiple files, and directory to both local and remote systems. We will be covering how to use Rsync to:

  • Copy file/directory within the local system
  • Copy file/directory from local to the remote system
  • Copy file/directory from the remote system to local

We have explained the commands and procedure mentioned in this article on a Ubuntu 20.04 LTS. You will need the command-line Terminal for copying files using rsync. To open the command line Terminal application, use the Ctrl+Alt+T keyboard shortcut.

Installing Rsync

Rsync comes pre-installed in Ubuntu 20.04 LTS. However, if it is accidentally removed from the system, you can install it as follows:

Читайте также:  Check file encode linux

Here are some of the commonly used options that can be used with rsync:

–a: archive mode
–v: shows details of the copying process
–p: shows the progress bar
–r: copies data recursively
–z: compresses data
–q: suppress output

Copy files and directories locally

The general syntax of rsync is:

Copy a single file locally

If you want to copy a file from one location to another within your system, you can do so by typing rsync followed by the source file name and the destination directory.

For instance, to copy a single file “file1.txt” from the current location that is Home directory to ~/Documents directory, the command would be:

Note: Instead of “/home/tin/file1.txt”, we can also type “file1” as we are currently working in the home directory. Also, we can use ~/Documents instead of mentioning the full path as “/home/tin/Documents”.

Copy multiple files locally

If you want to copy multiple files at once from one location to another within your system, you can do so by typing rsync followed by source files name and the destination directory.

For instance, to copy the files “file2.txt” and “file3.txt” from the Home directory to ~/Documents directory, the command would be:

To copy all the files with the same extension, you can use asterisk(*) symbol instead of individually specifying the file names. For instance, to copy all the files from the Home directory ending with “.zip” extension to the ~/Documents directory, we will use:

Copy directories locally

If you want to copy a directory with its sub-directory and all contents from one location to another within your system, you can do so as by typing rsync followed by the source and destination directory.

Читайте также:  Как поставить винду с линукса

For instance, to copy the “sample” directory to “test/” directory within the Home directory, the command would be:

Note: Specifying “/” after the source directory only copies the contents of the directory. If we do not specify the “/”after the source directory, the source directory will also be copied to the destination directory.

Like in the following output, you can see that our source directory “sample” is also copied to the destination directory (as we have used “sample” instead of “sample/” as a source directory).

Rsync supports incremental transfer as you can see in the above output that it only transfers the files which are new or updated.

Copy files based on maximum size

While copying, we can also specify the maximum size of files that can be copied with the “–max-size” option. For instance, to copy the files no larger than 2000k from ~/Downloads to ~/Documents directory, the command would be:

This command will copy all the files from ~/Downloads to ~/Documents directory except the ones larger than 2000k.

Copy files based on minimum size

Similarly, we can also specify the minimum size of files that can be copied with the “–min-size” option. For instance, to copy the files not lesser than 5M from ~/Downloads to ~/Documents directory, the command would be:

This command will copy all the files from ~/Downloads to the ~/Documents directory except the ones lesser than 5M.

Exclude files

While copying files from one directory to another, you can exclude some files using “—exclude” option followed by the file name or the extension of the file type.

Читайте также:  Get motherboard info linux

For instance, to exclude all the .zip files while copying files from ~/Downloads to ~/Documents directory, the command would be:

Copy files and directories remotely

With Rsync, you can copy a single file, multiple files, and directories to a remote system. For copying files and directories remotely, you will require:

  • Rsync installed on both local and remote system
  • SSH access to the remote system
  • Remote user password

Before copying files using rsync, make sure you can access the remote system via SSH:

The general syntax for copying files and directories to a remote system using rsync:

For instance, to copy a single file “file.txt” from ~/Downloads to remote system ~/Documents directory, the command would be:

Similarly, to copy the “~/Downloads/files” directory in a local system with its sub-directory and all contents to the “~/Downloads/samples” directory at the remote system:

You can also copy the files and directories from the remote machine to your local machine. For instance, to copy a “file2.txt” from the remote system to the local system desktop:

Similarly, to copy a directory from the remote system to the local system

You can also use all the options like –max-size, –min-size, –exclude and others while copying files to/from the remote system.

Rsync is a great tool for copying files from source to destination either locally or remotely. It includes a large set of options that gives you flexibility and control while transferring files to/from the remote system.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Оцените статью
Adblock
detector