Skip to content

Instantly share code, notes, and snippets.

@dehypnosis
Last active March 27, 2019 20:32
Show Gist options
  • Save dehypnosis/96e88e7c63e2a742af015f6fd5ce0dc0 to your computer and use it in GitHub Desktop.
Save dehypnosis/96e88e7c63e2a742af015f6fd5ce0dc0 to your computer and use it in GitHub Desktop.
bash functions for icloud nosync, resync
# icloud sync management shell functions
# Simple usage
$ nosync ./node_modules
$ resync ./some_dir ./and_dir ./and/deep/file_or_whatever
# example to call with pipe:
$ find ./test -type file | xargs -I{} zsh -c 'source ~/.zshrc && nosync {}'
$ ls -al test
total 0
drwxr-xr-x 8 dehypnosis staff 256 Mar 28 04:54 .
drwx------@ 10 dehypnosis staff 320 Mar 28 04:52 ..
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 .a.icloud.nosync
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 .b.icloud.nosync
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 .c.icloud.nosync
lrwxr-xr-x 1 dehypnosis staff 16 Mar 28 04:54 a -> .a.icloud.nosync
lrwxr-xr-x 1 dehypnosis staff 16 Mar 28 04:54 b -> .b.icloud.nosync
lrwxr-xr-x 1 dehypnosis staff 16 Mar 28 04:54 c -> .c.icloud.nosync
$ find ./test -type link | xargs -I{} zsh -c 'source ~/.zshrc && resync {}'
$ ls -al test
total 0
drwxr-xr-x 5 dehypnosis staff 160 Mar 28 04:58 .
drwx------@ 10 dehypnosis staff 320 Mar 28 04:54 ..
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 a
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 b
-rw-r--r-- 1 dehypnosis staff 0 Mar 28 04:54 c
# Advanced usage
$ find . -name node_modules | grep -v node_modules/ | xargs -I{} zsh -c 'source ~/.zshrc && nosync {}'
$ find . -name node_modules -type link | xargs -I{} zsh -c 'source ~/.zshrc && resync {}'
# bash functions
function nosync() {
FPATHS="$@"
for FPATH in $FPATHS
do
if [ ! -e $FPATH ]
then
echo "${FPATH}: no such file or directory"
continue
fi
FREAL=`realpath --no-symlinks $FPATH`
FDIR=${FREAL%/*}
FNAME=${FREAL##*/}
FNAME_NO=.${FNAME}.icloud.nosync
if [ -e $FDIR/$FNAME_NO ]
then
echo "${FPATH}: already linked to ${FNAME_NO}"
continue
fi
mv $FREAL $FDIR/$FNAME_NO
CWD=`pwd`
cd $FDIR
ln -s $FNAME_NO $FNAME && echo $FPATH
cd $CWD
done
}
function resync() {
FPATHS="$@"
for FPATH in $FPATHS
do
if [ ! -L $FPATH ]
then
echo "${FPATH}: no such symbolic link"
continue
fi
FREAL=`realpath --no-symlinks $FPATH`
FDIR=${FREAL%/*}
FNAME=${FREAL##*/}
FNAME_NO=.${FNAME}.icloud.nosync
if [ ! -e $FDIR/$FNAME_NO ]
then
echo "${FPATH}: not linked to ${FNAME_NO}"
continue
fi
rm $FREAL && mv $FDIR/$FNAME_NO $FREAL && echo $FPATH
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment