Shell 'tar: not found in archive' error when using regular expression

notbad picture notbad · Jun 5, 2013 · Viewed 73.1k times · Source

When I use tar -xzf *.gz to extract all the .gz files in the current directory, I get Not found in archive error. However, it works fine if I extract one by one or use a for-loop like

for file in `ls *.gz`; do tar -xzf $file; done

What is the reason for this error?

Answer

Igor Chubin picture Igor Chubin · Jun 5, 2013

When you write

 tar -xzf *.gz

your shell expands it to the string:

 tar -xzf 1.gz 2.gz 3.gz

(assuming 1.gz, 2.gz and 3.gz are in you current directory).

tar thinks that you want to extract 2.gz and 3.gz from 1.gz; it can't find these files in the archives and that causes the error message.

You need to use loop for of command xargs to extract your files.

ls *.gz |xargs -n1 tar -xzf

That means: run me tar -xzf for every gz-file in the current directory.