The contents of any Linux directory can be listed by using the ls command and it's very good at its job; however this useful tool can be improved by:
- customizing the ls output by using its many options (see Linux Secrets: How to Customize the ls Output)
- refine the file selection criteria by using Linux wildcards such as:
- the asterisk (*)
- the question mark (?)
- brackets ([])
Using ls Without Wildcards
The most common way to use ls is to view all of the files in a Linux directory - in this case the -1 option is used to view the files vertically:
In this case all of the files in the directory have been displayed; however this list can be filtered by introducing a search pattern.
Using an Asterisk in the ls Search Pattern
The asterisk (*) is used to represent a search pattern that contains anything; therefore to find any file with a name starting in proc, for example, the following would be entered onto the command line:
An asterisk can appear in multiple places in search string, and so the following would search for any files with stat anywhere in the name:
Or a file name starting with w, an underscore in it somewhere and ending in a tilde (~):
Finally the search pattern may be one looking for a particular file type, for instance an html file:
Using a Question Mark in the ls Search Pattern
The asterisk represents a search pattern containing any number of characters, however a question mark represents a search pattern containing one, and only one, character:
Using Brackets in the ls Search Pattern
Brackets, like question marks, are used to represent a single character; but the difference is that the brackets allow a choice of characters. The following will search for any file whose name contains either a upper or lower case W:
It is also possible to search for names starting with a defined pattern, for instance any upper case letter:
Although, it's worth noting that this particular pattern will only work with certain shells, for example it will work with Korn but not Bash.
Combining Asterisks, Brackets and Question Marks in the Same ls Search Pattern
Obviously it is possible to combine all of the wildcard characters in a search string that provide a very precise pattern - in this example a pattern to match the files with a suffix starting with upper or lower case H followed by any three characters:
Literal characters in the ls Search Pattern
Many characters (such as dollar signs and spaces) do not work within search strings because each has its own meaning; however, the trick is to turn the special character into a literal character by adding a backslash in front it; so the following would find any file with a space in its name:
Summary
A search pattern using wildcard characters can be added to the ls command; these wildcards are::
- * - searches for any pattern
- ? - searches for one, and only one, character
- [] - seaches for a single character, but allows a choice of characters
And, of course, all of the wildcards can be combined to provide a very precise search pattern.