-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_dotfiles.sh
executable file
·52 lines (41 loc) · 1.35 KB
/
link_dotfiles.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# Define the source directory for your dotfiles
DOTFILES_DIR="$HOME/.dotfiles"
# Backup directory for existing dotfiles
BACKUP_DIR="$HOME/.dotfiles_backup/"
# Exclude list
EXCLUDE_FILES=("link_dotfiles.sh" "README.md" ".git")
# Function to check if a file or directory should be excluded
is_excluded() {
local relative_path="${1#$DOTFILES_DIR/}"
for excluded in "${EXCLUDE_FILES[@]}"; do
if [[ "$relative_path" == "$excluded" || "$relative_path" == "$excluded/"* ]]; then
return 0 # True (excluded)
fi
done
return 1 # False (not excluded)
}
# Create the backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Recursively process files in the .dotfiles directory
find "$DOTFILES_DIR" -type f | while read -r SOURCE; do
# Skip excluded files
if is_excluded "$SOURCE"; then
echo "Skipping excluded: $SOURCE"
continue
fi
# Determine target path in $HOME
RELATIVE_PATH="${SOURCE#$DOTFILES_DIR/}"
TARGET="$HOME/$RELATIVE_PATH"
# Ensure the target directory exists
mkdir -p "$(dirname "$TARGET")"
# Backup existing file if it exists
if [ -e "$TARGET" ] || [ -L "$TARGET" ]; then
echo "Backing up existing: $TARGET to $BACKUP_DIR"
mv "$TARGET" "$BACKUP_DIR"
fi
# Create the symlink
echo "Creating symlink: $TARGET -> $SOURCE"
ln -s "$SOURCE" "$TARGET"
done
echo "Symlinks created successfully!"