How to Sync Directories on Android with Termux and Rsync

sync directories wide

Syncing directories on your Android device can be challenging, especially when dealing with multiple folders or remote servers. Fortunately, with the power of Termux and rsync, you can efficiently sync directories right from your terminal. Whether you’re backing up your files or keeping folders in sync, rsync provides a reliable solution for all your syncing needs.

Why Sync Directories with Rsync?

rsync is a versatile command-line tool designed to sync directories and files between different locations. If run as a daemon or periodically via crontab, it is a game changer. It’s highly efficient, supporting local-to-local, local-to-remote, and remote-to-local transfers. rsync offers incremental copying, compression, and the ability to preserve permissions, making it the go-to tool for syncing directories.

Getting Started with Termux and Rsync

To begin syncing directories on Android, you’ll first need to install rsync in Termux, a powerful terminal emulator. Here’s how to get started:

  1. Install Termux from the Google Play Store or F-Droid.
  2. Set Up Storage Access by running:bashCopy codetermux-setup-storage This command creates a directory in Termux that links to your device’s shared storage, typically at /storage/emulated/0/.
  3. Install Rsync by running:
pkg install rsync

Now you’re ready to sync directories on your Android device!

Syncing Directories Locally

Let’s start with syncing directories on your Android device. Suppose you have a directory /storage/emulated/0/SourceDir/ and want to sync it with /storage/emulated/0/DestinationDir/.

Here’s the command you’ll use:

$ rsync -avh /storage/emulated/0/SourceDir/ /storage/emulated/0/DestinationDir/

This command will sync directories by copying all files from SourceDir to DestinationDir while preserving permissions, timestamps, and more.

Syncing Directories with a Remote Server

One of the most powerful features of rsync is its ability to sync directories with a remote server. To do this, use the following command:

$ rsync -avh /storage/emulated/0/SourceDir/ user@remote.server:/path/to/destination/

Replace user and remote.server with your actual username and server address. This command will sync directories between your Android device and the remote server, keeping everything up to date.

Handling Symbolic Links

Sometimes, when you sync directories, you might find that rsync creates symbolic links in the target directory instead of copying the actual files. This happens if the source directory contains symbolic links. To ensure rsync copies the files these links point to, use the -L option:

$ rsync -avhL /storage/emulated/0/SourceDir/ /storage/emulated/0/DestinationDir/
This command will follow the symbolic links and copy the actual files, ensuring your directories are correctly synced.

Sync Directories with Deletion

If you want the destination directory to be an exact mirror of the source directory, including removing files that no longer exist in the source, add the --delete option:

$ rsync -avhL --delete /storage/emulated/0/SourceDir/ /storage/emulated/0/DestinationDir/
This command will sync directories and delete any files in the destination directory that are not present in the source directory.

Conclusion

With Termux and rsync, syncing directories on Android has never been easier. Whether you’re managing local folders or syncing with a remote server, rsync provides the flexibility and efficiency you need. Next time you need to sync directories on Android, Termux and rsync have you covered.

Leave a Reply

Your email address will not be published. Required fields are marked *