Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to automatically create symlinks to config files
#!/usr/bin/env python3
from pathlib import Path
from subprocess import run
# DOTFILES is the path to where you store your dotfiles
DOTFILES = Path.home().joinpath(".dotfiles/")
# SHELL_CMD should output a list of paths to files in your dotfiles directory
# They should be separated by newlines (\ņ)
SHELL_CMD = r"fdfind --hidden --type f --exclude '\.git' --exclude '\#*' ."
# EXCLUDES is a list of strings (interpreted as paths) to ignore
excludes_paths = [Path(x) for x in EXCLUDES]
prcs = run(SHELL_CMD, shell=True, capture_output=True, cwd=DOTFILES)
path_str = prcs.stdout.decode("utf-8")
print("Found the following dotfiles:\n", path_str)
path_list = [Path(x) for x in path_str.split("\n")]
path_dirs = [x.parent for x in path_list]
def main():
for dir in path_dirs:
if not Path.home().joinpath(dir).is_dir():
print(f"Creating directory {dir}")
for pp in path_list:
home_path = Path.home().joinpath(pp)
df_path = DOTFILES.joinpath(pp)
if (not home_path.is_symlink()) and not in excludes_paths:
print(f"Linking file {df_path} to symlink {home_path}")
except FileExistsError as err:
print(f"File {pp} already exists at {home_path}")
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment