-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_symlinks
executable file
·35 lines (31 loc) · 1.03 KB
/
create_symlinks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/env zsh
dir=~/dotfiles
# List all files in target dotfiles directory (excluding .git, this script, and the readme)
function list_candidates () find $dir \( \
-path $dir/.git -o \
-path $dir/create_symlinks -o \
-path $dir/README.md \) \
-prune -o -type f -print \
#create any missing subdirectories
list_candidates | awk 'BEGIN{FS=OFS="/"}{NF--; print}' | sort | uniq | xargs mkdir -pv
# create any missing symlinks, prompting if file already exists
for df in $(list_candidates | sed -En "s/.*dotfiles\///p") ; do
# file does not exist: create link
if ! test -e ~/$df; then
echo CREATING SYMLINK: $HOME/$df -\> $dir/$df
ln -s $dir/$df ~/$df ;
# link already exists: skip
elif test -L ~/$df; then ;
# non-link file exists: prompt user to confirm overwrite
elif test -f ~/$df; then
echo Overwrite file ~/$df with a symlink into $dir\? \[y/N\]
read overwrite
if [ "$overwrite" = "y" ] ; then
rm ~/$df ;
ln -s $dir/$df ~/$df ;
fi
;
else
echo WARNING: unexpected file type: ~/$df
fi
done