I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are:
- there will only be one top level directory
- the folder name doesn't necessarily match the tar.xz/gz filename (in other words something-1.3.5a.tar.gz might extract to a folder called something-1.3.5)
In order to lessen the amount of typing I have to do I wrote a small script, which among other things does the follow:
- extracts the tar.xz/gz file
- cds into the directory
My current hack is to do something like this:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
Basically what this does is it captures the output of the extraction, and takes the first line (which is the top level directory) and then cds into it.
So, my question is this, is there a cleaner/better way of doing this?
t
option intar
to list the contents, e.g.:cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.tar
but rathergtar
. Since there is also no standard tar option-z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...