Command line archive manager / extracter

275

Solution 1

A couple of suggestions:

unp

unp (available from software-center) is a small perl module that will decompress a wide variety of archives into the current folder. Use unp -s for the archive formats it supports

By default, it decompresses archives into the current folder.

file-roller

file-roller itself can be run from the command line. Use file-roller -h <archive name> to decompress the archive.

Solution 2

From here, to put in your .bashrc:

extract () {
   if [ -f $1 ] ; then
      case $1 in
         *.tar.bz2)   tar xjf $1      ;;
         *.tar.gz)   tar xzf $1      ;;
         *.bz2)      bunzip2 $1      ;;
         *.rar)      rar x $1      ;;
         *.gz)      gunzip $1      ;;
         *.tar)      tar xf $1      ;;
         *.tbz2)      tar xjf $1      ;;
         *.tgz)      tar xzf $1      ;;
         *.zip)      unzip $1      ;;
         *.Z)      uncompress $1   ;;
         *)         echo "'$1' cannot be extracted via extract()" ;;
      esac
   else
      echo "'$1' is not a valid file"
   fi
} 

Solution 3

What hasn't yet been mentioned is the swiss-army-knife of command-line utilities: atool; it is available in the repositories: click atool Install atool or run sudo apt-get install atool.

atool is actually a script comprising various functions such as aunpack, apack, als, acat, adiff, and arepack. Probably the most common use of the program is to extract archives using the aunpack command. There are quite a few interesting and useful options, but the basic way to extract most archives is just to run, for example,

aunpack myarchive.tar

The support for tar, 7z, zip, jar, rar, ar, lha and many more archive formats and their variations are documented by entering man atool in the terminal or by viewing the Ubuntu manpage online.

So, the useful thing is that if you have a tar, zip and gz archive in the same folder, all you need to do to extract them all is to run

aunpack -e *

and all supported archives will be unpacked with their folder structure retained. If the name of the resulting folder already exists, it will not be overwritten, but will be given a unique name like unpack-5645.

Or if you want to choose just one type of archive and unpack them all, instead use

aunpack -e *.zip

If you need files unpacked directly in a particular directory, with only the sub folder directory structure being retained (if it exits), enter, for example,

aunpack -e Scripts11.zip -X /home/mike/Videos

If a file with the same name already exists, you will be prompted for an action either to rename or overwrite unless you have specified the force option (-f) in the command.

As the manpage notes, aunpack first extracts

files to a unique (temporary) directory, and then mov[es] its contents back if possible. This also prevents local files from being overwritten by mistake.


With the other tools, other actions are possible, as mentioned by the man page:

  • apack creates archives from the files or folder specifies or stdin
  • als lists files within the specifies archive
  • acat enables one to extract archive files to stdout
  • adiff enables you to compare the two archives specified
  • arepack makes it possible to convert one archive format to another by placing the contents of the original archive in a temporary folder and then repacking the new one.

There are many more useful options for all of the utilities, but probably the one used the most will be aunpack, as that makes it so easy to extract multiple, different archives. It is great to be able to just issue one command and be able to extract multiple different archive formats without having to use the various commands for tar, zip, etc.

For a very recent article describing how to integrate the atool functions into the custom actions of your filemanager, such as Thunar, see this online magazine.

Solution 4

There is a tool called dtrx -- do the right extraction.

Install it from the Ubuntu repositories and run dtrx somefile.someext. The tool will interactively query the user about nested archives (zip of zip files).

dtrx is a sophisticated version of frabjous's extract.

Share:
275
Caleb S
Author by

Caleb S

Updated on September 18, 2022

Comments

  • Caleb S
    Caleb S over 1 year

    I have a method which needs to search an xml file which was parsed using XmlParser for an element by name and return it only if that element is an end node. For example:

        class xmlTest extends GroovyTestCase {
        def void test(){
            def xmlBody = """
            <rootElement>
                <elementWithOneChild>
                    <endElement>Here is the end</endElement>
                </elementWithOneChild>
                <elementWithManyChildren>
                    <one>1</one>
                    <two>1</two>
                    <three>1</three>
                </elementWithManyChildren>
            </rootElement>"""
    
        def parsedBody = new XmlParser().parseText(xmlBody)
    
        def search1 = parsedBody.depthFirst().grep({it.name() == "elementWithOneChild"})
        println search1[0].children().size()
    
        def search2 = parsedBody.depthFirst().grep({it.name() == "endElement"})
        println search2[0].children().size()
    
        def search3 = parsedBody.depthFirst().grep({it.name() == "elementWithManyChildren"})
        println search3[0].children().size()
        }   
    }
    

    My attempt to use Node.children().size() works except for the 1 to 1 case where an element contains one child element. In this case, search1.children().size() and search2.children().size() both return 1. Although, the size for elementWithManyChildren is 3. I am looking for some way to be able to tell an end node apart from an element with one child.

    One way I have found to work is:

    try{
        search1[0].children().iterator().next().name()
    }catch(e){
       //If the next node does not have a name, it is an end node
    }
    

    But that solution just seems like a poor one.

  • Caleb S
    Caleb S about 13 years
    Thanks, this seems to work and is not depth specific. Can you explain a little on what the '**' is doing?
  • coopbird19
    coopbird19 over 9 years
    Add an example of how to use it?
  • frabjous
    frabjous over 9 years
    extract file.zip