Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Filename Expansion

Often you want a command to work with a group of files. Wildcards are used to create a filename expansion pattern: a series of characters and wildcards that expands to a list of filenames. For example, the pattern /etc/* expands to a list of all6.2 the files in /etc.

* is a wildcard that can stand for any series of characters, so the pattern /etc/* will expand to a list of all the filenames beginning with /etc/.

This filename list is most useful as a set of arguments for a command. For example, the /etc directory contains a series of subdirectories called rc0.d, rc1.d, etc. Normally to view the contents of these, you would type the following:

ls /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d

ls /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rcS.d

This is tedious. Instead, you can use the ? wildcard as shown here:

ls /etc/rc?.d
/etc/rc?.d expands to a list of filenames that begin with rc, followed by any single character, followed by .d.

Available wildcards include the following:

Matches any group of 0 or more characters.
Matches exactly one character.
If you enclose some characters in brackets, the result is a wildcard that matches those characters. For example, [abc] matches either a, or b, or c. If you add a ^ after the first bracket, the sense is reversed; so [^abc] matches any character that is not a, b, or c. You can include a range, such as [a-j], which matches anything between a and j. The match is case sensitive, so to allow any letter, you must use [a-zA-Z].
Expansion patterns are simple once you see some concrete examples:

This will give you a list of all filenames that end in .txt, since the * matches anything at all.
This gives a list of filenames that end in either .h or .c.
This gives you all three-letter filenames that begin with a.
This gives you all three-letter filenames that do not begin with a.
This gives you every filename that starts with a, regardless of how many letters it has.

John Goerzen / Ossama Othman

  Published under the terms of the GNU General Public License Design by Interspire