0

I have 30 tgz files like these:

live-3.0.10-20101031.tgz
live-3.0.4-20101005.tgz

when I extract them it will be extract in to "live" folder.(ie: tar xvzf live-3.0.10-20101031.tgz will extract to "live" folder I would like to have a small script that will extract all files from oldest to newest to "live" folder. I want new files to be copied over old files this is what I did but I'm not if it's good...can you please let me know what you think?

#!/bin/bash 
for f in live*.tgz; do
    echo $f
    tar xzvf $f &
done
bmk
  • 2,339
  • 2
  • 15
  • 10
edotan
  • 1,876
  • 13
  • 39
  • 57

2 Answers2

1

Try this, you're missing the ; to separate both commands. Also the & is probably not needed, you could use it to parallelize the tar invocations but it's likely to do more harm than good if you have more than a couple of files (they will compete for I/O and CPU).

For sorting the file list correctly you can use sort -V, which sorts taking into account version numbers (the * wildcard on bash or plain sort would list version 10 before version 4, as 1<4). This should work:

for f in `ls live*.tgz | sort -V`; do echo $f; tar xzvf $f; done
Eduardo Ivanec
  • 14,881
  • 1
  • 37
  • 43
1

Not tested, form memory:

ls -1rt live*tgz | xargs tar xvf

Explanation:

  • ls -1 --> list files in one colum
  • ls -t --> list files based on modification time (best approach to creation time)
  • ls -r --> list in reverse order (older first)

Then the list is piped to tar trough xargs so tar takes each file one by one and unpacks them as you wanted, from older to newer.

HTH

hmontoliu
  • 3,753
  • 3
  • 23
  • 24
  • +1, this is the way to go if the creation time can be trusted (that is, versions were actually tarred in the right order - a reasonable assumption by the way). – Eduardo Ivanec May 02 '11 at 17:39
  • this is what I get:tar: live-3.0.10-20101031.tgz: Not found in archive tar: Error exit delayed from previous errors – edotan May 02 '11 at 17:53
  • Try this: `ls -1tr live*tgz | xargs -L 1 tar xvf`. – Steven Monday May 02 '11 at 18:02
  • it looks like it's working now but how can I be sure it really takes all the files and extracting them and not only the first file? – edotan May 02 '11 at 18:15
  • Add `-t` to the xargs command. With it xargs prints the command line being executed before actually executing it. – Eduardo Ivanec May 02 '11 at 18:37