(Bash) Let `tar` unzip into desired directory instead

Scenario 1: Unzip contents at a desired location

Use -C <desired_dir> to let tar first cd into <desired_dir> before unzipping. The contents of the tar file will be written into the directory.

Scenario 2: Change the name of the unzipped directory

This scenario assumes the tar contents being within a single dir (i.e. unzipping tar outputs a single directory <orig_dir> which then contains items).

  1. Create a new directory with the desired name <desired_dir>
  2. Use -C <desired_dir> --strip-components=1. The second option (with value 1) strips the first directory level, effectively removing <orig_dir> from the paths of the unzipped files. The files would then be unzipped into <desired_dir>.

Refer to man tar for details to the above options.

NOTE

Remote tar files can be unzipped without explicit downloading by using curl -L <path_to_remote_tar> then piping it to tar -xz.

References:

Written on May 25, 2021