I've made a manager for my dot files that creates sym links between each of the dot files in my dot_files/ dir and my $HOME.
I'm trying to write a script that deletes all of these specific sym links and no others.
Here's my O(n^2) solution that does the job fine.
delete_sym_links () {
# Include dot (.) files while looping
shopt -s dotglob
for DOT_FILE in ~/dot_files/*;
do
if [ ! -d "$DOT_FILE" ];
then
for FILE in ~/*;
do
if [ -h "$FILE" ] && [ $(basename $DOT_FILE) = $(basename $FILE) ];
then
rm "$FILE"
echo deleted "$FILE"
fi
done
fi
done
}
I'm trying to get the runtime down to O(n lg n). The bash is really tripping me up though.
Something like...
delete_sym_links () {
SYM_LINKS=($(find $HOME -maxdepth 1 -type l -ls | sort -n))
NUM_SYM_LINKS=$(find $HOME -maxdepth 1 -type l -ls | wc -l)
DOT_FILES=$(find $HOME/dot_files/ -maxdepth 1 -name ".*" -type f | sort -n)
NUM_DOT_FILES=$(find $HOME/dot_files/ -maxdepth 1 -name ".*" -type f | wc -l)
i=0
j=0
while (("$i" < "$NUM_SYM_LINKS")) && (("$j" < "$NUM_DOT_FILES"));
do
if [ $(basename ${SYM_LINKS[$i]}) = $(basename ${DOT_FILES[$j]}) ];
then
echo removing sym link ${SYM_LINKS[$i]}
rm ${SYM_LINKS[$i]}
((j++))
else
((i++))
fi
done
echo "All dot_files sym links removed"
}