Keeping files in sync across phones and tablets can feel like a juggling act. If you use Termux, you already have a powerful Linux toolkit in your pocket. In this post I’ll walk you through ten practical scripts you can run from Termux to sync files between Android devices. Each script is small, clear, and focused on a single use case so you can pick the one that fits your workflow and scale it up.
These scripts assume you are comfortable with basic Termux commands. If you are just getting started, check my guide on installing Termux and the essentials. If you care about privacy while syncing over networks, read up on using a VPN before transferring sensitive files. Also, if you work in a small business or manage devices, pair these approaches with a proper cybersecurity plan and network hardening. See resources on incident response, secure remote access, and network security linked throughout.
Prerequisites
- Termux installed and updated. (See how to install Termux.)
- Packages: openssh, rsync, inotify-tools, git, rclone, unison. Install with
pkg install openssh rsync inotify-tools git rclone unison
. - SSH keys for passwordless authentication when using SSH-based methods. See guides on securing remote access in my network security tips post.
- Optional: Syncthing app for Android for peer-to-peer syncing, or a cloud account (Google Drive, Nextcloud) configured with rclone. My VPN and cloud posts may help if you need private tunnels or third party services: Surfshark VPN review and VPNS to use when using Termux.
How I’ll present each script
Script name, short explanation, the script itself, and a brief note on when to use it. All scripts are ready to paste into a Termux session. Replace placeholders like user@host
and /sdcard/MySync
with your values.
1) Simple rsync over SSH (one-shot)
Use this to push a folder from Device A to Device B quickly. Fast, secure, and ideal for LAN or over a VPN.
#!/data/data/com.termux/files/usr/bin/bash
SRC="/sdcard/MySync/"
DEST="user@192.168.1.10:/storage/emulated/0/MySyncBackup/"
rsync -avz --delete -e "ssh -p 2222" "$SRC" "$DEST"
When to use: manual push when you have SSH access and want a reliable mirror.
2) Pull-only rsync (device B pulls from A)
If Device B can reach Device A but not vice versa, run this on Device B to pull changes.
#!/data/data/com.termux/files/usr/bin/bash
SRC="user@192.168.1.5:/sdcard/MySync/"
DEST="/sdcard/MySync/"
rsync -avz -e "ssh -p 2222" "$SRC" "$DEST"
When to use: periodic pull by a central device that aggregates files from several phones.
3) Watch-and-sync with inotify + rsync (near real-time)
Use inotifywait to trigger rsync when files change. Good for near real-time sync on LAN.
#!/data/data/com.termux/files/usr/bin/bash
WATCH_DIR="/sdcard/MySync"
REMOTE="user@192.168.1.10:/storage/emulated/0/MySyncBackup/"
inotifywait -m -r -e close_write,move,create,delete "$WATCH_DIR" | while read path action file; do
rsync -avz --delete -e "ssh -p 2222" "$WATCH_DIR/" "$REMOTE"
done
When to use: you want changes to propagate immediately while on the same network.
4) One-file sync via scp (quick single file)
Fast and small: copy a single file to another device.
#!/data/data/com.termux/files/usr/bin/bash
FILE="/sdcard/MySync/photo.jpg"
DEST="user@192.168.1.10:/storage/emulated/0/MySyncPhotos/"
scp -P 2222 "$FILE" "$DEST"
When to use: quick transfer of a photo or document.
5) Git-backed notes sync (text files)
Use Git for plain text notes or small config files. Works offline and merges changes.
#!/data/data/com.termux/files/usr/bin/bash
REPO_DIR="/sdcard/MyNotes"
cd "$REPO_DIR"
git add -A
git commit -m "auto-sync: $(date +'%Y-%m-%d %H:%M:%S')" || true
git push origin main
When to use: you keep notes or dotfiles and want version history and conflict resolution.
6) rclone sync to Google Drive or Nextcloud
Use rclone when you want cloud-backed sync. rclone supports Google Drive, Nextcloud, Dropbox, and more.
#!/data/data/com.termux/files/usr/bin/bash
LOCAL="/sdcard/MySync/"
REMOTE="gdrive:MySyncBackup"
rclone sync "$LOCAL" "$REMOTE" --drive-chunk-size 64M
When to use: you need remote backup and cross-platform access. Pair with a VPN and strong credentials. See my cloud and security posts for guidance.
7) Bi-directional sync with Unison
Unison gives proper two-way sync and handles conflicts better than raw rsync loops.
#!/data/data/com.termux/files/usr/bin/bash
DIR="/sdcard/MySync"
REMOTE="ssh://user@192.168.1.10//storage/emulated/0/MySync"
unison "$DIR" "$REMOTE" -auto -batch
When to use: you edit files on both devices and need a sensible merge tool.
8) Simple peer discovery and rsync loop
If you often move between networks, use a small loop that tries known peers and rsyncs when reachable.
#!/data/data/com.termux/files/usr/bin/bash
PEERS=("192.168.1.10" "192.168.8.5")
SRC="/sdcard/MySync/"
for P in "${PEERS[@]}"; do
if ping -c1 -W1 "$P" >/dev/null 2>&1; then
rsync -avz --delete -e "ssh -p 2222" "$SRC" "user@$P:/storage/emulated/0/MySyncBackup/"
break
fi
done
When to use: you have a small set of known devices and want automatic sync when one is nearby.
9) Sync via Syncthing CLI start + remote API trigger
Syncthing is a robust peer-to-peer sync solution. Install Syncthing for Android alongside Termux, and control it from Termux scripts to start/stop or trigger scans.
#!/data/data/com.termux/files/usr/bin/bash
# trigger a full rescan via Syncthing REST API
API_KEY="YourApiKey"
curl -X POST -H "X-API-Key: $API_KEY" http://127.0.0.1:8384/rest/system/rescan
When to use: you prefer a managed peer-to-peer solution that handles discovery and conflict resolution. Run Syncthing as the service and use Termux for automation. If you want a full install guide, check my quick projects and syncthing-related notes.
10) Encrypted backup sync using gpg + rclone
For sensitive data, encrypt before sending to cloud or remote device.
#!/data/data/com.termux/files/usr/bin/bash
SRC="/sdcard/MySync"
TMP="/data/data/com.termux/files/home/tmp_sync.tar.gz"
gpg --symmetric --cipher-algo AES256 --output "$TMP.gpg" <(tar -czf - -C "$SRC" .)
rclone copy "$TMP.gpg" remote:Backups/
rm -f "$TMP.gpg"
When to use: you need confidentiality before sending to cloud providers or shared devices. Pair this with a secure password manager and VPN guidance from my posts.
Security and operational tips
- Use SSH keys and avoid passwords for automated scripts. Protect private keys with a strong passphrase and Termux file permissions.
- Consider running rsync over an SSH tunnel or over a VPN when on public networks. My VPN posts explain options and why a VPN helps when syncing sensitive files.
- Limit what you sync. Avoid syncing app private data or system directories. Back up only the folders you explicitly want.
- Test scripts with a small folder first. Add
--dry-run
to rsync to see what would change. - Log activity. Add simple logging like
>> /data/data/com.termux/files/home/sync.log 2>&1
so you can audit runs.
Where these fit into a bigger security posture
If you manage multiple devices for work or keep business files on Android, think of syncing as part of your security plan, not just a convenience feature. Combine these scripts with the network security and incident response practices I write about. If you are running automated sync in a business context, pair it with an incident response plan and regular backups. For more on building a security mindset around tools, see the posts on cyber security plan for small business, best cyber incident response companies, and NIS2 guidance.
Common pain points and links to quick fixes
- Connectivity issues: Check the device IPs and network. If you use mobile hotspots, make sure routing allows device-to-device traffic. My quick Termux projects guide has troubleshooting tips: Quick Termux projects.
- Permission errors accessing
/sdcard
: Ensure you granted Termux storage access and use the correct paths. See the Termux install guide for storage steps. - Security concerns when using public Wi-Fi: Use a VPN and avoid plain SSH passwords. See the Surfshark review and VPN recommendations linked above.
- Conflicts and data loss: Use Unison or Syncthing for two-way sync to reduce conflicts. For strict mirrors, use rsync with care and test with
--dry-run
.
Wrapping up
Pick the script that matches your needs: rsync for simple mirroring, Unison for two-way merges, rclone for cloud backups, Git for text files, and Syncthing for a managed peer network. Combine these tools with secure SSH keys, a VPN when on public networks, and an appropriate backup cadence. If you want, I can convert any of these scripts into a single install-and-configure script that handles package installation, key creation, and a default config so you can get started with one command.
If you found this useful, check related guides I’ve written on Termux tools, network security, and ethical awareness. For a quick read on the security side of IoT and connected devices, see Can hackers control self-driving cars. If you tinker with phishing education or simulation tools in a lab, you might want to review safe, ethical practices first, like the post about MaxPhisher in Termux. Links to both are here: MaxPhisher in Termux.
Start small, test, and secure. Syncing is powerful, but only as safe as your setup. Happy syncing.
Top comments (0)