Skip to content

Instantly share code, notes, and snippets.

@pjhades
Last active May 13, 2016 02:30
Show Gist options
  • Save pjhades/6877b9f8e8091a9be2c572f48a52055a to your computer and use it in GitHub Desktop.
Save pjhades/6877b9f8e8091a9be2c572f48a52055a to your computer and use it in GitHub Desktop.
Move apps to SD card storage and create symbolic links at original positions pointing to the new paths. HAVE NOT TRIED, USE WITH CARE.
#!/usr/bin/env bash
name=$(basename $0)
usage="link files/directories to destination.\n\
usage: $name [-i|-p] <src> <abs dst dir>\n\
-i\task before overwriting\n\
-p\tpreview operations before executing\n"
confirm=''
preview=0
while getopts ":ip" opt; do
case $opt in
i)
confirm='-i'
;;
p)
preview=1
;;
\?)
echo "$name: bad option -$OPTARG"
echo -e $usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 2 ]; then
echo -e $usage
exit 1
fi
src=$1
dst=$2
! [ -e $src ] && { echo "$name: $src does not exist"; exit 1; }
! [ -e $dst ] && { echo "$name: $dst does not exist"; exit 1; }
if ! [ -d $dst ]; then
echo "$name: ${dst} is not a directory"
exit 1
fi
target="${dst%/}/$(basename $src)"
# warn on non-absolute destination paths
if [ ${target:0:1} != '/' ]; then
echo "$name: warning: non-absolute destination paths may cause broken links"
if [ ${target:0:1} == '.' ]; then
target=${target:2}
fi
target="$(pwd)/$target"
fi
if [ $preview -eq 1 ]; then
echo "mv $confirm $src $target"
echo "ln $confirm -s $target $src"
else
mv $confirm $src $target
ln $confirm -s $target $src
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment