$_ CLI In Depth
Learning progress 0%
build environment path dotfiles

Build: A Dotfiles Installer with Backups

5 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:10

The specification

The installer takes two arguments. The first is a source directory that holds dotfiles, which are files whose names start with a dot. The second is a destination directory, normally your home directory. The script must do four things. It must refuse to run when the source directory does not exist, and it must exit with a non-zero status when it does. It must create a symbolic link in the destination for every dotfile in the source. When the destination already has a file or a link with the same name and it does not point to the right source, the script must move that file into a backup directory named .dotfiles-backup inside the destination. Finally, running the script a second time must do nothing harmful: links that are already correct are left alone, and nothing that is already in the backup is overwritten.

Why a backup, and why a plain move

A destructive installer is a common source of lost settings. If the script deletes an old .bashrc, the hours spent tuning it are gone. Moving the file into a backup directory costs nothing and makes the change reversible. The backup is kept in the destination rather than in the source so that it stays with the machine where the change happened.

The script uses mv rather than rm. The move works for regular files, directories, and symbolic links, so the same rule applies to every kind of existing entry. A file name in the backup directory is the same as the original name, which makes it easy to restore by moving it back.

Working through the rules

Most of the logic is a loop over the source files and a single test per file. The test asks whether the destination entry is already a link that resolves to the same source. The readlink -f command resolves symbolic links to their final absolute path, so two different spellings of the same file compare as equal. When the test passes, the loop continues without touching the file. When it fails, the script creates the backup directory, moves the old entry there, and creates the link.

Tip

Use set -eu at the top of an installer. The script stops on the first error, and it reports an unset argument instead of silently using an empty path.

The reference solution

The reference script below meets the full specification. It is short, and each line corresponds to a rule. Read it once with the rules in mind, then write your own version before you compare.

BASH17 lines
#!/bin/bash
# usage: install.sh SRC_DIR DEST_DIR
set -eu
src=${1:?usage: install.sh SRC_DIR DEST_DIR}
dest=${2:?usage: install.sh SRC_DIR DEST_DIR}
[ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; }
backup="$dest/.dotfiles-backup"
mkdir -p "$dest"
for f in "$src"/.[!.]*; do
  [ -e "$f" ] || continue
  name=$(basename "$f")
  target="$dest/$name"
  if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi
  if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi
  ln -s "$(readlink -f "$f")" "$target"
  echo "linked $name"
done

Things the script does not do

The installer does not run other programs, does not edit files, and does not read the content of the dotfiles. It only moves and links. That narrow scope is a feature. Installing packages, changing the shell, or writing into a file such as .bashrc belongs in a separate step, where the change is visible. Keeping the installer to linking also means it is easy to audit in a few minutes.

The installer also has no opinion about where the dotfiles repository lives. You can keep it in a Git checkout, in a plain directory, or in a bundle copied from another machine. The script only reads the directory you name, which means the same tool works for all three layouts.

The script also does not handle directories inside the source. A dotfile directory such as .config is linked as a single entry, which is the behavior most people want, but it means the whole directory is shared between the source and the destination.

Testing the installer safely

Test the installer on a throwaway source and destination before you run it on your real home. Create a small source directory with two dotfiles, create a destination with one conflicting file, run the script, and inspect the result. Then run the script a second time. A correct installer prints nothing new on the second run except the links it had to make, and the backup still holds the original file exactly as it was.

Extending the design

Once the basic version works, there are natural extensions. A dry-run flag can print the actions instead of performing them. A restore command can move each backup entry back to its name. A timestamped backup directory keeps several rounds of backups separate. Add each one only when you need it, and test the second run after every change, because idempotence is the property that makes an installer safe to repeat.

Terminal lab · 3 steps · real Linux in your browser

Dotfiles installer with backups

Build install.sh in three steps: link dotfiles, move conflicting files into a backup, and refuse a missing source.

01
Link every dotfile
Create install.sh in /workspace. It takes a source directory and a destination directory, and it links every dotfile from the source into the destination: ``bash cat > /workspace/install.sh <<'EOF' #!/bin/bash set -eu src=$1 dest=$2 mkdir -p "$dest" for f in "$src"/.[!.]*; do name=$(basename "$f") ln -sfn "$(readlink -f "$f")" "$dest/$name" echo "linked $name" done EOF ` Run bash install.sh /workspace/src /workspace/home. Then /workspace/home/.bashrc and /workspace/home/.vimrc` should be symbolic links.
Show hint for step 1
Use readlink -f on the source so that the link points at the real file.
02
Move conflicting files into a backup
Replace install.sh with a version that moves any existing entry that does not already link to the source into .dotfiles-backup, and then creates the link. Rerunning must leave correct links alone: ``bash cat > /workspace/install.sh <<'EOF' #!/bin/bash set -eu src=$1 dest=$2 backup="$dest/.dotfiles-backup" mkdir -p "$dest" for f in "$src"/.[!.]*; do [ -e "$f" ] || continue name=$(basename "$f") target="$dest/$name" if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi ln -s "$(readlink -f "$f")" "$target" echo "linked $name" done EOF ` The setup creates a real /workspace/home/.bashrc that holds export OLD=1. After you run the script, the backup should hold that original file, and .bashrc` should be a link to the source.
Show hint for step 2
A correct link is skipped by the readlink comparison, so a second run never moves a link into the backup.
03
Refuse a missing source directory
Replace install.sh with the final version. It stops with a message and a non-zero status when the source directory does not exist, and it creates nothing in that case: ``bash cat > /workspace/install.sh <<'EOF' #!/bin/bash # usage: install.sh SRC_DIR DEST_DIR set -eu src=${1:?usage: install.sh SRC_DIR DEST_DIR} dest=${2:?usage: install.sh SRC_DIR DEST_DIR} [ -d "$src" ] || { echo "no such source directory: $src" >&2; exit 1; } backup="$dest/.dotfiles-backup" mkdir -p "$dest" for f in "$src"/.[!.]*; do [ -e "$f" ] || continue name=$(basename "$f") target="$dest/$name" if [ -L "$target" ] && [ "$(readlink -f "$target")" = "$(readlink -f "$f")" ]; then continue; fi if [ -e "$target" ] || [ -L "$target" ]; then mkdir -p "$backup"; mv "$target" "$backup/$name"; fi ln -s "$(readlink -f "$f")" "$target" echo "linked $name" done EOF ` Run bash install.sh /workspace/nope /workspace/home-x. It should fail and leave no /workspace/home-x` directory.
Show hint for step 3
Check the source before the first mkdir, so a bad argument creates nothing.
Check yourself Question 1 of 4
Concept Check Handling a conflicting file recall

What should the installer do with an existing file in the destination that does not match the source?

Recap & Key Takeaways

An installer should link dotfiles and move conflicting files into a backup directory rather than deleting them.
A second run should leave correct links alone and never overwrite the backup.
readlink -f lets the script compare two spellings of the same file.
Set -eu and check the source directory first so that bad input stops the script early.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue