Skip to content
Jonas edited this page Apr 20, 2023 · 4 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 path in given directory (first parameter) 
    # that do not math the given white list (second parameter)

    local search_dir=$1
    shift
    local white_list=("$@")
    local find_args=()
    local ignore_path
    for ignore_path in "${white_list[@]}"; do
        local base="$ignore_path"
        # 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 given whitelist and the directory 
    # searched from.
    find "$search_dir" -not \( "${find_args[@]}" -path "$search_dir" \) -prune | \
    while read file; do 
        if [[ -d "$file" ]]; then
            IgnorePath "$file/*"
        else
            IgnorePath "$file"
        fi
    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