Unlock the full potential of AWK with these popular commands that will streamline your text processing tasks and increase your productivity. Table of Contents Introduction
In this article, we covered 20 useful rsync command examples that can help you transfer and synchronize files between multiple computers and devices quickly and efficiently.
Rsync is a powerful file synchronization and transfer tool that is widely used in the Linux and Unix-based operating systems. It was first released in 1996 by Andrew Tridgell and Paul Mackerras, and since then, it has been continually developed to provide a fast, efficient, and flexible method for copying and synchronizing files between different computers and devices.
In this article, we will cover 20 useful rsync commands that will help you to manage and transfer your files efficiently.
Photo by admingeek from Infotechys
Copy files from a local directory to a remote server:
rsync -avz /path/to/local/dir username@remote_host:/path/to/remote/dir
The -avz
options control the behavior of the rsync command.
-a
option stands for “archive mode” and preserves the permissions, ownership, timestamp, and other attributes of the files being transferred.-v
option stands for “verbose” and provides detailed output about the transfer process.-z
option stands for “compress” and compresses the data during transmission to save bandwidth.Copy files from a remote server to a local directory:
rsync -avz username@remote_host:/path/to/remote/dir /path/to/local/dir
This command copies the contents of the remote directory at /path/to/remote/dir
to the local directory at /path/to/local/dir
.
Copy files using a specific SSH port:
rsync -avz -e "ssh -p 2222" /path/to/local/dir username@remote_host:/path/to/remote/dir
We reviewed the -avz
options in the first example.
-e
“ssh -p 2222
“: option that specifies the remote shell to use for the transfer. In this case, it’s the SSH protocol on port 2222./path/to/local/dir
: the path of the local directory that is being transferred.username@remote_host:/path/to/remote/dir
: the remote directory where the files will be transferred. The “username” is the username on the remote host, and “remote_host” is the hostname or IP address of the remote host.Exclude specific files or directories from the transfer:
rsync -avz --exclude='*.log' /path/to/local/dir username@remote_host:/path/to/remote/dir
This command uses rsync to transfer files from a local directory to a remote directory while excluding any files with the .log extension.
Copy files in archive mode, preserving permissions, ownership, and timestamps:
rsync -avz --archive /path/to/local/dir username@remote_host:/path/to/remote/dir
This command copies the contents of /path/to/local/dir
to the remote server at remote_host
, while preserving permissions, ownership, and timestamps of the original files.
Copy only the files that have changed since the last transfer:
rsync -avz --update /path/to/local/dir username@remote_host:/path/to/remote/dir
This command copies only the files that have been modified or added since the last transfer, from /path/to/local/dir
to /path/to/remote/dir
.
Copy files and delete any files on the remote server that are not in the local directory:
rsync -avz --delete /path/to/local/dir username@remote_host:/path/to/remote/dir
This command copies the contents of /path/to/local/dir
to the remote server at remote_host
and deletes any files on the remote server that are not in the local directory.
Limit the bandwidth used for the transfer:
rsync -avz --bwlimit=1000 /path/to/local/dir username@remote_host:/path/to/remote/dir
This command limits the bandwidth of the transfer to 1000 kilobits per second, while copying the contents of /path/to/local/dir
to the remote server at remote_host
.
Show the progress of the transfer:
rsync -avz --progress /path/to/local/dir username@remote_host:/path/to/remote/dir
This command shows the progress of the transfer while copying the contents of /path/to/local/dir
to the remote server at remote_host
(also referred to as -P
).
Copy files and compress them during the transfer:
rsync -avz --compress /path/to/local/dir username@remote_host:/path/to/remote/dir
This command compresses the files during the transfer, while copying the contents of /path/to/local/dir
to the remote server at remote_host
.
Copy files and delete any empty directories on the remote server:
rsync -avz --delete-empty-dirs /path/to/local/dir username@remote_host:/path/to/remote/dir
This command deletes empty directories on the remote server during the transfer from the local directory to a remote server.
Copy files and exclude hidden files and directories:
rsync -avz --exclude='.*' /path/to/local/dir username@remote_host:/path/to/remote/dir
This command copies files from the local directory to a remote server, while excluding all hidden files and directories.
Copy files and preserve hard links:
rsync -avz --hard-links /path/to/local/dir username@remote_host:/path/to/remote/dir
This command preserves hard links during the transfer from the local directory to a remote server.
Copy files and preserve SELinux contexts:
rsync -avz -X -A -S /path/to/local/dir username@remote_host:/path/to/remote/dir
This command preserves SELinux contexts during the transfer from the local directory to a remote server.
Copy files and preserve ACLs:
rsync -avz -A /path/to/local/dir username@remote_host:/path/to/remote/dir
This command preserves ACLs during the transfer from the local directory to a remote server.
Copy files and use a different file checksum algorithm:
rsync -avz --checksum=sha256 /path/to/local/dir username@remote_host:/path/to/remote/dir
This command uses the SHA256 checksum algorithm to verify file integrity during the transfer from the local directory to a remote server.
Copy files and preserve extended attributes:
rsync -avz -X /path/to/local/dir username@remote_host:/path/to/remote/dir
This command preserves extended attributes during the transfer from the local directory to a remote server.
Copy files and exclude files based on their modification time:
rsync -avz --exclude-from=/path/to/exclude-file /path/to/local/dir username@remote_host:/path/to/remote/dir
This command excludes files from the transfer based on their modification time using the exclude-file
list.
Don’t update files that already exist:
rsync -avz --ignore-existing /path/to/local/dir username@remote_host:/path/to/remote/dir
The --ignore-existing
option that tells rsync to skip transferring files that already exist on the remote host. This can be useful when you only want to transfer files that are new or have been updated.
Preserve ownership, permissions, timestamps, symlinks, while displaying progress and verbose output during transfer:
rsync -Pavl /path/to/local/dir username@remote_host:/path/to/remote/dir
The command “rsync -Pavl /path/to/local/dir username@remote_host:/path/to/remote/dir
” will synchronize the files in the local directory “/path/to/local/dir
” with the remote directory “/path/to/remote/dir
” on the host “remote_host
“, preserving permissions, ownership, timestamps, and symbolic links, and displaying progress and verbose output during the transfer.
Each of these rsync commands offers a specific functionality that can be helpful in different situations. Knowing the right command to use can save time and effort in transferring files between local and remote servers.
Rsync is an incredibly versatile and powerful tool that can be used for a wide variety of file transfer and synchronization tasks. By learning these 20 useful rsync commands, you can take advantage of all of the features and functionality that this tool has to offer. Whether you need to transfer files between local and remote servers, synchronize files across multiple devices, or backup your important data, rsync is the perfect tool for the job. With these commands in your arsenal, you’ll be able to work with rsync more efficiently and effectively than ever before.
Related Posts
Unlock the full potential of AWK with these popular commands that will streamline your text processing tasks and increase your productivity. Table of Contents Introduction
In this article, we will review the top 10 most commonly used Git commands. You can’t call yourself a competent DevOps Engineer or IT professional
If you’re deciding between Linux and Windows for your next operating system, knowing the key differences between the two could save you time, money, and