Vincent Laufer
Vincent Laufer

Reputation: 705

Why do some linux commands work with a "*" in a list of files but others do not?

why does, for instance,

ls -1 /path/to/something/data*data/file*.txt 

work fine, while something like the following returns an error:

tar -xzvf *tar.gz
tar: evsClient-v.0.0.6.tar.gz: Not found in archive
tar: evsClient-v.0.0.7.tar.gz: Not found in archive

Upvotes: 2

Views: 70

Answers (3)

Jens Krogsboell
Jens Krogsboell

Reputation: 1123

The "*" is actually expanded by the shell and the resulting list of filenames are then presented as arguments to the command in question.

The "ls" command supports a list of file names and so does the "tar" command. But the signature of tar is:

tar option(s) archive_name file_name(s)

So - in your example I assume that the command line is expanded to:

tar -xzvf evsClient-v.0.0.5.tar.gz evsClient-v.0.0.6.tar.gz evsClient-v.0.0.7.tar.gz

giving you the error because the two latter archives cannot be extracted from the first.

Upvotes: 1

chepner
chepner

Reputation: 532333

When * isn't quoted, any word containing it is treated as a shell pattern, which expands to a list of file names matching that pattern.

In your first example, the pattern expands to a list of existing files, which ls then dutifully displays.

In your second example, the pattern again expands to a list of matching files. However, only the first member of that list is treated as the argument to the f option. The remaining items are names of files you want to extract from the first one, which is not what you intended.

The general rule is that the pattern simply provides a list of file names; it's up to you to ensure that the resulting list of files is a correct set of arguments for the command you are running.

Upvotes: 2

jxh
jxh

Reputation: 70502

The -f option to tar only expects one argument to specify the file to process. If using a glob expression as you have to tar -xzvf and there are multiple files that get expanded as a result, the files after the first one are taken to be regular arguments to tar, not an option argument to -f.

Since you are using -x, tar is in extraction mode, and it is taking the other files to be the name of files to be extracted from the archive that it is operating on.

Upvotes: 3

Related Questions