summaryrefslogtreecommitdiff
path: root/slackbook/html/shell-command-line.html
blob: 6188bd951640db629abec263ffc64c872c26a88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!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 &#8220;&#8220;<tt class="ERRORNAME">Command
not found</tt>&#8221;&#8221; 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 &#8220;<tt class="FILENAME">.</tt>&#8221; 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 &#8220;1&#8221;, &#8220;2&#8221;, and &#8220;3&#8221;,
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
&#8220;any number of characters&#8221;, 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, &#8220;Well what about letters?&#8221; 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 &#62; 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">&gt; 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 &gt;&gt; blargh</kbd>
</pre>
</td>
</tr>
</table>

<p>This is the third most commonly used redirector; it does the same thing as
&#8220;&gt;&#8221;, except that &#8220;&gt;&gt;&#8221; 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 &#8220;&gt;&#8221;, it will be created. (&#8220;&gt;&#8221;
will obliterate the current contents of <tt class="FILENAME">blargh</tt>.)</p>

<p>There is also a &#8220;&lt;&#8221; 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 &lt; dosfile.txt &gt; 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 &gt;&gt; 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>