How to Use Wildcards in Linux 


               


How to Use Wildcards in Linux



Linux


In this article, we show how to use wildcards in Linux.

Wildcards are used in the linux shell to work with specific files that match what we are searching for.

It's a way of combing through files and using code, kind of like regular expressions, in order to select certain files that we are looking for.

So how do wildcards work?

Before we go over actual Linux code in the shell, let's first go over some actual wildcards that are used.

The following table below shows various wildcards and what they match if used.

Wildcard Matches
* Any characters
? Any single character
[characters] Any character that is a member of the set characters
[!characters] Any character that is not a member of the set characters
[[:class:]] Any character that is a member of the specified class


So * means any characters. ? means just a single character.

Whenever we specify any characters within square brackets, this means that if any file matches any of the characters within the square brackets, it is a match.

If we want to return all files that do not have certain characters, you specify within square brackets ! followed by the characters. This will return all files that contain these prohibited characters.

At the last row of this table we have the class.

Now, there are a number of character classes in linux.

The table below lists the commonly used character classes in linux.

Character Class Matches
[:alnum:] Any alphanumeric character
[:alpha:] Any alphabetic character
[:digit:] Any numeral
[:lower:] Any lowercase letter
[:upper:] Any uppercase letter


All classes begin and end with colons (:)

The alnum class is characters that are alphanumerical.

The alpha class is characters that are alphabetical.

The digit class is characters that are numerical.

The lower class is lowercase characters.

The upper class is uppercase characters.

Lastly, we will go over examples of wildcards.

This is shown in the table below.

Pattern Matches
* All files
d* Any file beginning with d
c*.txt Any file beginning with c and that ends with .txt
dropbox??? Any file beginning with dropbox followedy by exactly 3 characters
[abc]* Any file beinning with either an a, b, or c
note[0-9] Any file beginning with note followed by a digit of 0-9
note[0-9][0-9][0-9] Any file beginning with note followed by 3 digits
[[:upper:]] Any file beginning with an uppercase letter
*[[:lower:]123] Any file ending with a lowercase letter or the numbers 1, 2, or 3


So these are a number of examples of wildcards.

So let's now go to the linux shell and see how this works.

The following code below finds all files that begins with d.



So the code above lists all files that begins with d.

Below is linux code that does uses various wildcard examples to select certain files that we want.

Wilcards in linux

So we first list all the files in the current working directory, so that you can see what is in there.

We then list all files that begin with the letter d, which is done by the line, ls d*

We then want to list all files that end with .php. We do this using the line, ls *.php

We then list all files that end with .html. We do this using the line, ls *.html

We then list all files that begin with note followed by a digit.

So this shows how to use wildcards in linux.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...