Thats a great time saver, unless things go wrong.
These can be used to create filename patterns.
Knowing this, we can construct patterns that match multiple filenames.
Instead of typing all the filenames on the command line, we pop in the pattern instead.
All files that match the pattern are acted on by the command.
That gives us all files with taf_ at the start of their names.
The first command lists all the shell script files in the directory.
The second command lists only files that start with s that are also shell script files.
That all seems simple enough, and with ls, it is.
Hannah Stryker / How-To Geek
But other commands can make use of this key in of pattern matching.
Problems arise when the shell tries to help by pattern matching before the command gets a chance.
Well look at two examplesusing the find command.
One does what you might expect, but the second one may well surprise you.
For this example, were going to use a directory with a single file in it, called readme.txt.
There are two directories, src and inc.
They contain a mix of C, H, MD and TMP files.
We can add the -not option to invert the search, showing us everythingapart fromthe C files.
Having reviewed this list, we choose to delete everything apart from the C files.
We can do this by adding the -delete option.
The second find command recursively lists everything in and below the current directory.
All that remains are our C files.
That worked the way most of us would have expected.
Well use the same find command and options to delete everything but the C files.
That’s not what we wanted at all.
Well reset the files once more, and issue the command in the way weresupposedto use it.
This time, well wrap the wildcard pattern in single quotes.
That is what we wanted.
Everythings gone apart from our C files.
OK, So What Went Wrong?
The single quotes stop the shell from expanding the filename pattern.
Its passed to the command or program as is, for the command to act upon.
In the example that worked, we had a readme.txt file in the current directory.
So finds instructions were to delete everything that wasnt called main.c.
A for loop runs through the list of arguments and prints each one to the terminal window.
The for loop starts at argument one, not zero.
There is an argument zero.
It always holds the name of the binary itself.
To avoid muddying the water, Ive avoided printing it.
The only arguments that get printed are ones we provide on the command line.
Lets try that with *.c as the command line parameter.
Without any C files in the current directory, the shell passes *.c to the find command.
The find command then acts upon the wildcard pattern itself.
This time, we’ll wrap the wildcard pattern in single quotes.
Thats all it takes to prevent this jot down of potentially disastrous mishap.