Skip to Main Content

How Docs

General

Use UNIX Filename Generation

NOTE: This document refers to software or hardware that is not currently supported by the University of Wyoming Information Technology Client Support Services. Although the information is believed to be correct, be aware that since no support is available, this information may be out-of-date or possibly no longer valid.

Introduction

Filename Generation is the same thing MS-DOS calls the use of wildcards. In UNIX this feature is also known as file globbing. The shell processes certain special characters, comparing them to the list of files in the current directory. Then the result is passed on to the actual UNIX command. For example, when you issue the command:

ls unix*.txt

and you have these files in your directory:

unix1.txt unix2.txt unix_is_fun.txt unix3.doc unix.txt

the shell replaces unix*.txt with unix1.txt unix2.txt unix_is_fun.txt unix.txt, and passes each file name that matched the pattern to the ls command. The system is fed the following commands:

ls unix1.txt
ls unix2.txt
ls unix_is_fun.txt
ls unix.txt

Procedure

Question Mark (?)
The question mark is replaced by any single character in a filename. In the command line

frontier:˜>ls memo?

the question mark will cause ls to list all filenames or directories which begin with the string "memo" and contain exactly one more character at the end of the string, whatever that character may be. For example, if your directory contained the files memo1, memo2, memo3, memo4 mamo1, mimo, m_mo and the directories momol and memok, ls displays the list:

memo1 memo2 memo3 memo4 memok

Note: If any of the files are directories, their contents will be listed as well.

If instead you replaced one of the characters within the word "memo" with the character "?":

frontier:˜>ls m?mo

you would receive a list of all filenames in your directory which started with the character 'm', ended with the characters 'mo', and had any single character in place of the question mark:

mamo mimo m_mo

Asterisk (*)
The asterisk, (*), replaces any number (including none) of characters, including numbers, in a filename. Thus a command line containing the file reference 'memo*', like:

frontier:˜>ls memo*

would list all filenames which begin 'memo', however many characters follow.
Note: In general, files which start with a dot (.) are not matched unless you explicitly start your file specification with a dot.

Square brackets ([ ])
Square brackets, ([ ]) define character classes. They get us a limited form of the question mark, allowing you to search for specific filenames by specifying a list or a range of characters. Appending "[1234]" or "[1-4]" to "memo" in the command line

frontier:˜>ls memo[1-4]

would display the filenames "memo1", "memo2", "memo3", and "memo4" from the list above. The brackets define a character class that includes all the characters listed within the brackets. Square brackets are especially useful when combined with other special characters. The command line

frontier:˜>ls [aeiou]*

would list all the files whose names begin with the vowels "a", "e", "i", "o", or "u". (Brackets are also useful when creating your own, more complex applications, since they enable you to feed the names of selected files to other utilities or programs one at a time.)

Parentheses ( )
Finally, parentheses, ( ), allow you to specify partial filenames. If you happened to be in a directory containing files numbered from 1 through 1000, for example, you could type:

frontier:˜>ls 4(3[5-9]?)

to get a listing of 435 through 439.

For more information, enter the following command at the shell prompt:

man zshexpn


 

Reviewed: 0706 By: CD

Additional help with the installation and configuration of
UW-supported software is available:
Faculty/Staff
Contact the IT Help Desk at 766-HELP (4357), option 1
Email UserHelp@uwyo.edu
Contact your IT user consultant
(www.uwyo.edu/infotech/services/helpdesk/uc/)

Students
Email ASU-IT@uwyo.edu
Contact the IT Help Desk at 766-HELP (4357), option 1
Come to the student computer lab in the lobby of the
Information Technology Center.

Footer Navigation

University of Wyoming
 
1000 E. University Ave. Laramie, WY 82071 // UW Operators (307) 766-1121 // Contact Us