mirror of
git://slackware.nl/current.git
synced 2024-12-26 09:58:59 +01:00
5a12e7c134
Wed Aug 26 10:00:38 CDT 2009 Slackware 13.0 x86_64 is released as stable! Thanks to everyone who helped make this release possible -- see the RELEASE_NOTES for the credits. The ISOs are off to the replicator. This time it will be a 6 CD-ROM 32-bit set and a dual-sided 32-bit/64-bit x86/x86_64 DVD. We're taking pre-orders now at store.slackware.com. Please consider picking up a copy to help support the project. Once again, thanks to the entire Slackware community for all the help testing and fixing things and offering suggestions during this development cycle. As always, have fun and enjoy! -P.
307 lines
12 KiB
HTML
307 lines
12 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org" />
|
|
<title>The Command Line</title>
|
|
<meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" />
|
|
<link rel="HOME" title="Slackware Linux Essentials" href="index.html" />
|
|
<link rel="UP" title="The Shell" href="shell.html" />
|
|
<link rel="PREVIOUS" title="The Shell" href="shell.html" />
|
|
<link rel="NEXT" title="The Bourne Again Shell (bash)" href="shell-bash.html" />
|
|
<link rel="STYLESHEET" type="text/css" href="docbook.css" />
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
|
</head>
|
|
<body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"
|
|
alink="#0000FF">
|
|
<div class="NAVHEADER">
|
|
<table summary="Header navigation table" width="100%" border="0" cellpadding="0"
|
|
cellspacing="0">
|
|
<tr>
|
|
<th colspan="3" align="center">Slackware Linux Essentials</th>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td width="10%" align="left" valign="bottom"><a href="shell.html"
|
|
accesskey="P">Prev</a></td>
|
|
<td width="80%" align="center" valign="bottom">Chapter 8 The Shell</td>
|
|
<td width="10%" align="right" valign="bottom"><a href="shell-bash.html"
|
|
accesskey="N">Next</a></td>
|
|
</tr>
|
|
</table>
|
|
|
|
<hr align="LEFT" width="100%" />
|
|
</div>
|
|
|
|
<div class="SECT1">
|
|
<h1 class="SECT1"><a id="SHELL-COMMAND-LINE" name="SHELL-COMMAND-LINE">8.2 The Command
|
|
Line</a></h1>
|
|
|
|
<div class="SECT2">
|
|
<h2 class="SECT2"><a id="SHELL-COMMAND-LINE-RUNNING"
|
|
name="SHELL-COMMAND-LINE-RUNNING">8.2.1 Running Programs</a></h2>
|
|
|
|
<p>It's hard to get much accomplished without running a program; you might be able to
|
|
prop something up with your computer or hold a door open, and some will make the most
|
|
lovely humming noise when running, but that's really about it. And I think we can all
|
|
agree that its use as a humming doorstop isn't what brought the personal computer the
|
|
popularity it now enjoys.</p>
|
|
|
|
<p>So, remember how almost everything in Linux is a file? Well, that goes for programs,
|
|
too. Every command you run (that isn't built into the shell) resides as a file somewhere.
|
|
You run a program simply by specifying the full path to it.</p>
|
|
|
|
<p>For instance, remember that <tt class="COMMAND">su</tt> command from the last section?
|
|
Well, it's actually in the <tt class="FILENAME">/bin</tt> directory: <tt
|
|
class="COMMAND">/bin/su</tt> would run it nicely.</p>
|
|
|
|
<p>So why, then, does just typing <tt class="COMMAND">su</tt> work? After all, you didn't
|
|
say it was in <tt class="FILENAME">/bin</tt>. It could just as easily have been in <tt
|
|
class="FILENAME">/usr/local/share</tt>, right? How did it <span class="emphasis"><i
|
|
class="EMPHASIS">know</i></span>? The answer to that lies in the <tt
|
|
class="ENVAR">PATH</tt> environment variable; most shells have either <tt
|
|
class="ENVAR">PATH</tt> or something very much like <tt class="ENVAR">PATH</tt>. It
|
|
basically contains a list of directories to look in for programs you try to run. So when
|
|
you ran <tt class="COMMAND">su</tt>, your shell ran through its list of directories,
|
|
checking each one for an executable file called <tt class="COMMAND">su</tt> that it could
|
|
run; the first one it came to, it ran. This happens whenever you run a program without
|
|
specifying a full path to it; if you get a ““<tt class="ERRORNAME">Command
|
|
not found</tt>”” error, that only means that the program you tried to run
|
|
isn't in your <tt class="ENVAR">PATH</tt>. (Of course, this would be true if the program
|
|
doesn't exist at all...) We'll discuss environment variables in more depth in <a
|
|
href="shell-bash.html#SHELL-BASH-ENVIRONMENT">Section 8.3.1</a>.</p>
|
|
|
|
<p>Remember also that “<tt class="FILENAME">.</tt>” is shorthand for the
|
|
current directory, so if you happened to be in <tt class="FILENAME">/bin</tt>, <tt
|
|
class="FILENAME">./su</tt> would have worked as an explicit full path.</p>
|
|
</div>
|
|
|
|
<div class="SECT2">
|
|
<h2 class="SECT2"><a id="SHELL-COMMAND-LINE-WILDCARD"
|
|
name="SHELL-COMMAND-LINE-WILDCARD">8.2.2 Wildcard Matching</a></h2>
|
|
|
|
<p>Nearly every shell recognizes some characters as being substitutes or abbreviations
|
|
that mean anything goes here. Such characters are aptly named wildcards; the most common
|
|
are <var class="LITERAL">*</var> and <var class="LITERAL">?</var>. By convention, <var
|
|
class="LITERAL">?</var> usually matches any single character. For instance, suppose
|
|
you're in a directory with three files: <tt class="FILENAME">ex1.txt</tt>, <tt
|
|
class="FILENAME">ex2.txt</tt>, and <tt class="FILENAME">ex3.txt</tt>. You want to copy
|
|
all of those files (using the <tt class="COMMAND">cp</tt> command we cover in <a
|
|
href="file-commands-copymove.html#FILE-COMMANDS-COPYMOVE-CP">Section 10.5.1</a>) to
|
|
another directory, say <tt class="FILENAME">/tmp</tt>. Well, typing <tt
|
|
class="COMMAND">cp ex1.txt ex2.txt ex3.txt /tmp</tt> is entirely too much work. It's much
|
|
easier to type <tt class="COMMAND">cp ex?.txt /tmp</tt>; the <var class="LITERAL">?</var>
|
|
will match each of the characters “1”, “2”, and “3”,
|
|
and each in turn will be substituted in.</p>
|
|
|
|
<p>What's that you say? That's <span class="emphasis"><i
|
|
class="EMPHASIS">still</i></span> too much work? You're right. It's appalling; we have
|
|
labor laws to protect us from that sort of thing. Fortunately, we also have <var
|
|
class="LITERAL">*</var>. As was already mentioned, <var class="LITERAL">*</var> matches
|
|
“any number of characters”, including 0. So if those three files were the
|
|
only ones in the directory, we could have simply said <tt class="COMMAND">cp * /tmp</tt>
|
|
and gotten them all in one fell swoop. Suppose, though, that there is also a file called
|
|
<tt class="FILENAME">ex.txt</tt> and one called <tt class="FILENAME">hejaz.txt</tt>. We
|
|
want to copy <tt class="FILENAME">ex.txt</tt> but not <tt
|
|
class="FILENAME">hejaz.txt</tt>; <tt class="COMMAND">cp ex* /tmp</tt> will do that for
|
|
us.</p>
|
|
|
|
<p><tt class="COMMAND">cp ex?.txt /tmp</tt>, would, of course, only get our original
|
|
three files; there's no character in <tt class="FILENAME">ex.txt</tt> to match that <var
|
|
class="LITERAL">?</var>, so it would be left out.</p>
|
|
|
|
<p>Another common wildcard is the bracket pair <var class="LITERAL">[ ]</var>. Any
|
|
characters inside the brackets will be substituted in place of the <var class="LITERAL">[
|
|
]</var> to find matches. Sound confusing? It's not too bad. Suppose for instance, we have
|
|
a directory containing the following 8 files: <tt class="FILENAME">a1</tt>, <tt
|
|
class="FILENAME">a2</tt>, <tt class="FILENAME">a3</tt>, <tt class="FILENAME">a4</tt>, <tt
|
|
class="FILENAME">aA</tt>, <tt class="FILENAME">aB</tt>, <tt class="FILENAME">aC</tt>, and
|
|
<tt class="FILENAME">aD</tt> . We want to only find the files ending in numbers; <var
|
|
class="LITERAL">[ ]</var> will do this for us.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls a[1-4]</kbd>
|
|
a1 a2 a3 a4
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>But what we really want is just <tt class="FILENAME">a1</tt>, <tt
|
|
class="FILENAME">a2</tt>, and <tt class="FILENAME">a4</tt>? In the previous example we
|
|
used - to mean all values between 1 and 4. We can also separate individual entries with
|
|
commas.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls a[1,2,4]</kbd>
|
|
a1 a2 a4
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>I know what you're thinking now, “Well what about letters?” Linux is
|
|
case-sensitive, meaning that <var class="LITERAL">a</var> and <var
|
|
class="LITERAL">A</var> are different characters and are only related in your mind.
|
|
Capitals always come before lowercase letters, so <var class="LITERAL">A</var> and <var
|
|
class="LITERAL">B</var> come before <var class="LITERAL">a</var> and <var
|
|
class="LITERAL">b</var>. Continuing with our earlier example, if we wanted files <tt
|
|
class="FILENAME">a1</tt>, and <tt class="FILENAME">A1</tt>, we can find these quickly
|
|
with <var class="LITERAL">[ ]</var>.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls [A,a]1</kbd>
|
|
A1 a1
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>Note, that if we had included a hyphen instead of a comma, we would have gotten
|
|
incorrect results.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls [A-a]1</kbd>
|
|
A1 B1 C1 D1 a1
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>You can also combine hyphen and comma strings.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ls [A,a-d]</kbd>
|
|
A1 a1 b1 c1 d1
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="SECT2">
|
|
<h2 class="SECT2"><a id="SHELL-COMMAND-LINE-PIPING"
|
|
name="SHELL-COMMAND-LINE-PIPING">8.2.3 Input/Output Redirection and Piping</a></h2>
|
|
|
|
<p>(Here comes something cool.)</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ps > blargh</kbd>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>Y'know what that is? That's me running <tt class="COMMAND">ps</tt> to see which
|
|
processes are running; <tt class="COMMAND">ps</tt> is covered in <a
|
|
href="process-control-ps.html">Section 11.3</a>. That's not the cool part. The cool part
|
|
is <var class="LITERAL">> blargh</var>, which means, roughly, take the output from <tt
|
|
class="COMMAND">ps</tt> and write it to a file called <tt class="FILENAME">blargh</tt>.
|
|
But wait, it gets cooler.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ps | less</kbd>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>That one takes the output from <tt class="COMMAND">ps</tt> and pipes it through <tt
|
|
class="COMMAND">less</tt>, so I can scroll through it at my leisure.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ps >> blargh</kbd>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>This is the third most commonly used redirector; it does the same thing as
|
|
“>”, except that “>>” will append output from <tt
|
|
class="COMMAND">ps</tt> to the file <tt class="FILENAME">blargh</tt>, if said file
|
|
exists. If not, just like “>”, it will be created. (“>”
|
|
will obliterate the current contents of <tt class="FILENAME">blargh</tt>.)</p>
|
|
|
|
<p>There is also a “<” operator, which means take your input from the
|
|
following, but it's not used nearly so often.</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd
|
|
class="USERINPUT">fromdos < dosfile.txt > unixfile.txt</kbd>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>Redirection gets really fun when you start piling it up:</p>
|
|
|
|
<table border="0" bgcolor="#E0E0E0" width="100%">
|
|
<tr>
|
|
<td>
|
|
<pre class="SCREEN">
|
|
<samp class="PROMPT">%</samp> <kbd class="USERINPUT">ps | tac >> blargh</kbd>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p>That will run <tt class="COMMAND">ps</tt>, reverse the lines of its output, and append
|
|
those to the file <tt class="FILENAME">blargh</tt>. You can stack as many of these up as
|
|
you want; just be careful to remember that they get interpreted from left to right.</p>
|
|
|
|
<p>See the <tt class="COMMAND">bash</tt>(1) man page for more detailed information on
|
|
redirection.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="NAVFOOTER">
|
|
<hr align="LEFT" width="100%" />
|
|
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0"
|
|
cellspacing="0">
|
|
<tr>
|
|
<td width="33%" align="left" valign="top"><a href="shell.html"
|
|
accesskey="P">Prev</a></td>
|
|
<td width="34%" align="center" valign="top"><a href="index.html"
|
|
accesskey="H">Home</a></td>
|
|
<td width="33%" align="right" valign="top"><a href="shell-bash.html"
|
|
accesskey="N">Next</a></td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td width="33%" align="left" valign="top">The Shell</td>
|
|
<td width="34%" align="center" valign="top"><a href="shell.html"
|
|
accesskey="U">Up</a></td>
|
|
<td width="33%" align="right" valign="top">The Bourne Again Shell (bash)</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
|