1 <html> |
1 <html> |
2 <head> |
2 <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Chapter. Section_</title><link rel="stylesheet" href="/review/support/styles.css" type="text/css" /><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /><script type="text/javascript" src="/review/support/jquery-min.js"></script><script type="text/javascript" src="/review/support/form.js"></script><script type="text/javascript" src="/review/support/hsbook.js"></script></head> |
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
3 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch11Section_5"> |
4 <title> |
4 <div class="titlepage"></div> |
5 Chapter. Section_ |
5 <div class="toc"> |
6 </title> |
6 <p><b>Table of Contents</b></p> |
7 <link rel="stylesheet" href="/review/support/styles.css" type="text/css" /> |
7 <dl> |
8 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /> |
8 <dt><span class="article"><a href="#id2647624"></a></span></dt> |
9 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /> |
9 <dd><dl> |
10 <script type="text/javascript" src="/review/support/jquery-min.js"> |
10 <dt><span class="section"><a href="#id2457715">1. Module Objectives:</a></span></dt> |
11 </script> |
11 <dt><span class="section"><a href="#id2645976">2. tar:</a></span></dt> |
12 <script type="text/javascript" src="/review/support/form.js"> |
12 <dd><dl> |
13 </script> |
13 <dt><span class="section"><a href="#id2646529">2.1. Introduction:</a></span></dt> |
14 <script type="text/javascript" src="/review/support/hsbook.js"> |
14 <dt><span class="section"><a href="#id2698596">2.2. Getting Started(go go go!):</a></span></dt> |
15 </script> |
15 <dt><span class="section"><a href="#id2698875">2.3. Further Reading for this section:</a></span></dt> |
16 </head> |
16 </dl></dd> |
17 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
17 <dt><span class="section"><a href="#id2698920">3. GZip:</a></span></dt> |
18 <div class="chapter" id="chn11Section_5"> |
18 <dd><dl> |
19 <div class="titlepage"> |
19 <dt><span class="section"><a href="#id2698946">3.1. Get going:</a></span></dt> |
20 </div> |
20 <dt><span class="section"><a href="#id2699199">3.2. Further Reading for this section:</a></span></dt> |
21 <div class="toc"> |
21 </dl></dd> |
22 <p> |
22 <dt><span class="section"><a href="#id2699253">4. File Comparisons:</a></span></dt> |
23 <b> |
23 <dd><dl> |
24 Table of Contents |
24 <dt><span class="section"><a href="#id2699270">4.1. cmp:</a></span></dt> |
25 </b> |
25 <dt><span class="section"><a href="#id2699312">4.2. diff:</a></span></dt> |
26 </p> |
26 <dt><span class="section"><a href="#id2699394">4.3. comm:</a></span></dt> |
27 <dl> |
27 </dl></dd> |
28 <dt> |
28 <dt><span class="section"><a href="#id2699424">5. Environment Variables:</a></span></dt> |
29 <span class="article"> |
29 <dd><dl><dt><span class="section"><a href="#id2699524">5.1. Further Reading:</a></span></dt></dl></dd> |
30 <a href="#id2886967"> |
30 <dt><span class="section"><a href="#id2699578">6. Shell Scripting:</a></span></dt> |
31 </a> |
31 <dd><dl> |
32 </span> |
32 <dt><span class="section"><a href="#id2699586">6.1. Basics:</a></span></dt> |
33 </dt> |
33 <dt><span class="section"><a href="#id2697944">6.2. Shell Arithmetic:</a></span></dt> |
34 <dd> |
34 <dt><span class="section"><a href="#id2699963">6.3. if else construct:</a></span></dt> |
35 <dl> |
35 <dt><span class="section"><a href="#id2700069">6.4. Loops</a></span></dt> |
36 <dt> |
36 <dt><span class="section"><a href="#id2700315">6.5. Functions</a></span></dt> |
37 <span class="section"> |
37 <dt><span class="section"><a href="#id2700386">6.6. Further Reading:</a></span></dt> |
38 <a href="#id2697058"> |
38 </dl></dd> |
39 1. Module Objectives: |
39 </dl></dd> |
40 </a> |
40 </dl> |
41 </span> |
41 </div> |
42 </dt> |
42 <div class="article"> |
43 <dt> |
43 <div class="titlepage"> |
44 <span class="section"> |
44 <div> |
45 <a href="#id2885319"> |
45 <div><h2 class="title" id="id2647624"></h2></div> |
46 2. tar: |
46 <div><h2 class="title" id="id2647624"></h2></div> |
47 </a> |
47 </div> |
48 </span> |
48 <hr /> |
49 </dt> |
49 </div> |
50 <dd> |
50 <div class="toc"> |
51 <dl> |
51 <p><b>Table of Contents</b></p> |
52 <dt> |
52 <dl> |
53 <span class="section"> |
53 <dt><span class="section"><a href="#id2457715">1. Module Objectives:</a></span></dt> |
54 <a href="#id2885872"> |
54 <dt><span class="section"><a href="#id2645976">2. tar:</a></span></dt> |
55 2.1. Introduction: |
55 <dd><dl> |
56 </a> |
56 <dt><span class="section"><a href="#id2646529">2.1. Introduction:</a></span></dt> |
57 </span> |
57 <dt><span class="section"><a href="#id2698596">2.2. Getting Started(go go go!):</a></span></dt> |
58 </dt> |
58 <dt><span class="section"><a href="#id2698875">2.3. Further Reading for this section:</a></span></dt> |
59 <dt> |
59 </dl></dd> |
60 <span class="section"> |
60 <dt><span class="section"><a href="#id2698920">3. GZip:</a></span></dt> |
61 <a href="#id2937939"> |
61 <dd><dl> |
62 2.2. Getting Started(go go go!): |
62 <dt><span class="section"><a href="#id2698946">3.1. Get going:</a></span></dt> |
63 </a> |
63 <dt><span class="section"><a href="#id2699199">3.2. Further Reading for this section:</a></span></dt> |
64 </span> |
64 </dl></dd> |
65 </dt> |
65 <dt><span class="section"><a href="#id2699253">4. File Comparisons:</a></span></dt> |
66 <dt> |
66 <dd><dl> |
67 <span class="section"> |
67 <dt><span class="section"><a href="#id2699270">4.1. cmp:</a></span></dt> |
68 <a href="#id2938218"> |
68 <dt><span class="section"><a href="#id2699312">4.2. diff:</a></span></dt> |
69 2.3. Further Reading for this section: |
69 <dt><span class="section"><a href="#id2699394">4.3. comm:</a></span></dt> |
70 </a> |
70 </dl></dd> |
71 </span> |
71 <dt><span class="section"><a href="#id2699424">5. Environment Variables:</a></span></dt> |
72 </dt> |
72 <dd><dl><dt><span class="section"><a href="#id2699524">5.1. Further Reading:</a></span></dt></dl></dd> |
73 </dl> |
73 <dt><span class="section"><a href="#id2699578">6. Shell Scripting:</a></span></dt> |
74 </dd> |
74 <dd><dl> |
75 <dt> |
75 <dt><span class="section"><a href="#id2699586">6.1. Basics:</a></span></dt> |
76 <span class="section"> |
76 <dt><span class="section"><a href="#id2697944">6.2. Shell Arithmetic:</a></span></dt> |
77 <a href="#id2938263"> |
77 <dt><span class="section"><a href="#id2699963">6.3. if else construct:</a></span></dt> |
78 3. GZip: |
78 <dt><span class="section"><a href="#id2700069">6.4. Loops</a></span></dt> |
79 </a> |
79 <dt><span class="section"><a href="#id2700315">6.5. Functions</a></span></dt> |
80 </span> |
80 <dt><span class="section"><a href="#id2700386">6.6. Further Reading:</a></span></dt> |
81 </dt> |
81 </dl></dd> |
82 <dd> |
82 </dl> |
83 <dl> |
83 </div> |
84 <dt> |
84 <div class="section" title="1.Module Objectives:"> |
85 <span class="section"> |
85 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
86 <a href="#id2938288"> |
86 <a name="id2457715"></a>1.Module Objectives:</h2></div></div></div> |
87 3.1. Get going: |
87 <p id="ch11Section_5_1">After successfully completing this module a participant will be able to:</p> |
88 </a> |
88 <pre class="programlisting"> |
89 </span> |
89 - Understand |
90 </dt> |
|
91 <dt> |
|
92 <span class="section"> |
|
93 <a href="#id2938542"> |
|
94 3.2. Further Reading for this section: |
|
95 </a> |
|
96 </span> |
|
97 </dt> |
|
98 </dl> |
|
99 </dd> |
|
100 <dt> |
|
101 <span class="section"> |
|
102 <a href="#id2938596"> |
|
103 4. File Comparisons: |
|
104 </a> |
|
105 </span> |
|
106 </dt> |
|
107 <dd> |
|
108 <dl> |
|
109 <dt> |
|
110 <span class="section"> |
|
111 <a href="#id2938613"> |
|
112 4.1. cmp: |
|
113 </a> |
|
114 </span> |
|
115 </dt> |
|
116 <dt> |
|
117 <span class="section"> |
|
118 <a href="#id2938655"> |
|
119 4.2. diff: |
|
120 </a> |
|
121 </span> |
|
122 </dt> |
|
123 <dt> |
|
124 <span class="section"> |
|
125 <a href="#id2938737"> |
|
126 4.3. comm: |
|
127 </a> |
|
128 </span> |
|
129 </dt> |
|
130 </dl> |
|
131 </dd> |
|
132 <dt> |
|
133 <span class="section"> |
|
134 <a href="#id2938767"> |
|
135 5. Environment Variables: |
|
136 </a> |
|
137 </span> |
|
138 </dt> |
|
139 <dd> |
|
140 <dl> |
|
141 <dt> |
|
142 <span class="section"> |
|
143 <a href="#id2938866"> |
|
144 5.1. Further Reading: |
|
145 </a> |
|
146 </span> |
|
147 </dt> |
|
148 </dl> |
|
149 </dd> |
|
150 <dt> |
|
151 <span class="section"> |
|
152 <a href="#id2938921"> |
|
153 6. Shell Scripting: |
|
154 </a> |
|
155 </span> |
|
156 </dt> |
|
157 <dd> |
|
158 <dl> |
|
159 <dt> |
|
160 <span class="section"> |
|
161 <a href="#id2938929"> |
|
162 6.1. Basics: |
|
163 </a> |
|
164 </span> |
|
165 </dt> |
|
166 <dt> |
|
167 <span class="section"> |
|
168 <a href="#id2937287"> |
|
169 6.2. Shell Arithmetic: |
|
170 </a> |
|
171 </span> |
|
172 </dt> |
|
173 <dt> |
|
174 <span class="section"> |
|
175 <a href="#id2939306"> |
|
176 6.3. if else construct: |
|
177 </a> |
|
178 </span> |
|
179 </dt> |
|
180 <dt> |
|
181 <span class="section"> |
|
182 <a href="#id2939412"> |
|
183 6.4. Loops |
|
184 </a> |
|
185 </span> |
|
186 </dt> |
|
187 <dt> |
|
188 <span class="section"> |
|
189 <a href="#id2939658"> |
|
190 6.5. Functions |
|
191 </a> |
|
192 </span> |
|
193 </dt> |
|
194 <dt> |
|
195 <span class="section"> |
|
196 <a href="#id2939729"> |
|
197 6.6. Further Reading: |
|
198 </a> |
|
199 </span> |
|
200 </dt> |
|
201 </dl> |
|
202 </dd> |
|
203 </dl> |
|
204 </dd> |
|
205 </dl> |
|
206 </div> |
|
207 <div class="article"> |
|
208 <div class="titlepage"> |
|
209 <div> |
|
210 <div> |
|
211 <h2 class="title" id="id2886967"> |
|
212 </h2> |
|
213 </div> |
|
214 <div> |
|
215 <h2 class="title" id="id2886967"> |
|
216 </h2> |
|
217 </div> |
|
218 </div> |
|
219 <hr /> |
|
220 </div> |
|
221 <div class="toc"> |
|
222 <p> |
|
223 <b> |
|
224 Table of Contents |
|
225 </b> |
|
226 </p> |
|
227 <dl> |
|
228 <dt> |
|
229 <span class="section"> |
|
230 <a href="#id2697058"> |
|
231 1. Module Objectives: |
|
232 </a> |
|
233 </span> |
|
234 </dt> |
|
235 <dt> |
|
236 <span class="section"> |
|
237 <a href="#id2885319"> |
|
238 2. tar: |
|
239 </a> |
|
240 </span> |
|
241 </dt> |
|
242 <dd> |
|
243 <dl> |
|
244 <dt> |
|
245 <span class="section"> |
|
246 <a href="#id2885872"> |
|
247 2.1. Introduction: |
|
248 </a> |
|
249 </span> |
|
250 </dt> |
|
251 <dt> |
|
252 <span class="section"> |
|
253 <a href="#id2937939"> |
|
254 2.2. Getting Started(go go go!): |
|
255 </a> |
|
256 </span> |
|
257 </dt> |
|
258 <dt> |
|
259 <span class="section"> |
|
260 <a href="#id2938218"> |
|
261 2.3. Further Reading for this section: |
|
262 </a> |
|
263 </span> |
|
264 </dt> |
|
265 </dl> |
|
266 </dd> |
|
267 <dt> |
|
268 <span class="section"> |
|
269 <a href="#id2938263"> |
|
270 3. GZip: |
|
271 </a> |
|
272 </span> |
|
273 </dt> |
|
274 <dd> |
|
275 <dl> |
|
276 <dt> |
|
277 <span class="section"> |
|
278 <a href="#id2938288"> |
|
279 3.1. Get going: |
|
280 </a> |
|
281 </span> |
|
282 </dt> |
|
283 <dt> |
|
284 <span class="section"> |
|
285 <a href="#id2938542"> |
|
286 3.2. Further Reading for this section: |
|
287 </a> |
|
288 </span> |
|
289 </dt> |
|
290 </dl> |
|
291 </dd> |
|
292 <dt> |
|
293 <span class="section"> |
|
294 <a href="#id2938596"> |
|
295 4. File Comparisons: |
|
296 </a> |
|
297 </span> |
|
298 </dt> |
|
299 <dd> |
|
300 <dl> |
|
301 <dt> |
|
302 <span class="section"> |
|
303 <a href="#id2938613"> |
|
304 4.1. cmp: |
|
305 </a> |
|
306 </span> |
|
307 </dt> |
|
308 <dt> |
|
309 <span class="section"> |
|
310 <a href="#id2938655"> |
|
311 4.2. diff: |
|
312 </a> |
|
313 </span> |
|
314 </dt> |
|
315 <dt> |
|
316 <span class="section"> |
|
317 <a href="#id2938737"> |
|
318 4.3. comm: |
|
319 </a> |
|
320 </span> |
|
321 </dt> |
|
322 </dl> |
|
323 </dd> |
|
324 <dt> |
|
325 <span class="section"> |
|
326 <a href="#id2938767"> |
|
327 5. Environment Variables: |
|
328 </a> |
|
329 </span> |
|
330 </dt> |
|
331 <dd> |
|
332 <dl> |
|
333 <dt> |
|
334 <span class="section"> |
|
335 <a href="#id2938866"> |
|
336 5.1. Further Reading: |
|
337 </a> |
|
338 </span> |
|
339 </dt> |
|
340 </dl> |
|
341 </dd> |
|
342 <dt> |
|
343 <span class="section"> |
|
344 <a href="#id2938921"> |
|
345 6. Shell Scripting: |
|
346 </a> |
|
347 </span> |
|
348 </dt> |
|
349 <dd> |
|
350 <dl> |
|
351 <dt> |
|
352 <span class="section"> |
|
353 <a href="#id2938929"> |
|
354 6.1. Basics: |
|
355 </a> |
|
356 </span> |
|
357 </dt> |
|
358 <dt> |
|
359 <span class="section"> |
|
360 <a href="#id2937287"> |
|
361 6.2. Shell Arithmetic: |
|
362 </a> |
|
363 </span> |
|
364 </dt> |
|
365 <dt> |
|
366 <span class="section"> |
|
367 <a href="#id2939306"> |
|
368 6.3. if else construct: |
|
369 </a> |
|
370 </span> |
|
371 </dt> |
|
372 <dt> |
|
373 <span class="section"> |
|
374 <a href="#id2939412"> |
|
375 6.4. Loops |
|
376 </a> |
|
377 </span> |
|
378 </dt> |
|
379 <dt> |
|
380 <span class="section"> |
|
381 <a href="#id2939658"> |
|
382 6.5. Functions |
|
383 </a> |
|
384 </span> |
|
385 </dt> |
|
386 <dt> |
|
387 <span class="section"> |
|
388 <a href="#id2939729"> |
|
389 6.6. Further Reading: |
|
390 </a> |
|
391 </span> |
|
392 </dt> |
|
393 </dl> |
|
394 </dd> |
|
395 </dl> |
|
396 </div> |
|
397 <div class="section" title="1.Module Objectives:"> |
|
398 <div class="titlepage"> |
|
399 <div> |
|
400 <div> |
|
401 <h2 class="title" style="clear: both"> |
|
402 <a name="id2697058"> |
|
403 </a> |
|
404 1.Module Objectives: |
|
405 </h2> |
|
406 </div> |
|
407 </div> |
|
408 </div> |
|
409 <p id="ch11Section_5_1"> |
|
410 After successfully completing this module a participant will be able to: |
|
411 </p> |
|
412 <pre class="programlisting"> |
|
413 - Understand |
|
414 * What are archives and zipped files U |
90 * What are archives and zipped files U |
415 * What are environment variables U |
91 * What are environment variables U |
416 * What are Shell Scripts U |
92 * What are Shell Scripts U |
417 - Able to use file comparison commands like Ap |
93 - Able to use file comparison commands like Ap |
418 diff, cmp, comm |
94 diff, cmp, comm |
419 - Create and extract archives(.tar files) and zipped files(.gz) Ap |
95 - Create and extract archives(.tar files) and zipped files(.gz) Ap |
420 - Set/Modify environment as per need Ap |
96 - Set/Modify environment as per need Ap |
421 - Create shell scripts to automate tasks. Ap |
97 - Create shell scripts to automate tasks. Ap</pre> |
422 </pre> |
98 </div> |
423 </div> |
99 <div class="section" title="2.tar:"> |
424 <div class="section" title="2.tar:"> |
100 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
425 <div class="titlepage"> |
101 <a name="id2645976"></a>2.tar:</h2></div></div></div> |
426 <div> |
102 <div class="section" title="2.1.Introduction:"> |
427 <div> |
103 <div class="titlepage"><div><div><h3 class="title"> |
428 <h2 class="title" style="clear: both"> |
104 <a name="id2646529"></a>2.1.Introduction:</h3></div></div></div> |
429 <a name="id2885319"> |
105 <p id="ch11Section_5_2">In world of Linux based distribution, <span class="emphasis"><em>tarballs</em></span> is the term which pops up very often. It is part of the GNU project and comes as part of every distribution of GNU/Linux. Tarball is like defacto standard for releasing source code for free software. Some of common use of <span class="emphasis"><em>tar</em></span> archives is to: <span class="emphasis"><em>Store, backup, and transport</em></span>.</p> |
430 </a> |
106 <p id="ch11Section_5_3">GNU tar creates and manipulates archives which are actually collections of many other files; the program provides users with an organized and systematic method for controlling a large amount of data. It is basically form of creating archive by concatenating one or more files.</p> |
431 2.tar: |
107 </div> |
432 </h2> |
108 <div class="section" title="2.2.Getting Started(go go go!):"> |
433 </div> |
109 <div class="titlepage"><div><div><h3 class="title"> |
434 </div> |
110 <a name="id2698596"></a>2.2.Getting Started(go go go!):</h3></div></div></div> |
435 </div> |
111 <p id="ch11Section_5_4">As mentioned previously and if not, <span class="emphasis"><em>The best way to get started with any command line tool of Linux is to use "man".</em></span></p> |
436 <div class="section" title="2.1.Introduction:"> |
112 <pre class="programlisting"> |
437 <div class="titlepage"> |
113 $ man tar</pre> |
438 <div> |
114 <p id="ch11Section_5_5">or try these commands(the output may vary with different installations):</p> |
439 <div> |
115 <pre class="programlisting"> |
440 <h3 class="title"> |
116 $ tar --version |
441 <a name="id2885872"> |
|
442 </a> |
|
443 2.1.Introduction: |
|
444 </h3> |
|
445 </div> |
|
446 </div> |
|
447 </div> |
|
448 <p id="ch11Section_5_2"> |
|
449 In world of Linux based distribution, |
|
450 <span class="emphasis"> |
|
451 <em> |
|
452 tarballs |
|
453 </em> |
|
454 </span> |
|
455 is the term which pops up very often. It is part of the GNU project and comes as part of every distribution of GNU/Linux. Tarball is like defacto standard for releasing source code for free software. Some of common use of |
|
456 <span class="emphasis"> |
|
457 <em> |
|
458 tar |
|
459 </em> |
|
460 </span> |
|
461 archives is to: |
|
462 <span class="emphasis"> |
|
463 <em> |
|
464 Store, backup, and transport |
|
465 </em> |
|
466 </span> |
|
467 . |
|
468 </p> |
|
469 <p id="ch11Section_5_3"> |
|
470 GNU tar creates and manipulates archives which are actually collections of many other files; the program provides users with an organized and systematic method for controlling a large amount of data. It is basically form of creating archive by concatenating one or more files. |
|
471 </p> |
|
472 </div> |
|
473 <div class="section" title="2.2.Getting Started(go go go!):"> |
|
474 <div class="titlepage"> |
|
475 <div> |
|
476 <div> |
|
477 <h3 class="title"> |
|
478 <a name="id2937939"> |
|
479 </a> |
|
480 2.2.Getting Started(go go go!): |
|
481 </h3> |
|
482 </div> |
|
483 </div> |
|
484 </div> |
|
485 <p id="ch11Section_5_4"> |
|
486 As mentioned previously and if not, |
|
487 <span class="emphasis"> |
|
488 <em> |
|
489 The best way to get started with any command line tool of Linux is to use "man". |
|
490 </em> |
|
491 </span> |
|
492 </p> |
|
493 <pre class="programlisting"> |
|
494 $ man tar |
|
495 </pre> |
|
496 <p id="ch11Section_5_5"> |
|
497 or try these commands(the output may vary with different installations): |
|
498 </p> |
|
499 <pre class="programlisting"> |
|
500 $ tar --version |
|
501 tar (GNU tar) 1.20 |
117 tar (GNU tar) 1.20 |
502 Copyright (C) 2008 Free Software Foundation, Inc. |
118 Copyright (C) 2008 Free Software Foundation, Inc. |
503 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
119 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
504 This is free software: you are free to change and redistribute it. |
120 This is free software: you are free to change and redistribute it. |
505 There is NO WARRANTY, to the extent permitted by law. |
121 There is NO WARRANTY, to the extent permitted by law. |
512 restore individual files from the archive. |
128 restore individual files from the archive. |
513 Examples: |
129 Examples: |
514 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. |
130 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. |
515 tar -tvf archive.tar # List all files in archive.tar verbosely. |
131 tar -tvf archive.tar # List all files in archive.tar verbosely. |
516 tar -xf archive.tar # Extract all files from archive.tar. |
132 tar -xf archive.tar # Extract all files from archive.tar. |
517 ____________ |
133 ____________</pre> |
518 </pre> |
134 <div class="section" title="2.2.1.Creating a tar archive:"> |
519 <div class="section" title="2.2.1.Creating a tar archive:"> |
135 <div class="titlepage"><div><div><h4 class="title"> |
520 <div class="titlepage"> |
136 <a name="id2698650"></a>2.2.1.Creating a tar archive:</h4></div></div></div> |
521 <div> |
137 <p id="ch11Section_5_6">We will do some off-the road activity for this exercise. We will use an interesting command <span class="emphasis"><em>fortune</em></span> for creating our practice files and then performing archives of those files and directories. Content of the files would vary for users, as fortune works like that.</p> |
522 <div> |
138 <pre class="programlisting"> |
523 <h4 class="title"> |
139 $ mkdir fortune-files |
524 <a name="id2937993"> |
|
525 </a> |
|
526 2.2.1.Creating a tar archive: |
|
527 </h4> |
|
528 </div> |
|
529 </div> |
|
530 </div> |
|
531 <p id="ch11Section_5_6"> |
|
532 We will do some off-the road activity for this exercise. We will use an interesting command |
|
533 <span class="emphasis"> |
|
534 <em> |
|
535 fortune |
|
536 </em> |
|
537 </span> |
|
538 for creating our practice files and then performing archives of those files and directories. Content of the files would vary for users, as fortune works like that. |
|
539 </p> |
|
540 <pre class="programlisting"> |
|
541 $ mkdir fortune-files |
|
542 $ cd fortune-files/ |
140 $ cd fortune-files/ |
543 $ fortune > first.txt |
141 $ fortune > first.txt |
544 $ cat first.txt |
142 $ cat first.txt |
545 Expect the worst, it's the least you can do. |
143 Expect the worst, it's the least you can do. |
546 $ fortune > second.txt |
144 $ fortune > second.txt |
547 $ fortune > third.txt |
145 $ fortune > third.txt |
548 $ ls |
146 $ ls |
549 first.txt second.txt third.txt |
147 first.txt second.txt third.txt</pre> |
550 </pre> |
148 <p id="ch11Section_5_7">By now we have three txt files, with some random fortune content. To create a tar archive of these files we can use any of following commands according to ones convenience:</p> |
551 <p id="ch11Section_5_7"> |
149 <pre class="programlisting"> |
552 By now we have three txt files, with some random fortune content. To create a tar archive of these files we can use any of following commands according to ones convenience: |
150 $ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt |
553 </p> |
|
554 <pre class="programlisting"> |
|
555 $ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt |
|
556 first.txt |
151 first.txt |
557 second.txt |
152 second.txt |
558 third.txt |
153 third.txt |
559 $ ls |
154 $ ls |
560 allfiles.tar first.txt second.txt third.txt |
155 allfiles.tar first.txt second.txt third.txt</pre> |
561 </pre> |
156 <p id="ch11Section_5_8">allfiles.tar is our required tar archive of all the rest of files(or archive of files mentioned in command line). Other form of the previous command are:</p> |
562 <p id="ch11Section_5_8"> |
157 <pre class="programlisting"> |
563 allfiles.tar is our required tar archive of all the rest of files(or archive of files mentioned in command line). Other form of the previous command are: |
158 $ tar -c -v -f allfiles.tar first.txt second.txt third.txt</pre> |
564 </p> |
159 <p id="ch11Section_5_9">or</p> |
565 <pre class="programlisting"> |
160 <pre class="programlisting"> |
566 $ tar -c -v -f allfiles.tar first.txt second.txt third.txt |
161 $ tar -cvf allfiles.tar first.txt second.txt third.txt</pre> |
567 </pre> |
162 <p id="ch11Section_5_a">The general format for creating a tar archive is:</p> |
568 <p id="ch11Section_5_9"> |
163 <pre class="programlisting"> |
569 or |
164 tar [OPTION...] [FILE]...</pre> |
570 </p> |
165 <p id="ch11Section_5_b">For our command are using these options:</p> |
571 <pre class="programlisting"> |
166 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
572 $ tar -cvf allfiles.tar first.txt second.txt third.txt |
167 <li class="listitem" style="list-style-type: *"><p id="ch11Section_5_c">-c to Create the archive.</p></li> |
573 </pre> |
168 <li class="listitem" style="list-style-type: *"><p id="ch11Section_5_d">-v for Verbose mode, to get the names of the files as they are archived.</p></li> |
574 <p id="ch11Section_5_a"> |
169 <li class="listitem" style="list-style-type: *"><p id="ch11Section_5_e">-f mentioning the file name of the resulting tar archive.</p></li> |
575 The general format for creating a tar archive is: |
170 </ul></div> |
576 </p> |
171 <p id="ch11Section_5_f">To create archive of folder itself try this:</p> |
577 <pre class="programlisting"> |
172 <pre class="programlisting"> |
578 tar [OPTION...] [FILE]... |
173 $ tar -cvf fortune.tar fortune/</pre> |
579 </pre> |
174 <p id="ch11Section_5_10">To add files to existing tar archive, option <span class="emphasis"><em>`r`</em></span> is used:</p> |
580 <p id="ch11Section_5_b"> |
175 <pre class="programlisting"> |
581 For our command are using these options: |
176 $ fortune > fourth.txt |
582 </p> |
|
583 <div class="itemizedlist"> |
|
584 <ul class="itemizedlist" type="*"> |
|
585 <li class="listitem" style="list-style-type: *"> |
|
586 <p id="ch11Section_5_c"> |
|
587 -c to Create the archive. |
|
588 </p> |
|
589 </li> |
|
590 <li class="listitem" style="list-style-type: *"> |
|
591 <p id="ch11Section_5_d"> |
|
592 -v for Verbose mode, to get the names of the files as they are archived. |
|
593 </p> |
|
594 </li> |
|
595 <li class="listitem" style="list-style-type: *"> |
|
596 <p id="ch11Section_5_e"> |
|
597 -f mentioning the file name of the resulting tar archive. |
|
598 </p> |
|
599 </li> |
|
600 </ul> |
|
601 </div> |
|
602 <p id="ch11Section_5_f"> |
|
603 To create archive of folder itself try this: |
|
604 </p> |
|
605 <pre class="programlisting"> |
|
606 $ tar -cvf fortune.tar fortune/ |
|
607 </pre> |
|
608 <p id="ch11Section_5_10"> |
|
609 To add files to existing tar archive, option |
|
610 <span class="emphasis"> |
|
611 <em> |
|
612 `r` |
|
613 </em> |
|
614 </span> |
|
615 is used: |
|
616 </p> |
|
617 <pre class="programlisting"> |
|
618 $ fortune > fourth.txt |
|
619 $ tar -r fourth.txt -vf allfiles.tar |
177 $ tar -r fourth.txt -vf allfiles.tar |
620 fourth.txt |
178 fourth.txt</pre> |
621 </pre> |
179 <p id="ch11Section_5_11">There are other options too available for explicitly mentioning the position of archive, use <span class="emphasis"><em>tar --help</em></span> for getting all the details.</p> |
622 <p id="ch11Section_5_11"> |
180 <p id="ch11Section_5_12">Similarly to remove file from archive use <span class="emphasis"><em>--delete</em></span> option:</p> |
623 There are other options too available for explicitly mentioning the position of archive, use |
181 <pre class="programlisting"> |
624 <span class="emphasis"> |
182 $ tar --delete second.txt -f allfiles.tar |
625 <em> |
|
626 tar --help |
|
627 </em> |
|
628 </span> |
|
629 for getting all the details. |
|
630 </p> |
|
631 <p id="ch11Section_5_12"> |
|
632 Similarly to remove file from archive use |
|
633 <span class="emphasis"> |
|
634 <em> |
|
635 --delete |
|
636 </em> |
|
637 </span> |
|
638 option: |
|
639 </p> |
|
640 <pre class="programlisting"> |
|
641 $ tar --delete second.txt -f allfiles.tar |
|
642 $ tar -tf allfiles.tar |
183 $ tar -tf allfiles.tar |
643 first.txt |
184 first.txt |
644 third.txt |
185 third.txt |
645 fourth.txt |
186 fourth.txt</pre> |
646 </pre> |
187 </div> |
647 </div> |
188 <div class="section" title="2.2.2.Listing the files of archive:"> |
648 <div class="section" title="2.2.2.Listing the files of archive:"> |
189 <div class="titlepage"><div><div><h4 class="title"> |
649 <div class="titlepage"> |
190 <a name="id2698790"></a>2.2.2.Listing the files of archive:</h4></div></div></div> |
650 <div> |
191 <p id="ch11Section_5_13">Once files are archived, tar command have the <span class="emphasis"><em>`t`</em></span> option, for Listing all files in the tar file:</p> |
651 <div> |
192 <pre class="programlisting"> |
652 <h4 class="title"> |
193 $ tar tf allfiles.tar |
653 <a name="id2938133"> |
|
654 </a> |
|
655 2.2.2.Listing the files of archive: |
|
656 </h4> |
|
657 </div> |
|
658 </div> |
|
659 </div> |
|
660 <p id="ch11Section_5_13"> |
|
661 Once files are archived, tar command have the |
|
662 <span class="emphasis"> |
|
663 <em> |
|
664 `t` |
|
665 </em> |
|
666 </span> |
|
667 option, for Listing all files in the tar file: |
|
668 </p> |
|
669 <pre class="programlisting"> |
|
670 $ tar tf allfiles.tar |
|
671 first.txt |
194 first.txt |
672 second.txt |
195 second.txt |
673 third.txt |
196 third.txt</pre> |
674 </pre> |
197 <p id="ch11Section_5_14"><span class="strong"><strong>//this is not working for me in some cases :(</strong></span></p> |
675 <p id="ch11Section_5_14"> |
198 <p id="ch11Section_5_15">To locate a particular file among the archive mention its name after <span class="emphasis"><em>t</em></span> option.</p> |
676 <span class="strong"> |
199 <pre class="programlisting"> |
677 <strong> |
200 $ tar t second.txt allfiles.tar |
678 //this is not working for me in some cases :( |
201 second.txt</pre> |
679 </strong> |
202 <p id="ch11Section_5_16">one can also use elementary regex for locating the file, so in previous case even second.* will also return the same result.</p> |
680 </span> |
203 </div> |
681 </p> |
204 <div class="section" title="2.2.3.Extracting files from archive:"> |
682 <p id="ch11Section_5_15"> |
205 <div class="titlepage"><div><div><h4 class="title"> |
683 To locate a particular file among the archive mention its name after |
206 <a name="id2698840"></a>2.2.3.Extracting files from archive:</h4></div></div></div> |
684 <span class="emphasis"> |
207 <p id="ch11Section_5_17">To extract the content from a archive, use <span class="emphasis"><em>`x`</em></span> option:</p> |
685 <em> |
208 <pre class="programlisting"> |
686 t |
209 $ mkdir extract |
687 </em> |
|
688 </span> |
|
689 option. |
|
690 </p> |
|
691 <pre class="programlisting"> |
|
692 $ tar t second.txt allfiles.tar |
|
693 second.txt |
|
694 </pre> |
|
695 <p id="ch11Section_5_16"> |
|
696 one can also use elementary regex for locating the file, so in previous case even second.* will also return the same result. |
|
697 </p> |
|
698 </div> |
|
699 <div class="section" title="2.2.3.Extracting files from archive:"> |
|
700 <div class="titlepage"> |
|
701 <div> |
|
702 <div> |
|
703 <h4 class="title"> |
|
704 <a name="id2938183"> |
|
705 </a> |
|
706 2.2.3.Extracting files from archive: |
|
707 </h4> |
|
708 </div> |
|
709 </div> |
|
710 </div> |
|
711 <p id="ch11Section_5_17"> |
|
712 To extract the content from a archive, use |
|
713 <span class="emphasis"> |
|
714 <em> |
|
715 `x` |
|
716 </em> |
|
717 </span> |
|
718 option: |
|
719 </p> |
|
720 <pre class="programlisting"> |
|
721 $ mkdir extract |
|
722 $ cp allfiles.tar extract/ |
210 $ cp allfiles.tar extract/ |
723 $ cd extract |
211 $ cd extract |
724 $ tar -xvf allfiles.tar |
212 $ tar -xvf allfiles.tar |
725 first.txt |
213 first.txt |
726 second.txt |
214 second.txt |
727 third.txt |
215 third.txt</pre> |
728 </pre> |
216 <p id="ch11Section_5_18">To extract any particular file from archive, mention the name of file after <span class="emphasis"><em>x</em></span> option:</p> |
729 <p id="ch11Section_5_18"> |
217 <pre class="programlisting"> |
730 To extract any particular file from archive, mention the name of file after |
218 $ tar -x second.txt -vf allfiles.tar |
731 <span class="emphasis"> |
219 second.txt</pre> |
732 <em> |
220 </div> |
733 x |
221 </div> |
734 </em> |
222 <div class="section" title="2.3.Further Reading for this section:"> |
735 </span> |
223 <div class="titlepage"><div><div><h3 class="title"> |
736 option: |
224 <a name="id2698875"></a>2.3.Further Reading for this section:</h3></div></div></div> |
737 </p> |
225 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
738 <pre class="programlisting"> |
226 <li class="listitem" style="list-style-type: *"> |
739 $ tar -x second.txt -vf allfiles.tar |
227 <p id="ch11Section_5_19"></p> |
740 second.txt |
228 <div class="reference"> |
741 </pre> |
229 <div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/Tar_(file_format</div> |
742 </div> |
230 <p>)</p> |
743 </div> |
231 </li> |
744 <div class="section" title="2.3.Further Reading for this section:"> |
232 <li class="listitem" style="list-style-type: *"> |
745 <div class="titlepage"> |
233 <p id="ch11Section_5_1a"></p> |
746 <div> |
234 <div class="reference"> |
747 <div> |
235 <div class="titlepage"><hr /></div>http://www.gnu.org/software/tar/manual/tar.html</div> |
748 <h3 class="title"> |
236 </li> |
749 <a name="id2938218"> |
237 <li class="listitem" style="list-style-type: *"> |
750 </a> |
238 <p id="ch11Section_5_1b"></p> |
751 2.3.Further Reading for this section: |
239 <div class="reference"> |
752 </h3> |
240 <div class="titlepage"><hr /></div>http://linuxreviews.org/beginner/</div> |
753 </div> |
241 </li> |
754 </div> |
242 </ul></div> |
755 </div> |
243 </div> |
756 <div class="itemizedlist"> |
244 </div> |
757 <ul class="itemizedlist" type="*"> |
245 <div class="section" title="3.GZip:"> |
758 <li class="listitem" style="list-style-type: *"> |
246 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
759 <p id="ch11Section_5_19"> |
247 <a name="id2698920"></a>3.GZip:</h2></div></div></div> |
760 </p> |
248 <p id="ch11Section_5_1c">Tar creates archives but it does not compress data by itself unless specified explicitly. Hence all the archive we create using tar command, is simply of the size of total size of all individual files. With Linux there is a compression tool known as <span class="emphasis"><em>gzip</em></span> which is used to reduce the size of files mentioned. Whenever possible, each file is replaced by one with the extension .gz', so unlike `tar this command would <span class="emphasis"><em>replace the existing file</em></span>.</p> |
761 <div class="reference"> |
249 <div class="section" title="3.1.Get going:"> |
762 <div class="titlepage"> |
250 <div class="titlepage"><div><div><h3 class="title"> |
763 <hr /> |
251 <a name="id2698946"></a>3.1.Get going:</h3></div></div></div> |
764 </div> |
252 <p id="ch11Section_5_1d">As usual first commands to check out with gzip are <span class="emphasis"><em>man</em></span> and <span class="emphasis"><em>help</em></span>,</p> |
765 http://en.wikipedia.org/wiki/Tar_(file_format |
253 <pre class="programlisting"> |
766 </div> |
254 $ man gzip |
767 <p> |
255 $ gzip --help</pre> |
768 ) |
256 <p id="ch11Section_5_1e">Creating a zip of a bunch of files is fairly trivial, it can be done simply via:</p> |
769 </p> |
257 <pre class="programlisting"> |
770 </li> |
258 $ gzip [OPTION]... [FILE]...</pre> |
771 <li class="listitem" style="list-style-type: *"> |
259 <div class="section" title="3.1.1.Creating zip files:"> |
772 <p id="ch11Section_5_1a"> |
260 <div class="titlepage"><div><div><h4 class="title"> |
773 </p> |
261 <a name="id2698976"></a>3.1.1.Creating zip files:</h4></div></div></div> |
774 <div class="reference"> |
262 <p id="ch11Section_5_1f">Continuing from previous set of files and setup, we will like to zip them and hence the command would be:</p> |
775 <div class="titlepage"> |
263 <pre class="programlisting"> |
776 <hr /> |
264 $ gzip first.txt fourth.txt second.txt third.txt |
777 </div> |
|
778 http://www.gnu.org/software/tar/manual/tar.html |
|
779 </div> |
|
780 </li> |
|
781 <li class="listitem" style="list-style-type: *"> |
|
782 <p id="ch11Section_5_1b"> |
|
783 </p> |
|
784 <div class="reference"> |
|
785 <div class="titlepage"> |
|
786 <hr /> |
|
787 </div> |
|
788 http://linuxreviews.org/beginner/ |
|
789 </div> |
|
790 </li> |
|
791 </ul> |
|
792 </div> |
|
793 </div> |
|
794 </div> |
|
795 <div class="section" title="3.GZip:"> |
|
796 <div class="titlepage"> |
|
797 <div> |
|
798 <div> |
|
799 <h2 class="title" style="clear: both"> |
|
800 <a name="id2938263"> |
|
801 </a> |
|
802 3.GZip: |
|
803 </h2> |
|
804 </div> |
|
805 </div> |
|
806 </div> |
|
807 <p id="ch11Section_5_1c"> |
|
808 Tar creates archives but it does not compress data by itself unless specified explicitly. Hence all the archive we create using tar command, is simply of the size of total size of all individual files. With Linux there is a compression tool known as |
|
809 <span class="emphasis"> |
|
810 <em> |
|
811 gzip |
|
812 </em> |
|
813 </span> |
|
814 which is used to reduce the size of files mentioned. Whenever possible, each file is replaced by one with the extension .gz', so unlike `tar this command would |
|
815 <span class="emphasis"> |
|
816 <em> |
|
817 replace the existing file |
|
818 </em> |
|
819 </span> |
|
820 . |
|
821 </p> |
|
822 <div class="section" title="3.1.Get going:"> |
|
823 <div class="titlepage"> |
|
824 <div> |
|
825 <div> |
|
826 <h3 class="title"> |
|
827 <a name="id2938288"> |
|
828 </a> |
|
829 3.1.Get going: |
|
830 </h3> |
|
831 </div> |
|
832 </div> |
|
833 </div> |
|
834 <p id="ch11Section_5_1d"> |
|
835 As usual first commands to check out with gzip are |
|
836 <span class="emphasis"> |
|
837 <em> |
|
838 man |
|
839 </em> |
|
840 </span> |
|
841 and |
|
842 <span class="emphasis"> |
|
843 <em> |
|
844 help |
|
845 </em> |
|
846 </span> |
|
847 , |
|
848 </p> |
|
849 <pre class="programlisting"> |
|
850 $ man gzip |
|
851 $ gzip --help |
|
852 </pre> |
|
853 <p id="ch11Section_5_1e"> |
|
854 Creating a zip of a bunch of files is fairly trivial, it can be done simply via: |
|
855 </p> |
|
856 <pre class="programlisting"> |
|
857 $ gzip [OPTION]... [FILE]... |
|
858 </pre> |
|
859 <div class="section" title="3.1.1.Creating zip files:"> |
|
860 <div class="titlepage"> |
|
861 <div> |
|
862 <div> |
|
863 <h4 class="title"> |
|
864 <a name="id2938319"> |
|
865 </a> |
|
866 3.1.1.Creating zip files: |
|
867 </h4> |
|
868 </div> |
|
869 </div> |
|
870 </div> |
|
871 <p id="ch11Section_5_1f"> |
|
872 Continuing from previous set of files and setup, we will like to zip them and hence the command would be: |
|
873 </p> |
|
874 <pre class="programlisting"> |
|
875 $ gzip first.txt fourth.txt second.txt third.txt |
|
876 $ ls |
265 $ ls |
877 allfiles.tar first.txt.gz fourth.txt.gz second.txt.gz third.txt.gz zipped.tar.gz |
266 allfiles.tar first.txt.gz fourth.txt.gz second.txt.gz third.txt.gz zipped.tar.gz</pre> |
878 </pre> |
267 <p id="ch11Section_5_20">Hence, as mentioned above, all original files are replaced by .gz extension. The above task can also be restated and made easy with help of some common regex expression:</p> |
879 <p id="ch11Section_5_20"> |
268 <pre class="programlisting"> |
880 Hence, as mentioned above, all original files are replaced by .gz extension. The above task can also be restated and made easy with help of some common regex expression: |
269 $ gzip *.txt</pre> |
881 </p> |
270 <p id="ch11Section_5_21">Similar to <span class="emphasis"><em>tar</em></span> command, one can also use <span class="emphasis"><em>`-v`</em></span> option here to see the output in <span class="emphasis"><em>verbose</em></span> form. For the previous example, if we enable this option the result would be something like this:</p> |
882 <pre class="programlisting"> |
271 <pre class="programlisting"> |
883 $ gzip *.txt |
272 $ gzip -v *.txt |
884 </pre> |
|
885 <p id="ch11Section_5_21"> |
|
886 Similar to |
|
887 <span class="emphasis"> |
|
888 <em> |
|
889 tar |
|
890 </em> |
|
891 </span> |
|
892 command, one can also use |
|
893 <span class="emphasis"> |
|
894 <em> |
|
895 `-v` |
|
896 </em> |
|
897 </span> |
|
898 option here to see the output in |
|
899 <span class="emphasis"> |
|
900 <em> |
|
901 verbose |
|
902 </em> |
|
903 </span> |
|
904 form. For the previous example, if we enable this option the result would be something like this: |
|
905 </p> |
|
906 <pre class="programlisting"> |
|
907 $ gzip -v *.txt |
|
908 first.txt: 4.4% -- replaced with first.txt.gz |
273 first.txt: 4.4% -- replaced with first.txt.gz |
909 fourth.txt: -7.1% -- replaced with fourth.txt.gz |
274 fourth.txt: -7.1% -- replaced with fourth.txt.gz |
910 second.txt: -4.8% -- replaced with second.txt.gz |
275 second.txt: -4.8% -- replaced with second.txt.gz |
911 third.txt: 3.8% -- replaced with third.txt.gz |
276 third.txt: 3.8% -- replaced with third.txt.gz</pre> |
912 </pre> |
277 <p id="ch11Section_5_22">For files of very small sizes and some other cases, one might end up with a zipped file whose size is greater then original file, but compression is always performed(so don't be disheartened in the above case, as files are larger :P). So unlike tar, here all files are zipped separately by default, to make them part of one single chunk one can use some <span class="emphasis"><em>pipes</em></span> and <span class="emphasis"><em>redirections</em></span></p> |
913 <p id="ch11Section_5_22"> |
278 <pre class="programlisting"> |
914 For files of very small sizes and some other cases, one might end up with a zipped file whose size is greater then original file, but compression is always performed(so don't be disheartened in the above case, as files are larger :P). So unlike tar, here all files are zipped separately by default, to make them part of one single chunk one can use some |
279 $ gzip -c *.txt > all.gz</pre> |
915 <span class="emphasis"> |
280 <p id="ch11Section_5_23">Now in this case, all files would be zipped, concatenated and then the output would be written to a file all.gz leaving back all the original files. In the command above <span class="emphasis"><em>`-c`</em></span> option states to print the output to standard output(stdout) and following <span class="emphasis"><em>`>`</em></span> would redirect the output to file all.gz. So when we decompress this file, we will get a single file named 'all' with all the content of each files concatenated one after the another.</p> |
916 <em> |
281 <p id="ch11Section_5_24">For creating a zip archive of a complete directory, one has to use <span class="emphasis"><em>`-r`</em></span> options which means recursive, it makes gzip to traverse through all directory tree/structure. By default it will create zip files of each file inside the directory, that is even with the -r flag, gzip still compresses one file at a time :</p> |
917 pipes |
282 <pre class="programlisting"> |
918 </em> |
283 $ gzip -r fortune-files/ |
919 </span> |
|
920 and |
|
921 <span class="emphasis"> |
|
922 <em> |
|
923 redirections |
|
924 </em> |
|
925 </span> |
|
926 </p> |
|
927 <pre class="programlisting"> |
|
928 $ gzip -c *.txt > all.gz |
|
929 </pre> |
|
930 <p id="ch11Section_5_23"> |
|
931 Now in this case, all files would be zipped, concatenated and then the output would be written to a file all.gz leaving back all the original files. In the command above |
|
932 <span class="emphasis"> |
|
933 <em> |
|
934 `-c` |
|
935 </em> |
|
936 </span> |
|
937 option states to print the output to standard output(stdout) and following |
|
938 <span class="emphasis"> |
|
939 <em> |
|
940 `>` |
|
941 </em> |
|
942 </span> |
|
943 would redirect the output to file all.gz. So when we decompress this file, we will get a single file named 'all' with all the content of each files concatenated one after the another. |
|
944 </p> |
|
945 <p id="ch11Section_5_24"> |
|
946 For creating a zip archive of a complete directory, one has to use |
|
947 <span class="emphasis"> |
|
948 <em> |
|
949 `-r` |
|
950 </em> |
|
951 </span> |
|
952 options which means recursive, it makes gzip to traverse through all directory tree/structure. By default it will create zip files of each file inside the directory, that is even with the -r flag, gzip still compresses one file at a time : |
|
953 </p> |
|
954 <pre class="programlisting"> |
|
955 $ gzip -r fortune-files/ |
|
956 $ gzip -rv . |
284 $ gzip -rv . |
957 ./first.txt: 4.4% -- replaced with ./first.txt.gz |
285 ./first.txt: 4.4% -- replaced with ./first.txt.gz |
958 ./second.txt: -4.8% -- replaced with ./second.txt.gz |
286 ./second.txt: -4.8% -- replaced with ./second.txt.gz |
959 ./third.txt: 3.8% -- replaced with ./third.txt.gz |
287 ./third.txt: 3.8% -- replaced with ./third.txt.gz |
960 ./allfiles.tar: 96.6% -- replaced with ./allfiles.tar.gz |
288 ./allfiles.tar: 96.6% -- replaced with ./allfiles.tar.gz |
961 ./fourth.txt: -7.1% -- replaced with ./fourth.txt.gz |
289 ./fourth.txt: -7.1% -- replaced with ./fourth.txt.gz</pre> |
962 </pre> |
290 <p id="ch11Section_5_25">Hence one always sees files like xxxxx.tar.gz, to create a zip of whole directory in a single file, first archive everything inside a folder and then use gzip on that. For zipping the files using tar itself, one has to use the option <span class="emphasis"><em>`g`</em></span>.</p> |
963 <p id="ch11Section_5_25"> |
291 <pre class="programlisting"> |
964 Hence one always sees files like xxxxx.tar.gz, to create a zip of whole directory in a single file, first archive everything inside a folder and then use gzip on that. For zipping the files using tar itself, one has to use the option |
292 $ tar -cvzf zipped.tar.gz *.txt |
965 <span class="emphasis"> |
|
966 <em> |
|
967 `g` |
|
968 </em> |
|
969 </span> |
|
970 . |
|
971 </p> |
|
972 <pre class="programlisting"> |
|
973 $ tar -cvzf zipped.tar.gz *.txt |
|
974 first.txt |
293 first.txt |
975 fourth.txt |
294 fourth.txt |
976 second.txt |
295 second.txt |
977 third.txt |
296 third.txt</pre> |
978 </pre> |
297 <p id="ch11Section_5_26"><span class="emphasis"><em>Thats why gzip is designed as a complement to tar, not as a replacement.</em></span></p> |
979 <p id="ch11Section_5_26"> |
298 <p id="ch11Section_5_27">gzip command comes with a option <span class="emphasis"><em>`-l`</em></span> to view the compressed file contents:</p> |
980 <span class="emphasis"> |
299 <pre class="programlisting"> |
981 <em> |
300 $ gzip -l zipped.tar.gz |
982 Thats why gzip is designed as a complement to tar, not as a replacement. |
|
983 </em> |
|
984 </span> |
|
985 </p> |
|
986 <p id="ch11Section_5_27"> |
|
987 gzip command comes with a option |
|
988 <span class="emphasis"> |
|
989 <em> |
|
990 `-l` |
|
991 </em> |
|
992 </span> |
|
993 to view the compressed file contents: |
|
994 </p> |
|
995 <pre class="programlisting"> |
|
996 $ gzip -l zipped.tar.gz |
|
997 compressed uncompressed ratio uncompressed_name |
301 compressed uncompressed ratio uncompressed_name |
998 332 10240 97.0% zipped.tar |
302 332 10240 97.0% zipped.tar</pre> |
999 </pre> |
303 <p id="ch11Section_5_28">Other feature of gzip is option for mentioning the kind of compression one wants. There is a option of <span class="emphasis"><em>`-n`</em></span> where <span class="emphasis"><em>n varies from 0 to 9</em></span> which regulate the speed/quality of compression. With <span class="emphasis"><em>`-1`</em></span> or <span class="emphasis"><em>`--fast`</em></span> option it means the fastest compression method (less compression) and <span class="emphasis"><em>`--best`</em></span> or <span class="emphasis"><em>`-9`</em></span> indicates the slowest compression method, default compression level is <span class="emphasis"><em>`-6`</em></span>.</p> |
1000 <p id="ch11Section_5_28"> |
304 <p id="ch11Section_5_29">To decompress a already compressed file there are two options, either use <span class="emphasis"><em>`gunzip`</em></span> command or use <span class="emphasis"><em>`-d`</em></span> option with gzip command:</p> |
1001 Other feature of gzip is option for mentioning the kind of compression one wants. There is a option of |
305 <pre class="programlisting"> |
1002 <span class="emphasis"> |
306 $ gzip -dv *.gz |
1003 <em> |
|
1004 `-n` |
|
1005 </em> |
|
1006 </span> |
|
1007 where |
|
1008 <span class="emphasis"> |
|
1009 <em> |
|
1010 n varies from 0 to 9 |
|
1011 </em> |
|
1012 </span> |
|
1013 which regulate the speed/quality of compression. With |
|
1014 <span class="emphasis"> |
|
1015 <em> |
|
1016 `-1` |
|
1017 </em> |
|
1018 </span> |
|
1019 or |
|
1020 <span class="emphasis"> |
|
1021 <em> |
|
1022 `--fast` |
|
1023 </em> |
|
1024 </span> |
|
1025 option it means the fastest compression method (less compression) and |
|
1026 <span class="emphasis"> |
|
1027 <em> |
|
1028 `--best` |
|
1029 </em> |
|
1030 </span> |
|
1031 or |
|
1032 <span class="emphasis"> |
|
1033 <em> |
|
1034 `-9` |
|
1035 </em> |
|
1036 </span> |
|
1037 indicates the slowest compression method, default compression level is |
|
1038 <span class="emphasis"> |
|
1039 <em> |
|
1040 `-6` |
|
1041 </em> |
|
1042 </span> |
|
1043 . |
|
1044 </p> |
|
1045 <p id="ch11Section_5_29"> |
|
1046 To decompress a already compressed file there are two options, either use |
|
1047 <span class="emphasis"> |
|
1048 <em> |
|
1049 `gunzip` |
|
1050 </em> |
|
1051 </span> |
|
1052 command or use |
|
1053 <span class="emphasis"> |
|
1054 <em> |
|
1055 `-d` |
|
1056 </em> |
|
1057 </span> |
|
1058 option with gzip command: |
|
1059 </p> |
|
1060 <pre class="programlisting"> |
|
1061 $ gzip -dv *.gz |
|
1062 all.gz: -440.4% -- replaced with all |
307 all.gz: -440.4% -- replaced with all |
1063 first.txt.gz: 4.4% -- replaced with first.txt |
308 first.txt.gz: 4.4% -- replaced with first.txt |
1064 fourth.txt.gz: -7.1% -- replaced with fourth.txt |
309 fourth.txt.gz: -7.1% -- replaced with fourth.txt |
1065 second.txt.gz: -4.8% -- replaced with second.txt |
310 second.txt.gz: -4.8% -- replaced with second.txt |
1066 third.txt.gz: 3.8% -- replaced with third.txt |
311 third.txt.gz: 3.8% -- replaced with third.txt |
1067 zipped.tar.gz: 97.0% -- replaced with zipped.tar |
312 zipped.tar.gz: 97.0% -- replaced with zipped.tar</pre> |
1068 </pre> |
313 <p id="ch11Section_5_2a">or:</p> |
1069 <p id="ch11Section_5_2a"> |
314 <pre class="programlisting"> |
1070 or: |
315 $ gunzip -v *.gz</pre> |
1071 </p> |
316 <p id="ch11Section_5_2b">Both of those commands will give the same result. So here one can notice the content of file "all" which we created earlier, it will have content of all the rest of four files concatenated one after another, but "zipped.tar.gz" is zip of tar of all files, will effectively have zip of archives of all files separately, and hence the usage and importance of <span class="emphasis"><em>tar</em></span>.</p> |
1072 <pre class="programlisting"> |
317 </div> |
1073 $ gunzip -v *.gz |
318 </div> |
1074 </pre> |
319 <div class="section" title="3.2.Further Reading for this section:"> |
1075 <p id="ch11Section_5_2b"> |
320 <div class="titlepage"><div><div><h3 class="title"> |
1076 Both of those commands will give the same result. So here one can notice the content of file "all" which we created earlier, it will have content of all the rest of four files concatenated one after another, but "zipped.tar.gz" is zip of tar of all files, will effectively have zip of archives of all files separately, and hence the usage and importance of |
321 <a name="id2699199"></a>3.2.Further Reading for this section:</h3></div></div></div> |
1077 <span class="emphasis"> |
322 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1078 <em> |
323 <li class="listitem" style="list-style-type: *"> |
1079 tar |
324 <p id="ch11Section_5_2c"></p> |
1080 </em> |
325 <div class="reference"> |
1081 </span> |
326 <div class="titlepage"><hr /></div>http://linuxreviews.org/beginner/</div> |
1082 . |
327 </li> |
1083 </p> |
328 <li class="listitem" style="list-style-type: *"> |
1084 </div> |
329 <p id="ch11Section_5_2d"></p> |
1085 </div> |
330 <div class="reference"> |
1086 <div class="section" title="3.2.Further Reading for this section:"> |
331 <div class="titlepage"><hr /></div>http://lowfatlinux.com/linux-gzip-gunzip.html</div> |
1087 <div class="titlepage"> |
332 </li> |
1088 <div> |
333 <li class="listitem" style="list-style-type: *"> |
1089 <div> |
334 <p id="ch11Section_5_2e"></p> |
1090 <h3 class="title"> |
335 <div class="reference"> |
1091 <a name="id2938542"> |
336 <div class="titlepage"><hr /></div>http://www.gnu.org/software/gzip/manual/gzip.html</div> |
1092 </a> |
337 </li> |
1093 3.2.Further Reading for this section: |
338 <li class="listitem" style="list-style-type: *"> |
1094 </h3> |
339 <p id="ch11Section_5_2f"></p> |
1095 </div> |
340 <div class="reference"> |
1096 </div> |
341 <div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/ZIP_(file_format</div> |
1097 </div> |
342 <p>)</p> |
1098 <div class="itemizedlist"> |
343 </li> |
1099 <ul class="itemizedlist" type="*"> |
344 </ul></div> |
1100 <li class="listitem" style="list-style-type: *"> |
345 </div> |
1101 <p id="ch11Section_5_2c"> |
346 </div> |
1102 </p> |
347 <div class="section" title="4.File Comparisons:"> |
1103 <div class="reference"> |
348 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1104 <div class="titlepage"> |
349 <a name="id2699253"></a>4.File Comparisons:</h2></div></div></div> |
1105 <hr /> |
350 <p id="ch11Section_5_30">Linux based distributions also have some utilities for checking the content of files, comparing them very quickly to other files. These operations can be looking for differences/similarities. Some of the commands which prove handy are:</p> |
1106 </div> |
351 <div class="section" title="4.1.cmp:"> |
1107 http://linuxreviews.org/beginner/ |
352 <div class="titlepage"><div><div><h3 class="title"> |
1108 </div> |
353 <a name="id2699270"></a>4.1.cmp:</h3></div></div></div> |
1109 </li> |
354 <p id="ch11Section_5_31">If one wants to compare two files whether they are same or not, one can use this handy tool. Let us consider some situation, we run find/locate command to locate some file, and it turns out that we have a file with same name in different location, and in case we want to run a quick check on there content, cmp is the right tool. For my system I perform these tasks to illustrate the use of this command:</p> |
1110 <li class="listitem" style="list-style-type: *"> |
355 <pre class="programlisting"> |
1111 <p id="ch11Section_5_2d"> |
356 $ find . -name quick.c |
1112 </p> |
|
1113 <div class="reference"> |
|
1114 <div class="titlepage"> |
|
1115 <hr /> |
|
1116 </div> |
|
1117 http://lowfatlinux.com/linux-gzip-gunzip.html |
|
1118 </div> |
|
1119 </li> |
|
1120 <li class="listitem" style="list-style-type: *"> |
|
1121 <p id="ch11Section_5_2e"> |
|
1122 </p> |
|
1123 <div class="reference"> |
|
1124 <div class="titlepage"> |
|
1125 <hr /> |
|
1126 </div> |
|
1127 http://www.gnu.org/software/gzip/manual/gzip.html |
|
1128 </div> |
|
1129 </li> |
|
1130 <li class="listitem" style="list-style-type: *"> |
|
1131 <p id="ch11Section_5_2f"> |
|
1132 </p> |
|
1133 <div class="reference"> |
|
1134 <div class="titlepage"> |
|
1135 <hr /> |
|
1136 </div> |
|
1137 http://en.wikipedia.org/wiki/ZIP_(file_format |
|
1138 </div> |
|
1139 <p> |
|
1140 ) |
|
1141 </p> |
|
1142 </li> |
|
1143 </ul> |
|
1144 </div> |
|
1145 </div> |
|
1146 </div> |
|
1147 <div class="section" title="4.File Comparisons:"> |
|
1148 <div class="titlepage"> |
|
1149 <div> |
|
1150 <div> |
|
1151 <h2 class="title" style="clear: both"> |
|
1152 <a name="id2938596"> |
|
1153 </a> |
|
1154 4.File Comparisons: |
|
1155 </h2> |
|
1156 </div> |
|
1157 </div> |
|
1158 </div> |
|
1159 <p id="ch11Section_5_30"> |
|
1160 Linux based distributions also have some utilities for checking the content of files, comparing them very quickly to other files. These operations can be looking for differences/similarities. Some of the commands which prove handy are: |
|
1161 </p> |
|
1162 <div class="section" title="4.1.cmp:"> |
|
1163 <div class="titlepage"> |
|
1164 <div> |
|
1165 <div> |
|
1166 <h3 class="title"> |
|
1167 <a name="id2938613"> |
|
1168 </a> |
|
1169 4.1.cmp: |
|
1170 </h3> |
|
1171 </div> |
|
1172 </div> |
|
1173 </div> |
|
1174 <p id="ch11Section_5_31"> |
|
1175 If one wants to compare two files whether they are same or not, one can use this handy tool. Let us consider some situation, we run find/locate command to locate some file, and it turns out that we have a file with same name in different location, and in case we want to run a quick check on there content, cmp is the right tool. For my system I perform these tasks to illustrate the use of this command: |
|
1176 </p> |
|
1177 <pre class="programlisting"> |
|
1178 $ find . -name quick.c |
|
1179 ./Desktop/programs/quick.c |
357 ./Desktop/programs/quick.c |
1180 ./c-folder/quick.c |
358 ./c-folder/quick.c |
1181 $ cmp Desktop/programs/quick.c c-folder/quick.c |
359 $ cmp Desktop/programs/quick.c c-folder/quick.c |
1182 $ |
360 $</pre> |
1183 </pre> |
361 <p id="ch11Section_5_32">For me it returns nothing, hence that means both the files are exact copy of each other, by default, cmp is silent if the files are the same. Make some changes in one of the file and rerun the command. For me it works like this:</p> |
1184 <p id="ch11Section_5_32"> |
362 <pre class="programlisting"> |
1185 For me it returns nothing, hence that means both the files are exact copy of each other, by default, cmp is silent if the files are the same. Make some changes in one of the file and rerun the command. For me it works like this: |
363 $ cmp Desktop/programs/quick.c c-folder/quick.c |
1186 </p> |
364 Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24</pre> |
1187 <pre class="programlisting"> |
365 <p id="ch11Section_5_33">That is, if files differ, the byte and line number at which the first difference occurred is reported.</p> |
1188 $ cmp Desktop/programs/quick.c c-folder/quick.c |
366 </div> |
1189 Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24 |
367 <div class="section" title="4.2.diff:"> |
1190 </pre> |
368 <div class="titlepage"><div><div><h3 class="title"> |
1191 <p id="ch11Section_5_33"> |
369 <a name="id2699312"></a>4.2.diff:</h3></div></div></div> |
1192 That is, if files differ, the byte and line number at which the first difference occurred is reported. |
370 <p id="ch11Section_5_34">Now there are situations when one wants to exactly know the differences among two files, for them, GNU diff can show whether files are different without detailing the differences. For simple and basic usage of this programs, consider following example:</p> |
1193 </p> |
371 <pre class="programlisting"> |
1194 </div> |
372 $ echo -e "quick nbrown nfox njumped nover nthe nlazy ndog" > allcharacters.txt |
1195 <div class="section" title="4.2.diff:"> |
|
1196 <div class="titlepage"> |
|
1197 <div> |
|
1198 <div> |
|
1199 <h3 class="title"> |
|
1200 <a name="id2938655"> |
|
1201 </a> |
|
1202 4.2.diff: |
|
1203 </h3> |
|
1204 </div> |
|
1205 </div> |
|
1206 </div> |
|
1207 <p id="ch11Section_5_34"> |
|
1208 Now there are situations when one wants to exactly know the differences among two files, for them, GNU diff can show whether files are different without detailing the differences. For simple and basic usage of this programs, consider following example: |
|
1209 </p> |
|
1210 <pre class="programlisting"> |
|
1211 $ echo -e "quick nbrown nfox njumped nover nthe nlazy ndog" > allcharacters.txt |
|
1212 $ echo -e "quick nbrown nfox njmuped nover nteh nlzay ndog" > problem.txt |
373 $ echo -e "quick nbrown nfox njmuped nover nteh nlzay ndog" > problem.txt |
1213 $ diff problem.txt allcharacters.txt |
374 $ diff problem.txt allcharacters.txt |
1214 4c4 |
375 4c4 |
1215 < jmuped |
376 < jmuped |
1216 --- |
377 --- |
1291 lazy |
415 lazy |
1292 lzay |
416 lzay |
1293 over |
417 over |
1294 quick |
418 quick |
1295 teh |
419 teh |
1296 the |
420 the</pre> |
1297 </pre> |
421 </div> |
1298 </div> |
422 </div> |
1299 </div> |
423 <div class="section" title="5.Environment Variables:"> |
1300 <div class="section" title="5.Environment Variables:"> |
424 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1301 <div class="titlepage"> |
425 <a name="id2699424"></a>5.Environment Variables:</h2></div></div></div> |
1302 <div> |
426 <p id="ch11Section_5_3b">These variables like HOME, OSTYPE,Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.By convention, environment variables have UPPER CASE and shell variables have lower case names.</p> |
1303 <div> |
427 <p id="ch11Section_5_3c">Some of examples of Environment variables are(result may vary!):</p> |
1304 <h2 class="title" style="clear: both"> |
428 <pre class="programlisting"> |
1305 <a name="id2938767"> |
429 $ echo $OSTYPE |
1306 </a> |
|
1307 5.Environment Variables: |
|
1308 </h2> |
|
1309 </div> |
|
1310 </div> |
|
1311 </div> |
|
1312 <p id="ch11Section_5_3b"> |
|
1313 These variables like HOME, OSTYPE,Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.By convention, environment variables have UPPER CASE and shell variables have lower case names. |
|
1314 </p> |
|
1315 <p id="ch11Section_5_3c"> |
|
1316 Some of examples of Environment variables are(result may vary!): |
|
1317 </p> |
|
1318 <pre class="programlisting"> |
|
1319 $ echo $OSTYPE |
|
1320 linux-gnu |
430 linux-gnu |
1321 $ echo $HOME |
431 $ echo $HOME |
1322 /home/baali |
432 /home/baali</pre> |
1323 </pre> |
433 <p id="ch11Section_5_3d">To see all the variables and there values use any of following commands:</p> |
1324 <p id="ch11Section_5_3d"> |
434 <pre class="programlisting"> |
1325 To see all the variables and there values use any of following commands: |
435 $ printenv | less |
1326 </p> |
436 $ env</pre> |
1327 <pre class="programlisting"> |
437 <p id="ch11Section_5_3e">The most commonly used environment variable is "PATH", it defines a list of directories to search through when looking for a command to execute. If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory:</p> |
1328 $ printenv | less |
438 <pre class="programlisting"> |
1329 $ env |
439 $ set PATH=$PATH:$HOME/bin</pre> |
1330 </pre> |
440 <p id="ch11Section_5_3f">See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier:</p> |
1331 <p id="ch11Section_5_3e"> |
441 <pre class="programlisting"> |
1332 The most commonly used environment variable is "PATH", it defines a list of directories to search through when looking for a command to execute. If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory: |
442 $ set repo = $HOME/Desktop/random/code |
1333 </p> |
443 $ cd $repo</pre> |
1334 <pre class="programlisting"> |
444 <p id="ch11Section_5_40"><span class="emphasis"><em>set</em></span> command is used to define a variable for the current shell. Try opening a new shell and use the above mentioned command, it wont work as expected. The other child process wont be able to see these variables unless we <span class="emphasis"><em>export</em></span> them. Repeat the above mentioned activity with <span class="emphasis"><em>export</em></span> command. Now with all new shells, <span class="emphasis"><em>$repo</em></span> will work.</p> |
1335 $ set PATH=$PATH:$HOME/bin |
445 <p id="ch11Section_5_41">Again these changes are limited to current session. To make them permanent or get loaded each time you log in, just add those lines to <span class="emphasis"><em>.bashrc</em></span> file.</p> |
1336 </pre> |
446 <div class="section" title="5.1.Further Reading:"> |
1337 <p id="ch11Section_5_3f"> |
447 <div class="titlepage"><div><div><h3 class="title"> |
1338 See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier: |
448 <a name="id2699524"></a>5.1.Further Reading:</h3></div></div></div> |
1339 </p> |
449 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1340 <pre class="programlisting"> |
450 <li class="listitem" style="list-style-type: *"> |
1341 $ set repo = $HOME/Desktop/random/code |
451 <p id="ch11Section_5_42"></p> |
1342 $ cd $repo |
452 <div class="reference"> |
1343 </pre> |
453 <div class="titlepage"><hr /></div>http://lowfatlinux.com/linux-environment-variables.html</div> |
1344 <p id="ch11Section_5_40"> |
454 </li> |
1345 <span class="emphasis"> |
455 <li class="listitem" style="list-style-type: *"> |
1346 <em> |
456 <p id="ch11Section_5_43"></p> |
1347 set |
457 <div class="reference"> |
1348 </em> |
458 <div class="titlepage"><hr /></div>http://www.codecoffee.com/tipsforlinux/articles/030.html</div> |
1349 </span> |
459 </li> |
1350 command is used to define a variable for the current shell. Try opening a new shell and use the above mentioned command, it wont work as expected. The other child process wont be able to see these variables unless we |
460 <li class="listitem" style="list-style-type: *"> |
1351 <span class="emphasis"> |
461 <p id="ch11Section_5_44"></p> |
1352 <em> |
462 <div class="reference"> |
1353 export |
463 <div class="titlepage"><hr /></div>http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html</div> |
1354 </em> |
464 </li> |
1355 </span> |
465 <li class="listitem" style="list-style-type: *"> |
1356 them. Repeat the above mentioned activity with |
466 <p id="ch11Section_5_45"></p> |
1357 <span class="emphasis"> |
467 <div class="reference"> |
1358 <em> |
468 <div class="titlepage"><hr /></div>http://en.wikipedia.org/wiki/Environment_variable</div> |
1359 export |
469 </li> |
1360 </em> |
470 </ul></div> |
1361 </span> |
471 </div> |
1362 command. Now with all new shells, |
472 </div> |
1363 <span class="emphasis"> |
473 <div class="section" title="6.Shell Scripting:"> |
1364 <em> |
474 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1365 $repo |
475 <a name="id2699578"></a>6.Shell Scripting:</h2></div></div></div> |
1366 </em> |
476 <div class="section" title="6.1.Basics:"> |
1367 </span> |
477 <div class="titlepage"><div><div><h3 class="title"> |
1368 will work. |
478 <a name="id2699586"></a>6.1.Basics:</h3></div></div></div> |
1369 </p> |
479 <p id="ch11Section_5_46">Shell program or shell script,a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands. The first <span class="emphasis"><em>"Hello World"</em></span> sample for shell scripting is as easy as it sounds:</p> |
1370 <p id="ch11Section_5_41"> |
480 <pre class="programlisting"> |
1371 Again these changes are limited to current session. To make them permanent or get loaded each time you log in, just add those lines to |
481 $ echo '#!/bin/sh' > my-script.sh |
1372 <span class="emphasis"> |
|
1373 <em> |
|
1374 .bashrc |
|
1375 </em> |
|
1376 </span> |
|
1377 file. |
|
1378 </p> |
|
1379 <div class="section" title="5.1.Further Reading:"> |
|
1380 <div class="titlepage"> |
|
1381 <div> |
|
1382 <div> |
|
1383 <h3 class="title"> |
|
1384 <a name="id2938866"> |
|
1385 </a> |
|
1386 5.1.Further Reading: |
|
1387 </h3> |
|
1388 </div> |
|
1389 </div> |
|
1390 </div> |
|
1391 <div class="itemizedlist"> |
|
1392 <ul class="itemizedlist" type="*"> |
|
1393 <li class="listitem" style="list-style-type: *"> |
|
1394 <p id="ch11Section_5_42"> |
|
1395 </p> |
|
1396 <div class="reference"> |
|
1397 <div class="titlepage"> |
|
1398 <hr /> |
|
1399 </div> |
|
1400 http://lowfatlinux.com/linux-environment-variables.html |
|
1401 </div> |
|
1402 </li> |
|
1403 <li class="listitem" style="list-style-type: *"> |
|
1404 <p id="ch11Section_5_43"> |
|
1405 </p> |
|
1406 <div class="reference"> |
|
1407 <div class="titlepage"> |
|
1408 <hr /> |
|
1409 </div> |
|
1410 http://www.codecoffee.com/tipsforlinux/articles/030.html |
|
1411 </div> |
|
1412 </li> |
|
1413 <li class="listitem" style="list-style-type: *"> |
|
1414 <p id="ch11Section_5_44"> |
|
1415 </p> |
|
1416 <div class="reference"> |
|
1417 <div class="titlepage"> |
|
1418 <hr /> |
|
1419 </div> |
|
1420 http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html |
|
1421 </div> |
|
1422 </li> |
|
1423 <li class="listitem" style="list-style-type: *"> |
|
1424 <p id="ch11Section_5_45"> |
|
1425 </p> |
|
1426 <div class="reference"> |
|
1427 <div class="titlepage"> |
|
1428 <hr /> |
|
1429 </div> |
|
1430 http://en.wikipedia.org/wiki/Environment_variable |
|
1431 </div> |
|
1432 </li> |
|
1433 </ul> |
|
1434 </div> |
|
1435 </div> |
|
1436 </div> |
|
1437 <div class="section" title="6.Shell Scripting:"> |
|
1438 <div class="titlepage"> |
|
1439 <div> |
|
1440 <div> |
|
1441 <h2 class="title" style="clear: both"> |
|
1442 <a name="id2938921"> |
|
1443 </a> |
|
1444 6.Shell Scripting: |
|
1445 </h2> |
|
1446 </div> |
|
1447 </div> |
|
1448 </div> |
|
1449 <div class="section" title="6.1.Basics:"> |
|
1450 <div class="titlepage"> |
|
1451 <div> |
|
1452 <div> |
|
1453 <h3 class="title"> |
|
1454 <a name="id2938929"> |
|
1455 </a> |
|
1456 6.1.Basics: |
|
1457 </h3> |
|
1458 </div> |
|
1459 </div> |
|
1460 </div> |
|
1461 <p id="ch11Section_5_46"> |
|
1462 Shell program or shell script,a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands. The first |
|
1463 <span class="emphasis"> |
|
1464 <em> |
|
1465 "Hello World" |
|
1466 </em> |
|
1467 </span> |
|
1468 sample for shell scripting is as easy as it sounds: |
|
1469 </p> |
|
1470 <pre class="programlisting"> |
|
1471 $ echo '#!/bin/sh' > my-script.sh |
|
1472 $ clear >> my-script.sh |
482 $ clear >> my-script.sh |
1473 $ echo 'echo Hello World' >> my-script.sh |
483 $ echo 'echo Hello World' >> my-script.sh |
1474 $ chmod 755 my-script.sh |
484 $ chmod 755 my-script.sh |
1475 $ ./my-script.sh |
485 $ ./my-script.sh |
1476 Hello World |
486 Hello World</pre> |
1477 </pre> |
487 <p id="ch11Section_5_47">The #! syntax(also known as shebang) is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The chmod is required to make the script executable. This script will just execute two commands, <span class="emphasis"><em>clear</em></span> and <span class="emphasis"><em>echo</em></span> one after another. One can also do the same task using a one liner command <span class="emphasis"><em>clear; echo 'Hello World';</em></span> but as number of lines grows using a script file is helpful.</p> |
1478 <p id="ch11Section_5_47"> |
488 <p id="ch11Section_5_48">So lets create a script which gives us all the filenames for given initial alphabet or string in a directory. Let the name of script be <span class="emphasis"><em>initial.sh</em></span>, open it with text editor, and write:</p> |
1479 The #! syntax(also known as shebang) is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The chmod is required to make the script executable. This script will just execute two commands, |
489 <pre class="programlisting"> |
1480 <span class="emphasis"> |
490 #!/bin/sh |
1481 <em> |
|
1482 clear |
|
1483 </em> |
|
1484 </span> |
|
1485 and |
|
1486 <span class="emphasis"> |
|
1487 <em> |
|
1488 echo |
|
1489 </em> |
|
1490 </span> |
|
1491 one after another. One can also do the same task using a one liner command |
|
1492 <span class="emphasis"> |
|
1493 <em> |
|
1494 clear; echo 'Hello World'; |
|
1495 </em> |
|
1496 </span> |
|
1497 but as number of lines grows using a script file is helpful. |
|
1498 </p> |
|
1499 <p id="ch11Section_5_48"> |
|
1500 So lets create a script which gives us all the filenames for given initial alphabet or string in a directory. Let the name of script be |
|
1501 <span class="emphasis"> |
|
1502 <em> |
|
1503 initial.sh |
|
1504 </em> |
|
1505 </span> |
|
1506 , open it with text editor, and write: |
|
1507 </p> |
|
1508 <pre class="programlisting"> |
|
1509 #!/bin/sh |
|
1510 ls > temp |
491 ls > temp |
1511 grep ^$1 < temp |
492 grep ^$1 < temp |
1512 rm temp |
493 rm temp |
1513 $ chmod a+x initial.sh |
494 $ chmod a+x initial.sh |
1514 $ ./initial.sh s |
495 $ ./initial.sh s</pre> |
1515 </pre> |
496 <p id="ch11Section_5_49">The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via <span class="emphasis"><em>$#</em></span> with name of script being first member, that is $0. Now lets write a script for finding a file, and then checking when was it last modified:</p> |
1516 <p id="ch11Section_5_49"> |
497 <pre class="programlisting"> |
1517 The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via |
498 #!/bin/sh |
1518 <span class="emphasis"> |
|
1519 <em> |
|
1520 $# |
|
1521 </em> |
|
1522 </span> |
|
1523 with name of script being first member, that is $0. Now lets write a script for finding a file, and then checking when was it last modified: |
|
1524 </p> |
|
1525 <pre class="programlisting"> |
|
1526 #!/bin/sh |
|
1527 name=`find . -name $1 -print` |
499 name=`find . -name $1 -print` |
1528 echo $name |
500 echo $name |
1529 last_modified=`stat -c %y $name| cut -f 1 -d " "` |
501 last_modified=`stat -c %y $name| cut -f 1 -d " "` |
1530 echo "Last modified: $last_modified" |
502 echo "Last modified: $last_modified" |
1531 $ ./search.sh fname |
503 $ ./search.sh fname</pre> |
1532 </pre> |
504 <p id="ch11Section_5_4a">Try giving some file you want to search in place of fname. Please note in second line <span class="emphasis"><em>`</em></span> its a back-quote(other key mapped with tilda), it is specifically used to get the output of one command into a variable. In this particular case name is a User defined variables (UDV) which stores the value. We access value stored in any variable using <span class="emphasis"><em>$</em></span> symbol before name of variable.</p> |
1533 <p id="ch11Section_5_4a"> |
505 <p id="ch11Section_5_4b">naming conventions for variables?? do we need them??</p> |
1534 Try giving some file you want to search in place of fname. Please note in second line |
506 </div> |
1535 <span class="emphasis"> |
507 <div class="section" title="6.2.Shell Arithmetic:"> |
1536 <em> |
508 <div class="titlepage"><div><div><h3 class="title"> |
1537 ` |
509 <a name="id2697944"></a>6.2.Shell Arithmetic:</h3></div></div></div> |
1538 </em> |
510 <p id="ch11Section_5_4c">Shell also provides support for basic arithmetic operations. The syntax is:</p> |
1539 </span> |
511 <pre class="programlisting"> |
1540 its a back-quote(other key mapped with tilda), it is specifically used to get the output of one command into a variable. In this particular case name is a User defined variables (UDV) which stores the value. We access value stored in any variable using |
512 $ expr op1 math-operator op2</pre> |
1541 <span class="emphasis"> |
513 <p id="ch11Section_5_4d">Some of example which can be tried handily:</p> |
1542 <em> |
514 <pre class="programlisting"> |
1543 $ |
515 $ expr -3 + 5 |
1544 </em> |
|
1545 </span> |
|
1546 symbol before name of variable. |
|
1547 </p> |
|
1548 <p id="ch11Section_5_4b"> |
|
1549 naming conventions for variables?? do we need them?? |
|
1550 </p> |
|
1551 </div> |
|
1552 <div class="section" title="6.2.Shell Arithmetic:"> |
|
1553 <div class="titlepage"> |
|
1554 <div> |
|
1555 <div> |
|
1556 <h3 class="title"> |
|
1557 <a name="id2937287"> |
|
1558 </a> |
|
1559 6.2.Shell Arithmetic: |
|
1560 </h3> |
|
1561 </div> |
|
1562 </div> |
|
1563 </div> |
|
1564 <p id="ch11Section_5_4c"> |
|
1565 Shell also provides support for basic arithmetic operations. The syntax is: |
|
1566 </p> |
|
1567 <pre class="programlisting"> |
|
1568 $ expr op1 math-operator op2 |
|
1569 </pre> |
|
1570 <p id="ch11Section_5_4d"> |
|
1571 Some of example which can be tried handily: |
|
1572 </p> |
|
1573 <pre class="programlisting"> |
|
1574 $ expr -3 + 5 |
|
1575 2 |
516 2 |
1576 $ expr 10 % 3 |
517 $ expr 10 % 3 |
1577 1 |
518 1</pre> |
1578 </pre> |
519 <p id="ch11Section_5_4e">These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error.</p> |
1579 <p id="ch11Section_5_4e"> |
520 <pre class="programlisting"> |
1580 These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. |
521 $ expr 2*3 |
1581 </p> |
522 expr: syntax error</pre> |
1582 <pre class="programlisting"> |
523 <p id="ch11Section_5_4f">One can use back-quotes(`) also to get value of expr.</p> |
1583 $ expr 2*3 |
524 <pre class="programlisting"> |
1584 expr: syntax error |
525 $ echo `expr 6 + 3` |
1585 </pre> |
|
1586 <p id="ch11Section_5_4f"> |
|
1587 One can use back-quotes(`) also to get value of expr. |
|
1588 </p> |
|
1589 <pre class="programlisting"> |
|
1590 $ echo `expr 6 + 3` |
|
1591 9 |
526 9 |
1592 $ result=`expr 6 + 3` |
527 $ result=`expr 6 + 3` |
1593 $ echo $result |
528 $ echo $result |
1594 9 |
529 9</pre> |
1595 </pre> |
530 <p id="ch11Section_5_50">Shell uses three kinds of quotes. Double quotes("), anything enclosed among them except from variable trailing after $, and characters after would be printed as it is. Single quotes('), anything enclosed within them is just same, no formulation/interpretation. Back quotes(`), anything inclosed is considered as command, or is executed.</p> |
1596 <p id="ch11Section_5_50"> |
531 <pre class="programlisting"> |
1597 Shell uses three kinds of quotes. Double quotes("), anything enclosed among them except from variable trailing after $, and characters after would be printed as it is. Single quotes('), anything enclosed within them is just same, no formulation/interpretation. Back quotes(`), anything inclosed is considered as command, or is executed. |
532 $ echo "Today is date" |
1598 </p> |
|
1599 <pre class="programlisting"> |
|
1600 $ echo "Today is date" |
|
1601 Today is date |
533 Today is date |
1602 $ echo "Today is `date`" |
534 $ echo "Today is `date`" |
1603 Today is Wed Sep 16 17:32:22 IST 2009 |
535 Today is Wed Sep 16 17:32:22 IST 2009 |
1604 $ echo 'Today is `date`' |
536 $ echo 'Today is `date`' |
1605 Today is `date` |
537 Today is `date` |
1606 $ echo "Today is n `date`" |
538 $ echo "Today is n `date`" |
1607 Today is n Wed Sep 16 17:40:13 IST 2009 |
539 Today is n Wed Sep 16 17:40:13 IST 2009 |
1608 $ echo -e "Today is n `date`" |
540 $ echo -e "Today is n `date`" |
1609 Today is |
541 Today is |
1610 Wed Sep 16 17:41:13 IST 2009 |
542 Wed Sep 16 17:41:13 IST 2009</pre> |
1611 </pre> |
543 </div> |
1612 </div> |
544 <div class="section" title="6.3.if else construct:"> |
1613 <div class="section" title="6.3.if else construct:"> |
545 <div class="titlepage"><div><div><h3 class="title"> |
1614 <div class="titlepage"> |
546 <a name="id2699963"></a>6.3.if else construct:</h3></div></div></div> |
1615 <div> |
547 <p id="ch11Section_5_51">One can have simple <span class="emphasis"><em>if else if</em></span> constructs in shell scripts to check conditions. Lets take simple example of writing a script which returns back whether the argument passed is positive or not:</p> |
1616 <div> |
548 <pre class="programlisting"> |
1617 <h3 class="title"> |
549 #!/bin/sh |
1618 <a name="id2939306"> |
|
1619 </a> |
|
1620 6.3.if else construct: |
|
1621 </h3> |
|
1622 </div> |
|
1623 </div> |
|
1624 </div> |
|
1625 <p id="ch11Section_5_51"> |
|
1626 One can have simple |
|
1627 <span class="emphasis"> |
|
1628 <em> |
|
1629 if else if |
|
1630 </em> |
|
1631 </span> |
|
1632 constructs in shell scripts to check conditions. Lets take simple example of writing a script which returns back whether the argument passed is positive or not: |
|
1633 </p> |
|
1634 <pre class="programlisting"> |
|
1635 #!/bin/sh |
|
1636 if test $1 -gt 0 |
550 if test $1 -gt 0 |
1637 then |
551 then |
1638 echo "number is positive" |
552 echo "number is positive" |
1639 else |
553 else |
1640 echo "number is negative" |
554 echo "number is negative" |
1641 fi |
555 fi |
1642 $ ./sign.sh -11 |
556 $ ./sign.sh -11 |
1643 number is negative |
557 number is negative</pre> |
1644 </pre> |
558 <p id="ch11Section_5_52">This script will compare the first value passed as argument with 0 <span class="emphasis"><em>if test var -gt val</em></span>, var being $1 and val being 0, gt meaning greater then. Now this program has some flaw, it will give same result for following input: (-11) and (-1, 5), as we are checking just $1 which is first argument and hence the result. For handling such situation we can include <span class="emphasis"><em>if-else</em></span> clause which will warn user of correct usage of script.</p> |
1645 <p id="ch11Section_5_52"> |
559 <pre class="programlisting"> |
1646 This script will compare the first value passed as argument with 0 |
560 #this is the case when no argument is passed |
1647 <span class="emphasis"> |
|
1648 <em> |
|
1649 if test var -gt val |
|
1650 </em> |
|
1651 </span> |
|
1652 , var being $1 and val being 0, gt meaning greater then. Now this program has some flaw, it will give same result for following input: (-11) and (-1, 5), as we are checking just $1 which is first argument and hence the result. For handling such situation we can include |
|
1653 <span class="emphasis"> |
|
1654 <em> |
|
1655 if-else |
|
1656 </em> |
|
1657 </span> |
|
1658 clause which will warn user of correct usage of script. |
|
1659 </p> |
|
1660 <pre class="programlisting"> |
|
1661 #this is the case when no argument is passed |
|
1662 if [ $# -eq 0 ] |
561 if [ $# -eq 0 ] |
1663 then |
562 then |
1664 echo "$0 : You must give/supply one integers" |
563 echo "$0 : You must give/supply one integers" |
1665 exit 1 |
564 exit 1 |
1666 else |
565 else |
1667 if [ $# -gt 1 ] |
566 if [ $# -gt 1 ] |
1668 then |
567 then |
1669 echo "$0 : You must give one integer" |
568 echo "$0 : You must give one integer" |
1670 exit 1 |
569 exit 1 |
1671 fi |
570 fi |
1672 fi |
571 fi</pre> |
1673 </pre> |
572 <p id="ch11Section_5_53">One important thing to not in shell script is spacing, with many comparison and evaluation operation a wrongly placed space will spoil all the fun. So in previous example the expression <span class="emphasis"><em>[ $# -eq 0 ]</em></span> will work properly, but if we remove those leading or trailing spaces like <span class="emphasis"><em>[ $# -eq 0]</em></span>, it wont work as expected, or rather throw a warning. Both <span class="emphasis"><em>test</em></span> and <span class="emphasis"><em>[]</em></span> do the same task of testing a expression and returning true or false.</p> |
1674 <p id="ch11Section_5_53"> |
573 <p id="ch11Section_5_54">Lets create something interesting using these if-else clause. Now we will create a script which will greet the user when he opens the shell. We will create the script, change the permission to make it executable and append the <span class="emphasis"><em>.bashrc</em></span> file with <span class="emphasis"><em>./greet.sh</em></span> line and we are done. The script is:</p> |
1675 One important thing to not in shell script is spacing, with many comparison and evaluation operation a wrongly placed space will spoil all the fun. So in previous example the expression |
574 <pre class="programlisting"> |
1676 <span class="emphasis"> |
575 #!/bin/sh |
1677 <em> |
|
1678 [ $# -eq 0 ] |
|
1679 </em> |
|
1680 </span> |
|
1681 will work properly, but if we remove those leading or trailing spaces like |
|
1682 <span class="emphasis"> |
|
1683 <em> |
|
1684 [ $# -eq 0] |
|
1685 </em> |
|
1686 </span> |
|
1687 , it wont work as expected, or rather throw a warning. Both |
|
1688 <span class="emphasis"> |
|
1689 <em> |
|
1690 test |
|
1691 </em> |
|
1692 </span> |
|
1693 and |
|
1694 <span class="emphasis"> |
|
1695 <em> |
|
1696 [] |
|
1697 </em> |
|
1698 </span> |
|
1699 do the same task of testing a expression and returning true or false. |
|
1700 </p> |
|
1701 <p id="ch11Section_5_54"> |
|
1702 Lets create something interesting using these if-else clause. Now we will create a script which will greet the user when he opens the shell. We will create the script, change the permission to make it executable and append the |
|
1703 <span class="emphasis"> |
|
1704 <em> |
|
1705 .bashrc |
|
1706 </em> |
|
1707 </span> |
|
1708 file with |
|
1709 <span class="emphasis"> |
|
1710 <em> |
|
1711 ./greet.sh |
|
1712 </em> |
|
1713 </span> |
|
1714 line and we are done. The script is: |
|
1715 </p> |
|
1716 <pre class="programlisting"> |
|
1717 #!/bin/sh |
|
1718 #Script to greet the user according to time of day |
576 #Script to greet the user according to time of day |
1719 temph=`date | cut -c12-13` |
577 temph=`date | cut -c12-13` |
1720 dat=`date +"%A %d in %B of %Y (%r)"` |
578 dat=`date +"%A %d in %B of %Y (%r)"` |
1721 if [ $temph -lt 12 ] |
579 if [ $temph -lt 12 ] |
1722 then |
580 then |
1730 |
588 |
1731 if [ $temph -gt 16 -a $temph -le 18 ] |
589 if [ $temph -gt 16 -a $temph -le 18 ] |
1732 then |
590 then |
1733 mess="Good Evening $LOGNAME" |
591 mess="Good Evening $LOGNAME" |
1734 fi |
592 fi |
1735 echo -e "$mess nThis is $dat" |
593 echo -e "$mess nThis is $dat"</pre> |
1736 </pre> |
594 <p id="ch11Section_5_55">For me when I open the shell the output is something like:</p> |
1737 <p id="ch11Section_5_55"> |
595 <pre class="programlisting"> |
1738 For me when I open the shell the output is something like: |
596 Good Morning baali, Have a nice day! |
1739 </p> |
597 This is Wednesday 16 in September of 2009 (11:54:47 AM IST)</pre> |
1740 <pre class="programlisting"> |
598 </div> |
1741 Good Morning baali, Have a nice day! |
599 <div class="section" title="6.4.Loops"> |
1742 This is Wednesday 16 in September of 2009 (11:54:47 AM IST) |
600 <div class="titlepage"><div><div><h3 class="title"> |
1743 </pre> |
601 <a name="id2700069"></a>6.4.Loops</h3></div></div></div> |
1744 </div> |
602 <p id="ch11Section_5_56">Bash has three different commands for looping -- <code class="literal">for</code>, <code class="literal">while</code> and <code class="literal">until</code>.</p> |
1745 <div class="section" title="6.4.Loops"> |
603 <div class="section" title="6.4.1.for loop"> |
1746 <div class="titlepage"> |
604 <div class="titlepage"><div><div><h4 class="title"> |
1747 <div> |
605 <a name="id2700092"></a>6.4.1.<code class="literal">for</code> loop</h4></div></div></div> |
1748 <div> |
606 <p id="ch11Section_5_57">Suppose we have a set of files, that have names beginning with numbers followed by their names - <code class="literal">08 - Society.mp3</code>. We would like to rename these files to remove the numbering. How would we go about doing that? It is clear from the problem statement that we could use a <code class="literal">for</code> loop, to loop through the list of files and rename each of the files.</p> |
1749 <h3 class="title"> |
607 <p id="ch11Section_5_58">Let's first look at a simple <code class="literal">for</code> loop, to understand how it works.</p> |
1750 <a name="id2939412"> |
608 <pre class="programlisting"> |
1751 </a> |
609 for animal in rat cat dog man |
1752 6.4.Loops |
|
1753 </h3> |
|
1754 </div> |
|
1755 </div> |
|
1756 </div> |
|
1757 <p id="ch11Section_5_56"> |
|
1758 Bash has three different commands for looping -- |
|
1759 <code class="literal"> |
|
1760 for |
|
1761 </code> |
|
1762 , |
|
1763 <code class="literal"> |
|
1764 while |
|
1765 </code> |
|
1766 and |
|
1767 <code class="literal"> |
|
1768 until |
|
1769 </code> |
|
1770 . |
|
1771 </p> |
|
1772 <div class="section" title="6.4.1.for loop"> |
|
1773 <div class="titlepage"> |
|
1774 <div> |
|
1775 <div> |
|
1776 <h4 class="title"> |
|
1777 <a name="id2939434"> |
|
1778 </a> |
|
1779 6.4.1. |
|
1780 <code class="literal"> |
|
1781 for |
|
1782 </code> |
|
1783 loop |
|
1784 </h4> |
|
1785 </div> |
|
1786 </div> |
|
1787 </div> |
|
1788 <p id="ch11Section_5_57"> |
|
1789 Suppose we have a set of files, that have names beginning with numbers followed by their names - |
|
1790 <code class="literal"> |
|
1791 08 - Society.mp3 |
|
1792 </code> |
|
1793 . We would like to rename these files to remove the numbering. How would we go about doing that? It is clear from the problem statement that we could use a |
|
1794 <code class="literal"> |
|
1795 for |
|
1796 </code> |
|
1797 loop, to loop through the list of files and rename each of the files. |
|
1798 </p> |
|
1799 <p id="ch11Section_5_58"> |
|
1800 Let's first look at a simple |
|
1801 <code class="literal"> |
|
1802 for |
|
1803 </code> |
|
1804 loop, to understand how it works. |
|
1805 </p> |
|
1806 <pre class="programlisting"> |
|
1807 for animal in rat cat dog man |
|
1808 do |
610 do |
1809 echo $animal |
611 echo $animal |
1810 done |
612 done</pre> |
1811 </pre> |
613 <p id="ch11Section_5_59">We just wrote a list of animals, each animal's name separated by a space and printed each name on a separate line. The variable <code class="literal">animal</code> is a dummy variable and has no significance. You could use something as lame as <code class="literal">i</code> in place of <code class="literal">animal</code>.</p> |
1812 <p id="ch11Section_5_59"> |
614 <p id="ch11Section_5_5a">Now, we use a simple <code class="literal">for</code> loop to list the files that we are interested in.</p> |
1813 We just wrote a list of animals, each animal's name separated by a space and printed each name on a separate line. The variable |
615 <pre class="programlisting"> |
1814 <code class="literal"> |
616 ls *.mp3 > list |
1815 animal |
|
1816 </code> |
|
1817 is a dummy variable and has no significance. You could use something as lame as |
|
1818 <code class="literal"> |
|
1819 i |
|
1820 </code> |
|
1821 in place of |
|
1822 <code class="literal"> |
|
1823 animal |
|
1824 </code> |
|
1825 . |
|
1826 </p> |
|
1827 <p id="ch11Section_5_5a"> |
|
1828 Now, we use a simple |
|
1829 <code class="literal"> |
|
1830 for |
|
1831 </code> |
|
1832 loop to list the files that we are interested in. |
|
1833 </p> |
|
1834 <pre class="programlisting"> |
|
1835 ls *.mp3 > list |
|
1836 for i in `cat list` |
617 for i in `cat list` |
1837 do |
618 do |
1838 echo "$i" |
619 echo "$i" |
1839 done |
620 done</pre> |
1840 </pre> |
621 <p id="ch11Section_5_5b">If your filenames contain spaces, <code class="literal">for</code> assumes each space separated word to be a single item in the list and prints it in a separate line. We could change the script slightly to overcome this problem.</p> |
1841 <p id="ch11Section_5_5b"> |
622 <pre class="programlisting"> |
1842 If your filenames contain spaces, |
623 for i in *.mp3 |
1843 <code class="literal"> |
|
1844 for |
|
1845 </code> |
|
1846 assumes each space separated word to be a single item in the list and prints it in a separate line. We could change the script slightly to overcome this problem. |
|
1847 </p> |
|
1848 <pre class="programlisting"> |
|
1849 for i in *.mp3 |
|
1850 do |
624 do |
1851 echo "$i" |
625 echo "$i" |
1852 done |
626 done</pre> |
1853 </pre> |
627 <p id="ch11Section_5_5c">Now, we have each file printed on a separate line. Depending on the files that we have we could use grep to get the relevant portion of the filenames and rename the files.</p> |
1854 <p id="ch11Section_5_5c"> |
628 <pre class="programlisting"> |
1855 Now, we have each file printed on a separate line. Depending on the files that we have we could use grep to get the relevant portion of the filenames and rename the files. |
629 for i in *.mp3 |
1856 </p> |
|
1857 <pre class="programlisting"> |
|
1858 for i in *.mp3 |
|
1859 do |
630 do |
1860 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
631 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
1861 echo "$i -> $j" |
632 echo "$i -> $j" |
1862 done |
633 done</pre> |
1863 </pre> |
634 <p id="ch11Section_5_5d">Now we just replace the echo command with a <code class="literal">mv</code> or a <code class="literal">cp</code> command.</p> |
1864 <p id="ch11Section_5_5d"> |
635 <pre class="programlisting"> |
1865 Now we just replace the echo command with a |
636 for i in *.mp3 |
1866 <code class="literal"> |
|
1867 mv |
|
1868 </code> |
|
1869 or a |
|
1870 <code class="literal"> |
|
1871 cp |
|
1872 </code> |
|
1873 command. |
|
1874 </p> |
|
1875 <pre class="programlisting"> |
|
1876 for i in *.mp3 |
|
1877 do |
637 do |
1878 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
638 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
1879 cp "$i" "$j" |
639 cp "$i" "$j" |
1880 done |
640 done</pre> |
1881 </pre> |
641 <p id="ch11Section_5_5e">As an exercise, you could try sorting the files in reverse alphabetical order and then prefix numbers to each of the filenames.</p> |
1882 <p id="ch11Section_5_5e"> |
642 </div> |
1883 As an exercise, you could try sorting the files in reverse alphabetical order and then prefix numbers to each of the filenames. |
643 <div class="section" title="6.4.2.while"> |
1884 </p> |
644 <div class="titlepage"><div><div><h4 class="title"> |
1885 </div> |
645 <a name="id2700210"></a>6.4.2.<code class="literal">while</code> |
1886 <div class="section" title="6.4.2.while"> |
646 </h4></div></div></div> |
1887 <div class="titlepage"> |
647 <p id="ch11Section_5_5f">The <code class="literal">while</code> command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully.</p> |
1888 <div> |
648 <p id="ch11Section_5_60">Let's start with the lamest example of a while loop.</p> |
1889 <div> |
649 <pre class="programlisting"> |
1890 <h4 class="title"> |
650 while true |
1891 <a name="id2939553"> |
|
1892 </a> |
|
1893 6.4.2. |
|
1894 <code class="literal"> |
|
1895 while |
|
1896 </code> |
|
1897 </h4> |
|
1898 </div> |
|
1899 </div> |
|
1900 </div> |
|
1901 <p id="ch11Section_5_5f"> |
|
1902 The |
|
1903 <code class="literal"> |
|
1904 while |
|
1905 </code> |
|
1906 command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully. |
|
1907 </p> |
|
1908 <p id="ch11Section_5_60"> |
|
1909 Let's start with the lamest example of a while loop. |
|
1910 </p> |
|
1911 <pre class="programlisting"> |
|
1912 while true |
|
1913 do |
651 do |
1914 echo "True" |
652 echo "True" |
1915 done |
653 done</pre> |
1916 </pre> |
654 <p id="ch11Section_5_61">This, as you can see, is an infinite loop that prints the <code class="literal">True</code>.</p> |
1917 <p id="ch11Section_5_61"> |
655 <p id="ch11Section_5_62">Say we wish to write a simple program that takes user input and prints it back, until the input is <code class="literal">quit</code>, which quits the program.</p> |
1918 This, as you can see, is an infinite loop that prints the |
656 <pre class="programlisting"> |
1919 <code class="literal"> |
657 while [ "$variable" != "quit" ] |
1920 True |
|
1921 </code> |
|
1922 . |
|
1923 </p> |
|
1924 <p id="ch11Section_5_62"> |
|
1925 Say we wish to write a simple program that takes user input and prints it back, until the input is |
|
1926 <code class="literal"> |
|
1927 quit |
|
1928 </code> |
|
1929 , which quits the program. |
|
1930 </p> |
|
1931 <pre class="programlisting"> |
|
1932 while [ "$variable" != "quit" ] |
|
1933 do |
658 do |
1934 read variable |
659 read variable |
1935 echo "Input - $variable" |
660 echo "Input - $variable" |
1936 done |
661 done |
1937 exit 0 |
662 exit 0</pre> |
1938 </pre> |
663 </div> |
1939 </div> |
664 <div class="section" title="6.4.3.until"> |
1940 <div class="section" title="6.4.3.until"> |
665 <div class="titlepage"><div><div><h4 class="title"> |
1941 <div class="titlepage"> |
666 <a name="id2700260"></a>6.4.3.<code class="literal">until</code> |
1942 <div> |
667 </h4></div></div></div> |
1943 <div> |
668 <p id="ch11Section_5_63">The <code class="literal">until</code> loop is similar to the <code class="literal">while</code> loop, except that it executes until the conditional command does not execute properly.</p> |
1944 <h4 class="title"> |
669 <p id="ch11Section_5_64">The infinite loop changes to the following, when <code class="literal">until</code> is used.</p> |
1945 <a name="id2939603"> |
670 <pre class="programlisting"> |
1946 </a> |
671 until false |
1947 6.4.3. |
|
1948 <code class="literal"> |
|
1949 until |
|
1950 </code> |
|
1951 </h4> |
|
1952 </div> |
|
1953 </div> |
|
1954 </div> |
|
1955 <p id="ch11Section_5_63"> |
|
1956 The |
|
1957 <code class="literal"> |
|
1958 until |
|
1959 </code> |
|
1960 loop is similar to the |
|
1961 <code class="literal"> |
|
1962 while |
|
1963 </code> |
|
1964 loop, except that it executes until the conditional command does not execute properly. |
|
1965 </p> |
|
1966 <p id="ch11Section_5_64"> |
|
1967 The infinite loop changes to the following, when |
|
1968 <code class="literal"> |
|
1969 until |
|
1970 </code> |
|
1971 is used. |
|
1972 </p> |
|
1973 <pre class="programlisting"> |
|
1974 until false |
|
1975 do |
672 do |
1976 echo "True" |
673 echo "True" |
1977 done |
674 done</pre> |
1978 </pre> |
675 <p id="ch11Section_5_65">Now lets try and use these above mentioned options provided by shell to write a utility. Until now, when we try find or locate it looks through directories and files for result. But they wont search through tar archives and zipped files. Lets create a shell script for especially looking through these files</p> |
1979 <p id="ch11Section_5_65"> |
676 <pre class="programlisting"> |
1980 Now lets try and use these above mentioned options provided by shell to write a utility. Until now, when we try find or locate it looks through directories and files for result. But they wont search through tar archives and zipped files. Lets create a shell script for especially looking through these files |
677 #!/bin/sh |
1981 </p> |
|
1982 <pre class="programlisting"> |
|
1983 #!/bin/sh |
|
1984 |
678 |
1985 #To check number of arguments being passed. |
679 #To check number of arguments being passed. |
1986 if [ $# -eq 0 ] ; then |
680 if [ $# -eq 0 ] ; then |
1987 echo "Correct usage: $0 tar-archive filename nOr $0 filename" |
681 echo "Correct usage: $0 tar-archive filename nOr $0 filename" |
1988 exit 1 |
682 exit 1 |