UNIX / Linux Tutorial

1.8   Wildcards  |  1.9  Linux Plumbing  |  1.9.1 Standard Input And Standard Output
1.9.2  Redirecting Input And Output

1.8 Wildcards

A key feature of most Linux shells is the ability to refer to more than one file by name
using special characters. These wildcards let you refer to, say, all file names that contain
the character "n".

The wildcard "*" specifies any character or string of characters in a file name. When
you use the character "*" in a file name, the shell replaces it with all possible 
substitutions from file names in the directory you're referencing.

Here's a quick example. Suppose that Patrick has the files frog, joe and stuff in
his current directory:

                     /home/patrick# ls

                     frog  joe stuff

                     /home/patrick#

To specify all files containing the letter "o" in the filename, use the command:

                     /home/patrick# ls *f*

                     frog joe

                     /home/patrick#

As you can see, each instance of "*" is replaced with all substitutions that match the 
wild-card from filenames in the current directory.

The use of "*" by itself simply matches all filenames, because all characters match the
wildcard.

                     /home/patrick# ls *

                     frog  joe  stuff

                     /home/patrick#

Here are a few more examples:

                     /home/patrick# ls f*

                     frog

                     /home/patrick# ls *ff

                     stuff

                     /home/patrick# ls *f*

                     frog  stuff

                     /home/patrick# ls s*f

                     stuff

                     /home/patrick#

The process of changing a "*" into a series of filenames is called wildcard expansion
and is done by the shell. This is important: an individual command, such as ls, never sees
the "*" in its list of parameters. The shell expands the wildcard to include all 
filenames that match. So, the command:

                    /home/patrick# ls *o*

is expanded by the shell to:

                   /home/patrick# ls frog joe

One important note about the "*" wildcard: it does not match file names that begin
with a single period ("."). These files are treated as hidden files—while they are 
not really hidden, they don't show up on normal ls listings and aren't touched by the use of 
the "*" wildcard.

Here's an example. We mentioned earlier that each directory contains two special entries:
"." refers to the current directory, and ".." refers to the parent directory. 
However, when you use ls, these two entries don't show up:

                   /home/patrick# ls

                   frog joe stuff

                   /home/patrick#

If you use the -a switch with ls, however, you can display filenames that begin with "
.".
Observe:

                   /home/patrick#     ls     -a
                   . ..     .bash profile     .bashrc      frog        joe

                   stuff

                  /home/patrick#

The listing contains the two special entries, "."and"..", as well as two 
other "hidden" files—.bash profile and .bashrc. These two files are startup files 
used by bash when patrick logs in.

Note that when you use the "*" wildcard, none of the filenames beginning with "
."are displayed.

                 /home/patrick# ls *

                 frog joe stuff

                 /home/patrick#

This is a safety feature: if the "*" wildcard matched filenames beginning with 
".", it would also match the directory names "."and"..". This 
can be dangerous when using certain commands.
Another wildcard is "?". The "?" wildcard expands to only a single character.
Thus, "ls ?" displays all one-character filenames. And "ls termca?" 
would display:

"termcap"butnot "termcap.backup". Here's another example:



                /home/patrick# ls j?e

                joe

                /home/patrick# ls f??g

                frog

                /home/patrick# ls ????f

                stuff

                /home/patrick#

As you can see, wildcards lets you specify many files at one time. cp and mv commands 
actually can copy or move more than one file at a time. For example:

                /home/patrick# cp /etc/s* /home/patrick

copies all filenames in /etc beginning with "s" to the directory /home/patrick.
The format of the cp command is really:

                 cp     files       destination

where files lists the filenames to copy, and destination is the destination file or directory.
mv has an identical syntax.

If you are copying or moving more than one file, the destination must be a directory.
You can only copy or move a single file to another file.

1.9  Linux Plumbing

1.9.1 Standard Input And Standard Output

