Yale University Library

 

Workstation and Technology Services

Quick Links

Shell Metacharacters

You interact with the unix "kernel" using a "shell." There are several unix shells, but the most popular for interactive use are the Korn Shell and C Shell. Your default shell on the Library server is the Korn Shell. If you haven't reset it, that's what you're using. Unless you have deep attachments to the C Shell, you should stick with the Korn Shell. It doesn't matter too much when you're first starting out, but as you become more advanced, I think you'll find the Korn Shell easier to use.

As I travel around, I frequently see post-it notes with chmod 644 *.* written on them.

Does anyone know what this really means? Does it mean "change the permissions on every file in the directory to 644?" -- Nope! It really means change the permissions on every file with a dot in the name (and this doesn't include files that start with dot (hidden files)). I suspect the origin of this postit note is DOS wilcards (yuck!).

If you really want to change the permissions on every file in a directory, use chmod 644 *

This will change the permissions on every file at this level in the directory structure, and (watch out), this includes directories. On second thought, maybe chmod 644 *.* isn't so bad in a web environment, since most files have a dot in them--xxx.htm and most directories do not. :)

Just know what you are doing. Don't do stuff by rote. Be careful. Can anyone guess what rm * does?

Simple shell metacharacters

* Match any string of zero or more characters
? Match any single character
[abc...] Match any one of the enclosed characters (use a hyphen to specify a range: a-z, A-Z,0-9)
[!abc...] Match any one character NOT enclosed as above

Examples (and a tip to keep you from making a mess)

First the tip. If you're not sure what effect a metacharacter will have, test it on a "non-destructive" command like ls.

$ls -l f*

-rw-r--r-- 1 spaniel dogs 0 Jun 10 10:30 fish.htm
-rw-r----- 1 spaniel dogs 27644 Jun 10 10:31 ftp2.htm

Now that I know it will match the 2 files I want, I can run chmod on it.

$chmod 644 f*

How about:
$ls [fd]*
dog.htm fish.htm ftp2.html

Matches any file that starts with d or f followed by 0 or more characters.

This one will work in most situations to match any file ending in .htm and .html
$ls -l *.htm*
-rw-r--r-- 1 spaniel dogs 0 Jun 10 10:29 cat.htm
-rw-r--r-- 1 spaniel dogs 0 Jun 10 10:29 dog.htm
-rw-r--r-- 1 spaniel dogs 0 Jun 10 10:30 fish.htm
-rw-r--r-- 1 spaniel dogs 27644 Jun 10 10:31 ftp2.html

But what about this (admittedly forced) example?
$ls -l *.htm*
-rw-r--r-- 1 spaniel dogs 0 Jun 10 11:24 dog.htm.jpc
-rw-r--r-- 1 spaniel dogs 0 Jun 10 10:30 fish.htm
-rw-r--r-- 1 spaniel dogs 27644 Jun 10 10:31 ftp2.html

I only wanted to get files ending in .htm and .html. I got too much. This is why you should test commands you aren't sure about first.

There's actually a Korn Shell (only) metacharacter construction that would avoid this, but that's probably drifting too far off for this class:
ls -l *.htm?(l)
(Match 0 or more characters, followed by a dot, followed by htm, followed by 1 or 0 instances of the letter l)

As you might imagine, there are lots of things you can do with metacharacters and the shell besides list and change permissions


Revised 6/19/98