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

When you write:

tar -xf *.gz

the tar command sees (for example):

tar -xf abc.tar.gz def.tar.gz ghi.tar.gz

This is interpreted as a request to extract def.tar.gz and ghi.tar.gz from the archive abc.tar.gz. Since the files aren't in there, you get the warning message.

In other words, tar operates on a single tar file (possibly compressed) at a time (in one invocation). It does not operate on multiple tar files.

Note that if abc.tar.gz contains a file pqr/xyz/important.c, you can extract just the one file by specifying:

tar -xf abc.tar.gz pqr/xyz/important.c

The notation you used is only a variant on this notation.

(And yes, there can be reasons to tar a tar file. For example, Gmail does not allow you to ship a tar file or a gzipped tar file which contains a file that is executable. However, if you embed a gzipped tar file inside a non-compressed tar file, it does not look inside the inner file to find the executable file. I use this when I need to ship a tar file with an executable configure script.)


the main reason is tar takes a single filename argument. When you are calling tar *.tgz bash extends the * to all file names with tgz formant and it becomes tar file1 file2 file3 which is not accepted by tar. So either you use a for loop like for i in *.tgz; do tar -zxvf $i ;done or use some other command which executes tar as a side effect like shown in comments, is ls or find . -maxdepth 1 -name \*tgz -exec tar -zxvf {} \; (ls output is always risky)


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.

Tags:

Linux

Shell

Tar