Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 30 tgz files like these:


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?

for f in live*.tgz; do
    echo $f
    tar xzvf $f &
share|improve this question

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
share|improve this answer
how can I control that the older files will extrac first and last will be the newst file? – edotan May 2 '11 at 16:59
Do you mean to sort by the date on the filename (20101031 in your first example) or the one the file itself (the one that ls -l shows, for example)? – Eduardo Ivanec May 2 '11 at 17:01
See the edit, if you mean by version and filename this latest version should work. – Eduardo Ivanec May 2 '11 at 17:05
when I try to run for f in ls live*.tgz | sort -V; do echo $f; tar xzvf $f; done I get:sort: invalid option -- V Try `sort --help' for more information. – edotan May 2 '11 at 17:27
What version of Linux have you got? Perhaps -V is a recent extension. – Eduardo Ivanec May 2 '11 at 17:34

Not tested, form memory:

ls -1rt live*tgz | xargs tar xvf


  • 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.


share|improve this answer
+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 2 '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 2 '11 at 17:53
Try this: ls -1tr live*tgz | xargs -L 1 tar xvf. – Steven Monday May 2 '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 2 '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 2 '11 at 18:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.