GNU Stow does not run properly if the target files already exist. So, i have written a script which will first delete all files and links in the target then run Stow.
#!/bin/bash
fd --hidden --ignore-file .gitignore --base-directory="$HOME/.dotfiles/.common-dotfiles" --type l --type f | sd ^. $HOME | xargs -I{} rm {}
stow --no-folding --target="$HOME" --dir="$HOME/.dotfiles" --restow .common-dotfiles
But my gut is telling me that this is not a good idea. What am I missing here? Is there any risk running this script? How can I improve this script?
Update 1:
GNU Stow create an exact copy of the source directory structure in the target. So, I first take the directory structure of the source. Change the path to match the target. Then run xargs -I{} rm {} on it.
I have broken the output of the pipe bellow.
First I am searching all the files in the $HOME/.dotfiles/.common-dotfiles directory.
% fd --hidden --ignore-file .gitignore --base-directory="$HOME/.dotfiles/.common-dotfiles" --type l --type f
.config/VSCodium/User/globalStorage/alefragnani.project-manager/projects.json
.config/VSCodium/User/globalStorage/state.vscdb
.config/VSCodium/User/keybindings.json
.config/VSCodium/User/settings.json
.config/VSCodium/User/snippets/c.json
.config/VSCodium/User/snippets/custom-snippets.code-snippets
.config/VSCodium/product.json
Now I am modifying the path using piping the output of previous command to sd ^. $HOME to match the target directory.
% fd --hidden --ignore-file .gitignore --base-directory="$HOME/.dotfiles/.common-dotfiles" --type l --type f | sd ^. $HOME
/home/ismail/.config/VSCodium/User/globalStorage/alefragnani.project-manager/projects.json
/home/ismail/.config/VSCodium/User/globalStorage/state.vscdb
/home/ismail/.config/VSCodium/User/keybindings.json
/home/ismail/.config/VSCodium/User/settings.json
/home/ismail/.config/VSCodium/User/snippets/c.json
/home/ismail/.config/VSCodium/User/snippets/custom-snippets.code-snippets
/home/ismail/.config/VSCodium/product.json
So, now I am no longer deleting files in $HOME/.dotfiles/.common-dotfiles but in $HOME.