Can I Use Wildcards with Linux?


Wildcards come in handy when you want to perform an operation on a group of files. As with DOS or Windows, if you want to delete all your files that start with "jan" and end with "txt," you can use the asterisk (*) wildcard character, as in rm j an*txt, to delete all such files (rm is the command you use to delete files). The * character tells the shell to find any files that begin with "jan" and end with "txt" regardless of the number of characters between. It will even find files with no characters between, like jantxt. Thus, a file named jan-9 6.txt would be deleted, as would jantxt.

A more restrictive wildcard is the question mark, which matches any single character. Here are some examples of its use:

rm jan-8?.txt deletes jan-81.txt and jan-82.txt, but not jan-89b.txt

rm jan-95.??? deletes jan-95.txt and jan-95.dat, but not jan-95.db

When you use wildcards, the shell finds all matching files and expands the command you entered, so the rm jan-95.* command would be the same as typing

rm jan-95.txt jan-95.dat jan-95.db

Programmers take note: In Linux, the shell performs wildcard interpretation, and the actual commands (programs) never see the wildcard characters. This provides a convenient and common way for all Linux programs to handle wildcards. In DOS, the program (not the shell) must have the intelligence to handle wildcards. The unhappy result there is that you never know which DOS commands will accept wildcards, and each program may interpret them differently--yuck!

Previous Lesson: The Command Prompt
Next Lesson: Command History



Comments - most recent first
(Please feel free to answer questions posted by others!)

No comments yet

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.