---
title: Optimisation de tar
author: Frederic AOUSTIN
version: 1.0
last_date: 18/09/2026
---

# Optimisation de tar

![category](system)

Un simple truc et astuce avec tar: j'utilise souvent tar pour échanger des fichiers ... le principal soucis est lorsque que je lance ma commande tar

```bash
tar -cvf test.tar test/
```

Si le dossier *test* contient un dépot git je récupère le dossier *.git*, si il contient un environnement virtuel python ... même chose.

Par défaut **tar** ne gère pas, comme git ou docker par exemple, un fichier *.tarignore*

Mais il est assez simple de redefinir dans son fichier **.bashrc** une fonction tar pour gérer un tel fichier

```bash
tar() {
    local args=()
    local excludes=()
    local arg
    local found_create=false
    local skip_next=false
    local target

    for arg in "$@"; do
        if [[ "$arg" == "--" ]]; then
            break
        fi

        if [[ "$arg" == "-c" || "$arg" == -*c* ]]; then
            found_create=true
            break
        fi
    done

    if ! $found_create; then
        command tar "$@"
        return
    fi

    for arg in "$@"; do

        if $skip_next; then
            skip_next=false
            continue
        fi

        case "$arg" in
            -f|--file|-C|--directory)
                skip_next=true
                continue
                ;;
            -*)
                continue
                ;;
        esac

        if [[ -d "$arg" ]]; then
            target="${arg%/}"

            if [[ -f "$target/.tarignore" ]]; then
                excludes+=("--exclude-from=$target/.tarignore")
            fi
        fi
    done

    command tar "${excludes[@]}" "$@"
}
```

en faite cette fonction permet juste d'utiliser le paramètre **exclude-from** si un fichier **.tarignore** existe

simple ... efficase ... vive le batch

