Extraction commands often need both an archive name and a destination directory. Keeping those values separate makes scripts easier to review.

Program

Play the script to choose the destination directory used in the extract command.

target_dir
extract_target.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

target_dir="site"
archive="site.tar.gz"
mode="extract"
command="tar -xzf $archive -C $target_dir"
echo "$mode:$command"
#!/usr/bin/env bash

target_dir="restore"
archive="site.tar.gz"
mode="extract"
command="tar -xzf $archive -C $target_dir"
echo "$mode:$command"
#!/usr/bin/env bash

target_dir="tmp"
archive="site.tar.gz"
mode="extract"
command="tar -xzf $archive -C $target_dir"
echo "$mode:$command"
  1. target_dir ← site

    3target_dir="site"4archive="site.tar.gz"
    values this stepsitetarget_dir
  2. archive ← site.tar.gz

    3target_dir="site"4archive="site.tar.gz"5mode="extract"
    values this stepsite.tar.gzarchive
  3. mode ← extract

    4archive="site.tar.gz"5mode="extract"6command="tar -xzf $archive -C $target_dir"
    values this stepextractmode
  4. command ← tar -xzf site.tar.gz -C site

    5mode="extract"6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    values this steptar -xzf site.tar.gz -C sitecommandsite.tar.gzarchivesitetarget_dir
  5. echo "$mode:$command"

    6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    outputextract:tar -xzf site.tar.gz -C site
    values this stepextractmodetar -xzf site.tar.gz -C sitecommand
  1. target_dir ← restore

    3target_dir="restore"4archive="site.tar.gz"
    values this steprestoretarget_dir
  2. archive ← site.tar.gz

    3target_dir="restore"4archive="site.tar.gz"5mode="extract"
    values this stepsite.tar.gzarchive
  3. mode ← extract

    4archive="site.tar.gz"5mode="extract"6command="tar -xzf $archive -C $target_dir"
    values this stepextractmode
  4. command ← tar -xzf site.tar.gz -C restore

    5mode="extract"6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    values this steptar -xzf site.tar.gz -C restorecommandsite.tar.gzarchiverestoretarget_dir
  5. echo "$mode:$command"

    6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    outputextract:tar -xzf site.tar.gz -C restore
    values this stepextractmodetar -xzf site.tar.gz -C restorecommand
  1. target_dir ← tmp

    3target_dir="tmp"4archive="site.tar.gz"
    values this steptmptarget_dir
  2. archive ← site.tar.gz

    3target_dir="tmp"4archive="site.tar.gz"5mode="extract"
    values this stepsite.tar.gzarchive
  3. mode ← extract

    4archive="site.tar.gz"5mode="extract"6command="tar -xzf $archive -C $target_dir"
    values this stepextractmode
  4. command ← tar -xzf site.tar.gz -C tmp

    5mode="extract"6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    values this steptar -xzf site.tar.gz -C tmpcommandsite.tar.gzarchivetmptarget_dir
  5. echo "$mode:$command"

    6command="tar -xzf $archive -C $target_dir"7echo "$mode:$command"
    outputextract:tar -xzf site.tar.gz -C tmp
    values this stepextractmodetar -xzf site.tar.gz -C tmpcommand
extract `tar -xzf` is the common gzip-compressed tar extraction shape.
destination `-C target` tells tar where to extract files.
separate values Storing archive and target separately makes the final command easier to inspect.