Here are a couple of examples using braces to do pattern matching:
$ ls [abw]*
apple banana watermelon
$ ls [agw]*[ne]
apple grape watermelon
In the first example, any file beginning with
a
,
b
, or
w
is matched. In the second, any file that
begins with
a
,
g
, or
w
and also ends with either
n
or
e
is matched. You can also include ranges
within brackets. For example:
$ ls [a-g]*
apple banana grape grapefruit
Here, any filenames beginning with a letter from
a
through
g
are matched.
Using File-Redirection Metacharacters
Commands receive data from standard input and send it to standard output. Using pipes (described
earlier), you can direct standard output from one command to the standard input of another. With
files, you can use less than (
<
) and greater than (
>
) signs to direct data to and from files. Here are
the file-redirection characters:
<
-- Directs the contents of a file to the command. In most cases, this is the default action
expected by the command and the use of the character is optional; using
more bigfile
is the same as
more < bigfile
.
>
-- Directs the output of a command to a file, deleting the existing file.
>>
-- Directs the output of a command to a file, adding the output to the end of the
existing file.
Here are some examples of command lines where information is directed to and from files:
$ mail root < ~/.bashrc
$ man chmod | col -b > /tmp/chmod
$ echo "I finished the project on $(date)" >> ~/projects
In the first example, the contents of the
.bashrc
file in the home directory are sent in a mail mes-
sage to the computer's root user. The second command line formats the
chmod
man page (using the
man
command), removes extra back spaces (
col -b
), and sends the output to the file
/tmp/chmod
(erasing the previous
/tmp/chmod
file, if it exists). The final command results in the following text
being added to the user's project file:
I finished the project on Sat Jan 27 13:46:49 PST 2008
Understanding File Permissions
After you've worked with Linux for a while, you are almost sure to get a
Permission denied
message. Permissions associated with files and directories in Linux were designed to keep users
from accessing other users' private files and to protect important system files.
71
Running Commands from the Shell
2