Skip to content

Instantly share code, notes, and snippets.

@MatMercer
Last active March 31, 2018 21:54
Show Gist options
  • Save MatMercer/51e2ed507354f80ac3c91f569bb004cb to your computer and use it in GitHub Desktop.
Save MatMercer/51e2ed507354f80ac3c91f569bb004cb to your computer and use it in GitHub Desktop.
Syncig musics files to a Android phone using a command line.

Syncing music files to a Android phone

I use Simple MTPFS to access the Android phone and rsync to sync the music files.

Demo Usage

./mount_android.sh ~/Documents/temp/android     
Trying to mount android to '/home/mat/Documents/temp/android'.
Could not retrieve device storage.
For android phones make sure the screen is unlocked.
Error, can't connect to android. Is it connected and you granted permission?
The security prompt only shows up in the first time, unlock your phone and grant permission.
After granting permission, re-run this script with same parameters.

[ Unlock phone and grant permission ]

./mount_android.sh ~/Documents/temp/android
Trying to mount android to '/home/mat/Documents/temp/android'.
Success. Android connected.

./sync.sh ~/Music ~/Documents/temp/android/Music
sending incremental file list [ rsync ]
...

./umount_android.sh ~/Documents/temp/android    
Trying to umount android from '/home/mat/Documents/temp/android'
Android umounted!

Profit!

Thanks Björn Bidar for giving hints about best practices in shell script programming.

#!/bin/bash
#########################################
# Script to mount a android phone #
# using simple-mtpfs. #
# By Mateus Mercer. 2018 #
# <mateusmercer@gmail.com> #
#########################################
## Executable name
ME=$(basename "$0")
## Parameters validation
if [ $# -eq 0 ]
then
echo >&2 "Please specify the mount point"
echo >&2 "$ME [mount-path]"
exit 1
fi
## Get mountpath
ANDRODIR=$1
echo >&2 "Trying to mount android to '$ANDRODIR'."
## Creates mount point dir, ignoring if it already exists
mkdir $ANDRODIR 2> /dev/null
## Command not found error
if ! command -v simple-mtpfs > /dev/null 2>&1; then
echo >&2 "Error, simple-mtpfs command not found."
echo >&2 "Please install it using AUR helpers for arch."
echo >&2 "Or by going to this link: https://github.com/phatina/simple-mtpfs"
exit 2
fi
## Mount the android
simple-mtpfs $ANDRODIR
## Can't connect to android
if [ $? -eq 1 ]; then
echo >&2 "Error, can't connect to android. Is it connected and you granted permission?"
echo >&2 "The security prompt only shows up in the first time, unlock your phone and grant permission."
echo >&2 "After granting permission, re-run this script with same parameters."
exit 3
fi
echo >&2 "Success. Android connected."
#!/bin/bash
#########################################
# Script to sync musics #
# using rsync. #
# By Mateus Mercer. 2018 #
# <mateusmercer@gmail.com> #
#########################################
## Executable name
ME=`basename "$0"`
## Parameters validation
if [ $# -lt 2 ]
then
echo >&2 "Please specify the src and dest directory"
echo >&2 "$ME [src-dir] [dest-dir]"
exit 1
fi
## Get the parameters
SRC=$1
DEST=$2
## Sync
rsync --verbose --progress --recursive --inplace --no-perms --no-owner --no-group --ignore-times $SRC $DEST
#!/bin/bash
#########################################
# Script to umount a android phone#
# using fusermount. #
# By Mateus Mercer. 2018 #
# <mateusmercer@gmail.com> #
#########################################
## Executable name
ME=`basename "$0"`
## Parameters validation
if [ $# -eq 0 ]
then
echo >&2 "Please specify the mount point"
echo >&2 "$ME [mount-path]"
exit 1
fi
## Get mountpath
ANDRODIR=$1
echo >&2 "Trying to umount android from '$ANDRODIR'"
## Umount android dir using fuser
fusermount -u $ANDRODIR
if [ $? -eq 1 ]; then
echo >&2 "Error, can't umount android. Is it disconnected or not mounted?"
exit 2
fi
echo >&2 "Android umounted!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment