Sometimes there is a need to extract some files on Linux console. Below y’ll find list of 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 content of files. Tar (from tape archive) has long history and was intended to work with tape media, so when you omitf
tar tries to work with 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 file would normally have extension .tar.gz. To extract such files you can use tar with z
option, which causes tar to automatically invoke gzip. Modify above example and you get able to extract tar.gz files too.
tar -xzf somearchive.tar.gz
In old tar version, the “z” option is maybe not available. In that case, just use mighty UNIX pipes:
gzip -dc target.tar.gz | tar xf -
Gzip options explained
d
- Do decompress!c
- write to the console (So that tar can take it from there )t
- Tests file integrityl
- lists archive file information
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
Options d,c,t have the same meaning.
zcat
Some files have .tar.Z endings. They can be extracted by
zcat somearchive.tar.Z | tar xf -
Custom ‘unpack’ Function
Do all this and more with one command, by adding 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
}