views:

154

answers:

3

I'm extracting a tar file within a shell script and it is is creating folder with a name like:

supportsuite-5-20-3/

The "supportsuite" part of the name is always the same, but I don't know what the remaining characters are in advance. I'm unpacking the tar file in a script and I need the name of the resulting folder from within that script. What is the best way to put that directory name into a shell variable within the script after the tar has been extracted?

I'm script challenged, so thanks in advance for any help you can provide.

A: 

This will unzip it and keep the folder name in tact.

find . -type f -name "*.tar" -exec tar -xv '{}' \;
Gazler
A: 

If you're assuming that all of the files in the tar file are in the same directory, you could do something like:

DIRNAME=$(tar -tvf $TARFILE  | head -1 | sed -e 's:^.* \([^/]*\)/.*$:\1:')

Note that it will probably misbehave if your filenames have spaces in them. If you care about this you'll need to adjust the regex in the sed command.

Laurence Gonsalves
A: 

It's a little bit of a hack, but this might help (written for bash:

tar zxf supportsuite-5-20-3.tgz
NEWDIR=$(cd supportsuite-* && pwd)
jheddings
Could do just NEWDIR="supportsuite-*" Though both approaches assumes theres one and only one directory starting with supportsuite- in the current directory.
nos