29
submitted 2 days ago* (last edited 1 day ago) by sartaj@lemmy.world to c/linux@lemmy.ml

I have a laptop and a phone in which termux is installed. I want to move files from my phone to my computer using rsync if all files are older than 30 days. I think I should use find command with -exec rsync.

find . -mtime +30 -exec rsync something 

but the problem is my phone's IP address is always changing so how do I transfer file? I have to run this script in my laptop

solution

i wrote a script which move files and runs in my laptop

  1. i can't use hostname because I am on android 9 and don't want to install anything else.
  2. most of the time I am offline so using any internet service is not a good idea ( my devices are connected in themselves )

here is my script

#!/usr/bin/bash

#checking if script not already running and exit if already running
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

set -e

TARGET_MAC="d2:1c:3c:3d:6a:a7"
RSYNC_LOG="$HOME/log/rsync_log.txt"  # Rclone log file


function get_ip() {
  # need to edit sudoers file to run 	sudo nmap -sn 192.168.43.0/24 without password
  # Allow members of group sudo to execute any command
  # %sudo	ALL=(ALL:ALL) ALL
  # 'username' ALL=(ALL) NOPASSWD: /usr/bin/nmap -sn 192.168.43.0/24


	sudo nmap -sn 192.168.43.0/24 | grep --before-context=3 -i "$TARGET_MAC" | tail -n 3 | head -n 1 | awk '{ print $6 }' | sed 's/[()]//g'; }


# run command only if wifi is connected
if echo $(nmcli general status) | grep -q -w "connected" ; then

  # for ssh to work in a changing IP address, a configuration line has to be enabled so that it does not ask for yes/no while connecting to a changed ip address
	TARGET_IP=$(get_ip)
  ssh -p 8022  "$TARGET_IP" 'find /data/data/com.termux/files/home/storage/shared/Pictures/ -mtime +30  -type d,f -printf %P\\0' | rsync --append --update --times --atimes --mkpath --verbose --human-readable --remove-source-files --log-file="$HOME/log/synced.log" --exclude='.dtrash' --files-from=- --from0  -e "ssh -p 8022" "$TARGET_IP":/data/data/com.termux/files/home/storage/shared/Pictures/ $HOME/Pictures
top 15 comments
sorted by: hot top controversial new old
[-] catloaf@lemm.ee 17 points 2 days ago

DNS was invented in 1985 to solve this problem.

You could also run it from the phone.

[-] sartaj@lemmy.world 1 points 1 day ago

i don't know how to use DNS

[-] catloaf@lemm.ee 2 points 1 day ago

So you'd rather reinvent TCP/IP from scratch?

[-] sartaj@lemmy.world 1 points 1 day ago

it satisfies my use case

[-] truthfultemporarily@feddit.org 13 points 2 days ago

Use the hostname of your phone instead.

[-] sartaj@lemmy.world 0 points 1 day ago

I am on android 9

[-] tasankovasara@sopuli.xyz 1 points 1 day ago

Kudos, I think that is a great DIY solution!

[-] Xanza@lemm.ee 5 points 2 days ago* (last edited 2 days ago)

VPN.. This is a specific problem that VPNs were created to solve.

On your Wireguard network (or whichever), note your vLAN IPs, and configure rclone/rsync with them like normal. Ensure you're connected to Wireguard and then run your command;

❯ fd --change-older-than 30days -X rclone sync phone:my/path/here pc:destination/path/here

Using a VPN ensures that regardless of WiFi/Cellular connections you'll still be able to transfer at home, or remotely. Using methods like hostnames only work locally.

[-] sartaj@lemmy.world 1 points 1 day ago

i use my phone's hotspot to connect to laptop

[-] Xanza@lemm.ee 1 points 1 day ago

Seems dubious, and will only work locally..

[-] RedSnt@feddit.dk 4 points 2 days ago

Yeah, like @catloaf@lemm.ee said, Dynamic DNS ought to fix this issue. There are plenty of free ones. I currently use ClouDNS, but No-IP is also decent.
Hardest part is updating from a phone, but there's articles on both sites for how to do that.

[-] gonzo028@sopuli.xyz 3 points 2 days ago* (last edited 2 days ago)

I use syncopoli to rsync files from my phone to the computer. Perhaps this helps.

https://f-droid.org/packages/org.amoradi.syncopoli

[-] sartaj@lemmy.world 3 points 1 day ago

i dont want to install another app and tweak battey saver setting, auto start managent etc. termux is already set up

[-] BaumGeist@lemmy.ml 1 points 2 days ago

Is your phone running a GNU distro or rooted Android, or is it just regular Android with a Termux chroot?

[-] sartaj@lemmy.world 1 points 1 day ago

I am on native android

this post was submitted on 14 May 2025
29 points (100.0% liked)

Linux

54184 readers
505 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS