Skip to content

Instantly share code, notes, and snippets.

@thekid
Last active December 21, 2015 09:39
Show Gist options
  • Select an option

  • Save thekid/6286530 to your computer and use it in GitHub Desktop.

Select an option

Save thekid/6286530 to your computer and use it in GitHub Desktop.
Sync directory with an ID / name combination
#!/bin/sh
dir=${1-test}
mkdir -p $dir
rm -f $dir/updated
while read input ; do
id=${input%=*}
name=${input#*=}
echo -n "#$id -> $name: "
if [ -e $dir/.$id ] ; then
current=$(readlink $dir/.$id)
if [ "$current" != "$name" ] ; then
echo -n "(renamed from $current): "
mv $dir/$current $dir/$name
ln -s -f $name $dir/.$id
fi
echo "OK"
else
echo "Creating..."
mkdir -p $dir/$name
ln -s $name $dir/.$id
fi
echo ".$id=$name" >> $dir/updated
done < test.map
# Find links not in updated file -> to be deleted
sort $dir/updated -o $dir/updated
find $dir -maxdepth 1 -type l -printf '%f=%l\n' | diff -u - $dir/updated | grep ^-.[0-9] | cut -d . -f2 > $dir/delete
while read input ; do
id=${input%=*}
name=${input#*=}
echo "#$id -> $name: Deleting..."
rm $dir/.$id
mv $dir/$name $dir/.$id.deleted
done < $dir/delete
# Clean up
rm $dir/updated $dir/delete
echo ""
echo "@ $dir"
ls -Al $dir
1549=timm
1552=alex
@thekid
Copy link
Author

thekid commented Aug 20, 2013

Now add a line 1777=dude:

$ sh sync.sh
#1549 -> timm: OK
#1552 -> alex: OK
#1777 -> dude: Creating...

@ test
total 3
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1549 -> timm
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1552 -> alex
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:07 .1777 -> dude
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 alex
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:07 dude
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 timm

@thekid
Copy link
Author

thekid commented Aug 20, 2013

Then modify the line to 1777=dudette:

$ sh sync.sh
#1549 -> timm: OK
#1552 -> alex: OK
#1777 -> dudette: (renamed from dude): OK

@ test
total 3
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1549 -> timm
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1552 -> alex
lrwxrwxrwx  1 Timm Friebe None 7 Aug 20 22:08 .1777 -> dudette
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 alex
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:07 dudette
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 timm

@thekid
Copy link
Author

thekid commented Aug 20, 2013

Finally, remove the line with 1777 in it again:

$ sh sync.sh
#1549 -> timm: OK
#1552 -> alex: OK
#1777 -> dudette: Deleting...

@ test
total 2
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1549 -> timm
lrwxrwxrwx  1 Timm Friebe None 4 Aug 20 22:06 .1552 -> alex
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:07 .1777.deleted
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 alex
drwxr-xr-x+ 1 Timm Friebe None 0 Aug 20 22:06 timm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment