There are different types of archives – those are canonical (for tapes) tar archive files, there are tar files compressed with Z, gzip or bzip2, etc. There are also apt, deb, rpm, yum packages, but those do require full scale package manager, as opposed to simple extraction utility.
This short tip should give you instructions on extracting archived files from different archives, using Linux shell utility called ‘tar’.
Now let us show you few known extraction techniques for simple archives.
To extract files from archive with *.tar extension, you should execute the following command:
tar xf filename.tar
NOTE: This will extract files to current directory. If you want to extract it somewhere else, you should execute this command instead:
tar xf filename.tar -C /somepath
This will extract all files from filename.tar and put them into /somepath folder. NOTE: The path should exist already (mkdir /somepath to create new directory).
To extract files from *.tar.gz or *.tgz archive, you should issue the following command:
tar zxf filename.tar.gz
ORtar zxf filename.tgz
To extract files from *.tar.bz2 archives, you should use the following command:
tar jxf filename.tar.bz2
To extract files from *.tar.Z archive, you should issue the following command:
tar Zxf filename.tar.Z
NOTE: Z should be capital, otherwise no files will be extracted, since small ‘z’ means to use Gzip filter, but big ‘Z’ means to use Compress filter – which are DIFFERENT.
To see how files are being extracted, use the ‘v’ (as in Verbose) switch.
tar zxvf filename.tar.gztar jxvf filename.tar.bz2tar xZvf filename.tar.Z
To extract a single file from using TAR utility, execute one of the following commands:
tar zxf filename.tar.gz somefile1 somefile2 – for extraction of somefile1 and somefile2 ONLY, from tar.gz archivetar jxf filename.tar.bz2 somefile – for extraction of somefile ONLY, from tar.bz2 archivetar Zxf filename.tar.Z someFile – for extraction of someFile ONLY, from tar.Z archive
By default, tar will overwrite any files that match those names and paths as given in the archive. You can change this behavior by adding ‘k’ switch.
tar zxfk filename.tar.gz – will extract ONLY files that are NOT EXISTING in the path of extraction, giving “File exists” message for every file found
To overwrite all files, except those that are newer than ones in archive, issue this command:
tar --keep-newer-files zxf file.tgz – will keep all newer files found in path of extraction
Add new comment