Command line archive manager / extracter
I keep forgetting all the corresponding extract commands for every compression format.
So I was thinking about writing a script that merely maps file extensions to the appropriate extract command. But I guess something like this already exists, I just can't find it.
I would be happy with something like file-roller, but file-roller is a GUI tool.
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 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.