Many Linux commands get input from what is called standard input and send their
output to standard output (often abbreviated as stdin and stdout). Your shell sets
things up so that standard input is your keyboard, and standard output is the screen.
Here's an example using the cat command. Normally, cat reads data from all of the
files specified by the command line, and sends this data directly to stdout. Therefore,
using the command:

               /home/patrick/papers# cat history-final masters-thesis

displays the contents of the file history-final followed by masters-thesis.

However, if you don't specify a filename, cat reads data from stdin and sends it
back to stdout. Here's an example:

               /home/patrick/papers# cat

               Hello there.

               Hello there.

               Bye.

               Bye.

               Ctrl-D

              /home/patrick/papers#

Each line that you type is immediately echoed back by cat. When reading from standard
input, you indicate the input is "finished" by sending an EOT (end-of-text) signal, 
in general, generated by pressing Ctrl-D .

Here's another example. The sort command reads lines of text (again, from stdin,
unless you specify one or more filenames) and sends the sorted output to stdout. Try the
following:

             /home/patrick/papers# sort

             bananas

             carrots

             apples

             Ctrl-D

             apples

             bananas

             carrots

            /home/patrick/papers#

Now we can alphabetize our shopping list... isn't Linux useful?

1.9.2  Redirecting Input And Output

Now, let's say that you want to send the output of sort to a file, to save our shopping
list on disk. The shell lets you redirect standard output to a filename, by using the 
">" symbol. Here's how it works:

            /home/patrick/papers#   sort   >  > shopping - list

            bananas

            carrots

            apples

            Ctrl-D

           /home/patrick/papers#

As you can see, the result of the sort command isn't displayed, but is saved to the file
named shopping-list. Let's look at this file:

           /home/patrick/papers#  cat  shopping - list

           apples

           bananas

           carrots

           /home/patrick/papers#

Now you can sort your shopping list, and save it, too! But let's suppose that you are storing
the unsorted, original shopping list in the file items. One way of sorting the information
and saving it to a file would be to give sort the name of the file to read, in lieu of 
standard input, and redirect standard output as we did above, as follows:

          /home/patrick/papers#       sort items     >      shopping  -  list

          /home/patrick/papers# cat shopping - list

           apples

           bananas

           carrots

          /home/patrick/papers#

However, there's another way to do this. Not only can you redirect standard output, you
can redirect standard input as well, using the "<" symbol:

          /home/patrick/papers#   <  sort < items

           apples

           bananas

           carrots

          /home/patrick/papers#

Technically, sort < items is equivalent to sort items, but lets you demonstrate
the following point: sort < items behaves as if the data in the file items was typed
to standard input. The shell handles the redirection. sort wasn't given the name of the
file (items) to read; as far as sort is concerned, it still reads from standard input as if
you had typed the data from your keyboard.

This introduces the concept of a filter. A filter is a program that reads data from standard
input, processes it in some way, and sends the processed data to standard output. Using
redirection, standard input and standard output can be referenced from files. As mentioned
above, stdin and stdout default to the keyboard and screen respectively. sort is a
simple filter. It sorts the incoming data and sends the result to standard output. cat is even
simpler. It doesn't do anything with the incoming data, it simply outputs whatever is given
to it.

HOME

1.1 Introduction   1.2.10 Referring To Home Directories   1.3.4  Copying Files

1.6 Exploring The File System   1.8   Wildcards   1.9.3 Pipes   1.10.3 Permissions Dependencies

1.12.4  Stopping And Restarting Jobs   1.13.3 Inserting Text   1.13.9 Including Other Files

1.14.3 Shell Initialization Scripts   System Administration   2.3.1 The /etc/imitate file

2.4 Managing File Systems   2.6 Managing Users  2.6.5 Groups   2.7.2 gzip and compress

2.8.3 Making Backups To Tape Devices   2.9.1 Upgrading The Kernel   

2.9.3 Installing A Device Driver Module

BOOK: LINUX QUICK COMMAND REFERENCE

http://personal.atl.bellsouth.net/~psadler

© copyright KnowledgeWorks, Inc. (2001)