Tar gotcha: extract from multiple tar files in one go
Suppose you have a monthly process to archive some data such as log files etc. Each month a separate archive file is created, and so after a few months you will have several archive files – for example as shown below:
archive.2014-08.tar.gz
archive.2014-09.tar.gz
archive.2014.10.tar.gz
Now if you wish to extract your data from all three files, you could run individual commands such as:
1 2 3 |
$tar -zxvf archive.2014-08.tar.gz $tar -zxvf archive.2014-09.tar.gz $tar -zxvf archive.2014-10.tar.gz |
This works fine. However the following won’t work if you want a one-line which does them all in one short:
1 2 3 |
$tar -xzvf *.tar.gz tar: archive.2014-09.tar.gz: Not found in archive tar: archive.2014-10.tar.gz: Not found in archive |
Basically the way that tar command works, if more than one filename (or an expansion) is passed as an argument, it will look for the second, third, etc. files
1 |
$ls *.tar.gz | xargs -n1 tar -xzvf |