views:

73

answers:

3

i have a script that i need to run on a large number of files with the extension *.tar.gz.

what i want to do is instead of uncompressing them and then running the script, i want to be able to uncompress them as i run the command and then work on the uncompressed folder, all with a single command.

i think a pipe is a good solution for this but i haven't used it before. how wud i do this?

+1  A: 

The -v orders tar to print filenames as it extracts each file:

tar -xzvf file.tar.gz | xargs -I {} -d\\n myscript "{}"

This way the script will contain commands to deal with a single file, passed as a parameter (thanks to xargs) to your script ($1 in the script context).

Edit: the -I {} -d\\n part will make it work with spaces in filenames.

aularon
-c and -z? and no argument to -f?
strager
@strager sorry I was fixing that
aularon
`-v` will give you a list of each file expanded. @sfactor wanted his script to "work on the uncompressed folder" presumably for each of the files `*.tar.gz` (which you'll need to add to your `tar` command).
Johnsyweb
@Johnsyweb: he is saying: _what i want to do is instead of uncompressing them and then running the script, i want to be able to uncompress them as i run the command and then work on the uncompressed folder, **all with a single command** ._
aularon
@strager -z is because he wants to work on a `.gz` tar archive.
aularon
@strager sorry I thought one had to put `-z` when extracting as well as when compressing, now I found it can be omitted :)
aularon
Note that if any of the filenames contain spaces (or worse, newlines) this won't work well.
bdonlan
@bdonlan I fixed it, to make file with spaces work. thanks
aularon
Instead use -0 with xargs, which reads until null termination, and takes it as single file name, even with newlines in it. And print null at the end of each file name.
thegeek
A: 

You can use a for loop:

for file in *.tar.gz; do tar -xf "$file"; your commands here; done

Or expanded:

for file in *.tar.gz; do
    tar -xf "$file"
    # your commands here
done
strager
if you don't quote $file, this will fail for any filenames containing spaces.
static_rtti
@static_rtti, That's evil! But you're right; thanks.
strager
A: 

The following three lines of bash...

for archive in *.tar.gz; do
    tar zxvf "${archive}" 2>&1 | sed -e 's!x \([^/]*\)/.*!\1!' | sort -u | xargs some_script.sh
done

...will iterate over each gzipped tarball in the current directory, decompress it, grab the top-most directories of the decompressed contents and pass those as arguments to somescript.sh. This probably uses more pipes than you were expecting but seems to do what you are asking for.

N.B: tar xf can only take one file per invocation.

Johnsyweb