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.

Terminal window showing the manual page for the Mutt email client on Linux

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 main page of the Homepage homelab dashboard with services running on it.

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.

Konsole Terminal open on the Kubuntu Focus Ir14 Linux laptop.

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.

A directory listing of a collection of various file types, in a terminal window.

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.

Using ls  in a terminal window, to select files that start with taf_.

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.

Using ls in a terminal window to select different groups of files, by using wildcards.

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.

A recursive ls directory listing showing subdirectories and files, in a terminal window.

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.

Using find in a terminal window to recursively find files with a C extension.

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.

Using find in a terminal window to recursively find files without a C extension.

Everythings gone apart from our C files.

OK, So What Went Wrong?

The single quotes stop the shell from expanding the filename pattern.

Using find in a terminal window to recursively delete files without a C extension.

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.

Using ls in a terminal window to recursively list files. There is a file called main.c in the current directory.

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.

Attemtping to use find in a terminal window to recursively delete files without a C extension. There is a file called main.c in the current directory.

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.

Using ls recursively in a terminal window to show the files and subdirectories. There is a file called main.c in the current directory.

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.

Using find with the wildcard enclosed in single quote marks, to recursively delete all files with a C extension.

This time, we’ll wrap the wildcard pattern in single quotes.

Thats all it takes to prevent this jot down of potentially disastrous mishap.

Using the example program to count and list command line arguments, in a terminal window.

Using the example program with a command parameter of *.c, without a C in the same directory.

Using the example program with a command parameter of *.c, with a C in the same directory.

Using the example program to count and display command paramters, when *.c is enclosed in single quotes.