Skip to content
Hyacinthe Cartiaux edited this page Aug 15, 2025 · 8 revisions

If you want to whitelist files instead of blacklisting files using IgnorePath you can define a helper function that ignores everything except a specific whitelist:

function IgnorePathsExcept() {
    # Ignore all paths in the given directory (first parameter)
    # that do not match the given whitelist (second parameter)

    local search_dir="${1%/}"
    shift
    local white_list=("$@")
    local find_args=()
    local ignore_path

    if [ ! -d "$search_dir" ]; then
        FatalError "The path ${search_dir} must be an existing directory\n"
    fi

    for ignore_path in "${white_list[@]}"; do
        local base="$ignore_path"
        if [[ "$ignore_path" =~ ^/ ]]; then
            FatalError "The path ${ignore_path} in the whitelist is not relative to the directory ${search_dir}\n"
        fi
        # Add all base paths to the argument list as well, otherwise
        # -prune will prevent us from reaching the whitelisted files.
        while [ "$base" != '.' ]; do
            find_args+=(-path "$search_dir/$base" -o)
            base="$(dirname "$base")"
        done
    done

    # Find everything except the given whitelist and the directory
    # being searched from
    find "$search_dir" -not \( "${find_args[@]}" -path "$search_dir" \) -prune | \
        while read -r file; do
            if [[ -d "$file" ]]; then
                IgnorePath "$file/*"
            fi
            IgnorePath "$file"
        done
}

Let's say you want to track only specific folders and files from your home directory (~). You can do this by simply defining a whitelist and using the helper function we just defined:

white_list=(
    '.gitconfig'
    '.zshrc'
    '.config/*'
    '.kde4/share/config/*'
    '.zsh/*'
)
IgnorePathsExcept ~ "${white_list[@]}"

Now everything in your home directory except the given whitelist will be ignored.

Clone this wiki locally