How to extract specific file from tar archive

Extract specific file from tar archive.

Download sample archive file - Do you think you know Python? is a great example here.

$ wget --quiet https://github.com/satwikkansal/wtfpython/archive/v3.0.0.tar.gz

Display archive contents.

$ tar --list --verbose --gzip --file v3.0.0.tar.gz 
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/
-rw-rw-r-- root/root        84 2019-12-21 17:08 wtfpython-3.0.0/.gitattributes
-rw-rw-r-- root/root       274 2019-12-21 17:08 wtfpython-3.0.0/.gitignore
-rw-rw-r-- root/root       120 2019-12-21 17:08 wtfpython-3.0.0/.travis.yml
-rw-rw-r-- root/root      2229 2019-12-21 17:08 wtfpython-3.0.0/CONTRIBUTING.md
-rw-rw-r-- root/root      2721 2019-12-21 17:08 wtfpython-3.0.0/CONTRIBUTORS.md
-rw-rw-r-- root/root       460 2019-12-21 17:08 wtfpython-3.0.0/LICENSE
-rw-rw-r-- root/root    109732 2019-12-21 17:08 wtfpython-3.0.0/README.md
-rw-rw-r-- root/root      3231 2019-12-21 17:08 wtfpython-3.0.0/code-of-conduct.md
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/images/
-rw-rw-r-- root/root      8317 2019-12-21 17:08 wtfpython-3.0.0/images/logo.png
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/images/string-intern/
-rw-rw-r-- root/root      9731 2019-12-21 17:08 wtfpython-3.0.0/images/string-intern/string_intern.png
-rw-rw-r-- root/root      7515 2019-12-21 17:08 wtfpython-3.0.0/images/tic-tac-toe.png
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/images/tic-tac-toe/
-rw-rw-r-- root/root    162385 2019-12-21 17:08 wtfpython-3.0.0/images/tic-tac-toe/after_board_initialized.png
-rw-rw-r-- root/root     51686 2019-12-21 17:08 wtfpython-3.0.0/images/tic-tac-toe/after_row_initialized.png
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/
-rw-rw-r-- root/root       456 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/insert_ids.py
-rw-rw-r-- root/root     12273 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/notebook_generator.py
-rw-rw-r-- root/root      1346 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/notebook_instructions.md
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/obsolete/
-rw-rw-r-- root/root      1629 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/obsolete/add_categories
-rw-rw-r-- root/root      1532 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/obsolete/generate_contributions.py
-rw-rw-r-- root/root     70597 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/obsolete/initial.md
-rw-rw-r-- root/root      4129 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/obsolete/parse_readme.py
-rw-rw-r-- root/root    209480 2019-12-21 17:08 wtfpython-3.0.0/irrelevant/wtf.ipynb
-rw-rw-r-- root/root       205 2019-12-21 17:08 wtfpython-3.0.0/mixed_tabs_and_spaces.py
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/
-rw-rw-r-- root/root     72018 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/content.md
-rw-rw-r-- root/root      1521 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/setup.py
drwxrwxr-x root/root         0 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/wtf_python/
-rw-rw-r-- root/root         0 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/wtf_python/__init__.py
-rw-rw-r-- root/root       868 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/wtf_python/main.py
-rw-rw-r-- root/root       136 2019-12-21 17:08 wtfpython-3.0.0/wtfpython-pypi/wtfpython

Extract README.md to the current directory.

$ tar --extract --verbose --gzip --file v3.0.0.tar.gz --strip-components 1 wtfpython-3.0.0/README.md
wtfpython-3.0.0/README.md
$ ls
README.md  v3.0.0.tar.gz

Extract README.md to the extracted directory.

$ mkdir extracted
$ tar --extract --verbose --gzip --file v3.0.0.tar.gz --strip-components 1 --directory extracted wtfpython-3.0.0/README.md
wtfpython-3.0.0/README.md
$ ls extracted/
README.md

Extract *.png images that are at least three components deep to the images directory.

$ mkdir images
[email protected]:~/test$ tar --extract --verbose --gzip --file v3.0.0.tar.gz --strip-components 3 --directory images --wildcards wtfpython-3.0.0/images/*.png
wtfpython-3.0.0/images/string-intern/string_intern.png
wtfpython-3.0.0/images/tic-tac-toe/after_board_initialized.png
wtfpython-3.0.0/images/tic-tac-toe/after_row_initialized.png
$ ls images/
after_board_initialized.png  after_row_initialized.png  string_intern.png