Linux Classes
Share This With a Friend  

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!)

Peter     (11 Oct 2012, 22:08)
What command would I use to find and remove all files in a directory (or multiple subdirectories) that would match with the file extension *.htm or *.html

I am trying to clean up some archives and I have literally thousands of directories I need to remove html (some of them .htm) and .php documents from.

Using Fedora 17/64, 8 gig ram with Sata 2 hard drives for what its worth.

Id prefer not to have to confirm every file deletion too. I don't have days to watch over it.

No scripts please.

Suggestions appreciated.

Thank you.

Jared     (11 Mar 2012, 20:10)
What Linux distro do you recommend using? (it seems that a distro without a GUI would be better for your tutorial). I know others have asked this but I didn't see a response. I'm currently using Ubuntu but a lot of the tutorials are not working correctly in Ubuntu's terminal. I cannot even access multiple terminals. I keep getting some sort of usb error.
steve     (13 Feb 2012, 04:24)
Bob, is it just my imagination or do you have to do some of this stuff differently in Ubuntu and Mint (based on Ubuntu). The feel I get from reading in the forums is that they have changed some things to make it easier as well as to 'protect the new user from himself'
Muralidhar M S     (03 Jan 2012, 07:30)

To remove every kind of file and not just text files, use the below command.

rm jan*

A file names three letter extension is also part of the file name in Unix. (File name extensions/suffixes have no meaning on Unix. They are simply part of the file name)
norm     (15 Jul 2011, 01:39)
Now can you tell us what we need to keep in mind when confronted by the bewildering array of terminals and shells available on linux.

My understanding is a shell is a program mainly concerned with interpreting command strings, and a
terminal or tty or getty is a program mainly concerned with emulating a particular hardware combination, namely a keyboard and its display device be it a line printer or video display.
If I'm right about this, what do we need to know about the choices we have?
Thanx Bob !
wala     (01 Jul 2010, 20:53)
The unhappy result there is that you never know which DOS commands will accept wildcards, and each program may interpret them differently--yuck!
Bob Rankin     (07 Jun 2010, 12:57)
That command should work. Although it's the same as:

rm jan*
gouri     (06 Jun 2010, 22:47)
respected bob, y isnt this possible ]
rm jan*.*
dont u think this would b useful in removin all files that start with jan & irrespective of their formats(.txt, .db, .doc)

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.