Sometimes there is a need to extract some files on the Linux console. Below you’ll find a list of the most common methods for that.
tar
Tar archives are the most common way of distributing bundles of files under Linux or UNIX. The .tar
file represents a bundle of files packaged with GNU tar program. To extract such files use:
tar xf somearchive.tar
tar xvf somearchive.tar
-
Provide option
f
if you want to extract the content of files. Tar (from tape archive) has a long history and was intended to work with tape media, so when you omitf
tar try to work with a tape device. -
v
- stands for verbose. List all the files by extract process. -
x
- Extract command.
Before extracting you may be interested in Looking inside of tar. Do it with option t
:
tar tf archive.tar
gzip
Often tar files are also compressed. One of the most known compressed formats is GNU Zip (gzip). Tar bundled and zipped files would normally have extension .tar.gz. To extract such files you can use tar
with z
option. This way tar
would also automatically invoke gzip. Modify the above example and you get able to extract tar.gz files too.
tar -xzf somearchive.tar.gz
In a very old tar version, the z
flag was not available, initially, those operations were used with mighty UNIX pipes:
gzip -dc somearchive.tar.gz | tar xf -`
As we see in the above example. -d
flag is used to decompress. Of course, you can decompress files without using tar.
However, gzip is designed to compress (and replace uncompressed) files one by one (Find more details about it in the corresponding article), this rule is also applicable for decompression as we see above. Use k
flag to keep the source file.
Gzip flags recap:
d
- decompressc
- outputs to console instead of filet
- Tests file integrityl
- lists archive file informationv
- Option shows affected file and compression rationk
- keeps source file
bzip2
Sometimes you can find files ending with .tar.bz2. Those are files packaged with bzip (a block-sorting file compressor). Use it like gzip
tar xjvf filename.tar.bz2
Flags -d,-c,-t
have the same meaning as in gzip above.
zcat
Zcat is a command line utility for viewing the contents of a compressed file without literally uncompressing it. It expands a compressed file to standard output allowing you to have a look at its contents.
Actually, zcat
works with gzipped files and extracts them to the console. So we can pipe and extract our tar.gz
and tar.z
files this way:
zcat somearchive.tar.z | tar xf -
The output to console is a powerfull Unix way to create desired outcomes
# Hehre searching inside the compressed files in one line without creating extra file
zcat example.docx.gz | grep "my function"
The output can be also redirected to a file wia >
operator.
zcat file1.txt.gz file2.txt.gz > new_file.txt
zip
The most convenient way to work with zip files are zip
and unzip
programs. Both wou would be preinstalled on most Linux distributions.
#Unzip in place
unzip file.zip
#Unzip into the desired folder if the leaf directory do not exist it would be created (but only leaf)
unzip file.zip -d <destination_folder>
# Only list archive content
unzip -l file.zip
Custom ‘unpack’ Function
Do all this and more with one command, by adding a function to your .bashrc
file. Thank you Wesley Rice.
function unpack () {
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via >unpack<" ;;
esac
}