Commands.page Logo

How to Extract Specific Files from Archive Ubuntu

This guide explains how to extract specific files or a range of files from large archives on Ubuntu. You will learn command-line methods using tar and unzip to avoid unpacking entire datasets, saving time and disk space.

List Archive Contents

Before extracting data, you must identify the exact file paths inside the archive. Open your terminal and use the list command appropriate for your file type.

For tar archives, run:

tar -tf archive.tar.gz

For zip archives, run:

unzip -l archive.zip

Review the output to note the exact names and paths of the files you wish to retrieve.

Extract Specific Files from Tar

To extract specific files from a tarball, provide the filenames after the archive name. You do not need to extract the entire contents.

Use the following syntax:

tar -xf archive.tar.gz path/to/file1.txt path/to/file2.txt

The -x flag tells tar to extract, and -f specifies the filename. You can list as many specific files as needed separated by spaces. Ensure you use the exact paths shown in the list step.

Extract Specific Files from Zip

The unzip command allows you to target specific files similarly. Append the filenames you want to the end of the command.

Use the following syntax:

unzip archive.zip file1.txt file2.txt

Unzip will only decompress the listed files and ignore the rest of the archive contents.

Using Wildcards for File Ranges

Archives do not support numerical ranges directly, but you can use wildcards to extract a range of sequentially named files. This is useful if your files are named like data_01.log, data_02.log, etc.

To extract files matching a pattern, use an asterisk:

tar -xf archive.tar.gz data_0*.log

This command extracts all files starting with data_0 and ending with .log. You can combine this with specific filenames to customize your extraction range further. Always test your wildcard pattern with the list command first to ensure it matches only the intended files.