Last active
December 21, 2015 09:39
-
-
Save thekid/6286530 to your computer and use it in GitHub Desktop.
Sync directory with an ID / name combination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1549=timm | |
| 1552=alex |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finally, remove the line with
1777in it again: