Linux Secrets: How to Use Wildcards with ls

Create Custom Search Patterns for ls Using Wildcard Characters

© Mark Alexander Bain

Dec 4, 2008
Linux Search Patterns on Linux, Mark Alexander Bain
Wildcard characters can be used with ls on Linux to provide a listing of the contents of a directory, but only selecting a very precise set of files.

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:

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:

$ ls -1
process_article
Using ls with Wildcards.html
writing_status
writing_status~

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:

$ ls -1 proc*
process_article

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:

$ ls -1 *stat*
writing_status
writing_status~

Or a file name starting with w, an underscore in it somewhere and ending in a tilde (~):

$ ls -1 w*_*~
writing_status~

Finally the search pattern may be one looking for a particular file type, for instance an html file:

$ ls -1 *.html
Using ls with Wildcards.html

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:

$ ls -1 writing_status?
writing_status~

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:

$ ls -1 *[Ww]*
Using ls with Wildcards.html
writing_status
writing_status~

It is also possible to search for names starting with a defined pattern, for instance any upper case letter:

$ ls -1 [A-Z]*
Using ls with Wildcards.html

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:

$ ls -1 *.[Hh]???
Using ls with Wildcards.html

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:

$ ls -1 *\ *
Using ls with Wildcards.html

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.


The copyright of the article Linux Secrets: How to Use Wildcards with ls in Linux Programming is owned by Mark Alexander Bain. Permission to republish Linux Secrets: How to Use Wildcards with ls in print or online must be granted by the author in writing.


Linux Search Patterns on Linux, Mark Alexander Bain
Use Wildcards to Produce a Precise Search Pattern, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo