1 <html> |
1 <html> |
2 <head> |
2 <head> |
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
4 <title>Chapter 1. </title> |
4 <title> |
5 <link rel="stylesheet" href="hgbook.css" type="text/css"> |
5 Chapter. Using_Linux_Tools |
6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> |
6 </title> |
7 </head> |
7 <link rel="stylesheet" href="/review/support/styles.css" type="text/css" /> |
8 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" title="Chapter 1. "> |
8 <meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /> |
9 <div class="titlepage"></div> |
9 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png" /> |
10 <div class="toc"> |
10 <script type="text/javascript" src="/review/support/jquery-min.js"> |
11 <p><b>Table of Contents</b></p> |
11 </script> |
12 <dl> |
12 <script type="text/javascript" src="/review/support/form.js"> |
13 <dt><span class="article"><a href="#id2894749"></a></span></dt> |
13 </script> |
14 <dd><dl> |
14 <script type="text/javascript" src="/review/support/hsbook.js"> |
15 <dt><span class="section"><a href="#id2704840">1. Introducing Linux</a></span></dt> |
15 </script> |
16 <dd><dl> |
16 </head> |
17 <dt><span class="section"><a href="#id2702510">1.1. Historical Background</a></span></dt> |
17 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
18 <dt><span class="section"><a href="#id2945802">1.2. Design and Implications</a></span></dt> |
18 <div class="chapter" id="ch9Using_Linux_Tools"> |
19 <dt><span class="section"><a href="#id2946086">1.3. Reasons for Using Linux</a></span></dt> |
19 <div class="titlepage"> |
20 </dl></dd> |
20 </div> |
21 <dt><span class="section"><a href="#id2946227">2. Getting Started</a></span></dt> |
21 <div class="toc"> |
22 <dd><dl><dt><span class="section"><a href="#id2946236">2.1. Logging in, activating the user interface and logging out</a></span></dt></dl></dd> |
22 <p> |
23 <dt><span class="section"><a href="#id2946515">3. Basic Commands</a></span></dt> |
23 <b> |
24 <dd><dl> |
24 Table of Contents |
25 <dt><span class="section"><a href="#id2946524">3.1. ls</a></span></dt> |
25 </b> |
26 <dt><span class="section"><a href="#id2946687">3.2. date</a></span></dt> |
26 </p> |
27 <dt><span class="section"><a href="#id2946747">3.3. cd</a></span></dt> |
27 <dl> |
28 <dt><span class="section"><a href="#id2946849">3.4. who</a></span></dt> |
28 <dt> |
29 <dt><span class="section"><a href="#id2946932">3.5. mkdir</a></span></dt> |
29 <span class="article"> |
30 </dl></dd> |
30 <a href="#id2573418"> |
31 <dt><span class="section"><a href="#id2947099">4. Getting Help</a></span></dt> |
31 </a> |
32 <dd><dl> |
32 </span> |
33 <dt><span class="section"><a href="#id2947108">4.1. apropos and whatis</a></span></dt> |
33 </dt> |
34 <dt><span class="section"><a href="#id2947202">4.2. man</a></span></dt> |
34 <dd> |
35 <dt><span class="section"><a href="#id2947396">4.3. info</a></span></dt> |
35 <dl> |
36 <dt><span class="section"><a href="#id2947484">4.4. --help</a></span></dt> |
36 <dt> |
37 </dl></dd> |
37 <span class="section"> |
38 <dt><span class="section"><a href="#id2947536">5. Basic file handling</a></span></dt> |
38 <a href="#id2383509"> |
39 <dd><dl> |
39 1. Introducing Linux |
40 <dt><span class="section"><a href="#id2947545">5.1. cp</a></span></dt> |
40 </a> |
41 <dt><span class="section"><a href="#id2947858">5.2. mv</a></span></dt> |
41 </span> |
42 <dt><span class="section"><a href="#id2947990">5.3. rm</a></span></dt> |
42 </dt> |
43 </dl></dd> |
43 <dd> |
44 <dt><span class="section"><a href="#id2948158">6. Command Line Arguments</a></span></dt> |
44 <dl> |
45 <dt><span class="section"><a href="#id2948251">7. Basic Text Processing</a></span></dt> |
45 <dt> |
46 <dd><dl> |
46 <span class="section"> |
47 <dt><span class="section"><a href="#id2948260">7.1. head</a></span></dt> |
47 <a href="#id2381179"> |
48 <dt><span class="section"><a href="#id2948347">7.2. tail</a></span></dt> |
48 1.1. Historical Background |
49 <dt><span class="section"><a href="#id2948475">7.3. cut</a></span></dt> |
49 </a> |
50 <dt><span class="section"><a href="#id2948562">7.4. paste</a></span></dt> |
50 </span> |
51 </dl></dd> |
51 </dt> |
52 <dt><span class="section"><a href="#id2948682">8. Shell Meta Characters</a></span></dt> |
52 <dt> |
53 <dt><span class="section"><a href="#id2948789">9. Looking At Files</a></span></dt> |
53 <span class="section"> |
54 <dd><dl> |
54 <a href="#id2624471"> |
55 <dt><span class="section"><a href="#id2948797">9.1. cat</a></span></dt> |
55 1.2. Design and Implications |
56 <dt><span class="section"><a href="#id2948985">9.2. more</a></span></dt> |
56 </a> |
57 <dt><span class="section"><a href="#id2949096">9.3. less</a></span></dt> |
57 </span> |
58 </dl></dd> |
58 </dt> |
59 <dt><span class="section"><a href="#id2949337">10. Directory Structure</a></span></dt> |
59 <dt> |
60 <dd><dl> |
60 <span class="section"> |
61 <dt><span class="section"><a href="#id2949602">10.1. man hier</a></span></dt> |
61 <a href="#id2624755"> |
62 <dt><span class="section"><a href="#id2949618">10.2. ls -l</a></span></dt> |
62 1.3. Reasons for Using Linux |
63 </dl></dd> |
63 </a> |
64 <dt><span class="section"><a href="#id2949641">11. Permissions and Ownership</a></span></dt> |
64 </span> |
65 <dd><dl><dt><span class="section"><a href="#id2949650">11.1. chmod</a></span></dt></dl></dd> |
65 </dt> |
66 <dt><span class="section"><a href="#id2945091">12. Redirection and Piping</a></span></dt> |
66 </dl> |
67 <dd><dl> |
67 </dd> |
68 <dt><span class="section"><a href="#id2950576">12.1. Redirecting standard input and standard output</a></span></dt> |
68 <dt> |
69 <dt><span class="section"><a href="#id2950645">12.2. Piping</a></span></dt> |
69 <span class="section"> |
70 <dt><span class="section"><a href="#id2950707">12.3. Redirecting to and from the standard file handles</a></span></dt> |
70 <a href="#id2624896"> |
71 <dt><span class="section"><a href="#id2950901">12.4. Chained pipelines</a></span></dt> |
71 2. Getting Started |
72 <dt><span class="section"><a href="#id2950934">12.5. Redirect to multiple outputs</a></span></dt> |
72 </a> |
73 </dl></dd> |
73 </span> |
74 <dt><span class="section"><a href="#id2950964">13. More Text Processing</a></span></dt> |
74 </dt> |
75 <dd><dl> |
75 <dd> |
76 <dt><span class="section"><a href="#id2950973">13.1. grep</a></span></dt> |
76 <dl> |
77 <dt><span class="section"><a href="#id2951162">13.2. tr</a></span></dt> |
77 <dt> |
78 </dl></dd> |
78 <span class="section"> |
79 <dt><span class="section"><a href="#id2951309">14. Elementary Regex</a></span></dt> |
79 <a href="#id2624904"> |
80 <dd><dl><dt><span class="section"><a href="#id2951511">14.1. Lazy quantification</a></span></dt></dl></dd> |
80 2.1. Logging in, activating the user interface and logging out |
81 <dt><span class="section"><a href="#id2951586">15. One Liners</a></span></dt> |
81 </a> |
82 </dl></dd> |
82 </span> |
83 </dl> |
83 </dt> |
84 </div> |
84 </dl> |
85 <div class="article"> |
85 </dd> |
86 <div class="titlepage"> |
86 <dt> |
87 <div> |
87 <span class="section"> |
88 <div><h2 class="title"><a name="id2894749"></a></h2></div> |
88 <a href="#id2625184"> |
89 <div><h2 class="title"><a name="id2894749"></a></h2></div> |
89 3. Basic Commands |
90 </div> |
90 </a> |
91 <hr> |
91 </span> |
92 </div> |
92 </dt> |
93 <div class="toc"> |
93 <dd> |
94 <p><b>Table of Contents</b></p> |
94 <dl> |
95 <dl> |
95 <dt> |
96 <dt><span class="section"><a href="#id2704840">1. Introducing Linux</a></span></dt> |
96 <span class="section"> |
97 <dd><dl> |
97 <a href="#id2625193"> |
98 <dt><span class="section"><a href="#id2702510">1.1. Historical Background</a></span></dt> |
98 3.1. ls |
99 <dt><span class="section"><a href="#id2945802">1.2. Design and Implications</a></span></dt> |
99 </a> |
100 <dt><span class="section"><a href="#id2946086">1.3. Reasons for Using Linux</a></span></dt> |
100 </span> |
101 </dl></dd> |
101 </dt> |
102 <dt><span class="section"><a href="#id2946227">2. Getting Started</a></span></dt> |
102 <dt> |
103 <dd><dl><dt><span class="section"><a href="#id2946236">2.1. Logging in, activating the user interface and logging out</a></span></dt></dl></dd> |
103 <span class="section"> |
104 <dt><span class="section"><a href="#id2946515">3. Basic Commands</a></span></dt> |
104 <a href="#id2625356"> |
105 <dd><dl> |
105 3.2. date |
106 <dt><span class="section"><a href="#id2946524">3.1. ls</a></span></dt> |
106 </a> |
107 <dt><span class="section"><a href="#id2946687">3.2. date</a></span></dt> |
107 </span> |
108 <dt><span class="section"><a href="#id2946747">3.3. cd</a></span></dt> |
108 </dt> |
109 <dt><span class="section"><a href="#id2946849">3.4. who</a></span></dt> |
109 <dt> |
110 <dt><span class="section"><a href="#id2946932">3.5. mkdir</a></span></dt> |
110 <span class="section"> |
111 </dl></dd> |
111 <a href="#id2625416"> |
112 <dt><span class="section"><a href="#id2947099">4. Getting Help</a></span></dt> |
112 3.3. cd |
113 <dd><dl> |
113 </a> |
114 <dt><span class="section"><a href="#id2947108">4.1. apropos and whatis</a></span></dt> |
114 </span> |
115 <dt><span class="section"><a href="#id2947202">4.2. man</a></span></dt> |
115 </dt> |
116 <dt><span class="section"><a href="#id2947396">4.3. info</a></span></dt> |
116 <dt> |
117 <dt><span class="section"><a href="#id2947484">4.4. --help</a></span></dt> |
117 <span class="section"> |
118 </dl></dd> |
118 <a href="#id2625517"> |
119 <dt><span class="section"><a href="#id2947536">5. Basic file handling</a></span></dt> |
119 3.4. who |
120 <dd><dl> |
120 </a> |
121 <dt><span class="section"><a href="#id2947545">5.1. cp</a></span></dt> |
121 </span> |
122 <dt><span class="section"><a href="#id2947858">5.2. mv</a></span></dt> |
122 </dt> |
123 <dt><span class="section"><a href="#id2947990">5.3. rm</a></span></dt> |
123 <dt> |
124 </dl></dd> |
124 <span class="section"> |
125 <dt><span class="section"><a href="#id2948158">6. Command Line Arguments</a></span></dt> |
125 <a href="#id2625600"> |
126 <dt><span class="section"><a href="#id2948251">7. Basic Text Processing</a></span></dt> |
126 3.5. mkdir |
127 <dd><dl> |
127 </a> |
128 <dt><span class="section"><a href="#id2948260">7.1. head</a></span></dt> |
128 </span> |
129 <dt><span class="section"><a href="#id2948347">7.2. tail</a></span></dt> |
129 </dt> |
130 <dt><span class="section"><a href="#id2948475">7.3. cut</a></span></dt> |
130 </dl> |
131 <dt><span class="section"><a href="#id2948562">7.4. paste</a></span></dt> |
131 </dd> |
132 </dl></dd> |
132 <dt> |
133 <dt><span class="section"><a href="#id2948682">8. Shell Meta Characters</a></span></dt> |
133 <span class="section"> |
134 <dt><span class="section"><a href="#id2948789">9. Looking At Files</a></span></dt> |
134 <a href="#id2625768"> |
135 <dd><dl> |
135 4. Getting Help |
136 <dt><span class="section"><a href="#id2948797">9.1. cat</a></span></dt> |
136 </a> |
137 <dt><span class="section"><a href="#id2948985">9.2. more</a></span></dt> |
137 </span> |
138 <dt><span class="section"><a href="#id2949096">9.3. less</a></span></dt> |
138 </dt> |
139 </dl></dd> |
139 <dd> |
140 <dt><span class="section"><a href="#id2949337">10. Directory Structure</a></span></dt> |
140 <dl> |
141 <dd><dl> |
141 <dt> |
142 <dt><span class="section"><a href="#id2949602">10.1. man hier</a></span></dt> |
142 <span class="section"> |
143 <dt><span class="section"><a href="#id2949618">10.2. ls -l</a></span></dt> |
143 <a href="#id2625776"> |
144 </dl></dd> |
144 4.1. apropos and whatis |
145 <dt><span class="section"><a href="#id2949641">11. Permissions and Ownership</a></span></dt> |
145 </a> |
146 <dd><dl><dt><span class="section"><a href="#id2949650">11.1. chmod</a></span></dt></dl></dd> |
146 </span> |
147 <dt><span class="section"><a href="#id2945091">12. Redirection and Piping</a></span></dt> |
147 </dt> |
148 <dd><dl> |
148 <dt> |
149 <dt><span class="section"><a href="#id2950576">12.1. Redirecting standard input and standard output</a></span></dt> |
149 <span class="section"> |
150 <dt><span class="section"><a href="#id2950645">12.2. Piping</a></span></dt> |
150 <a href="#id2625871"> |
151 <dt><span class="section"><a href="#id2950707">12.3. Redirecting to and from the standard file handles</a></span></dt> |
151 4.2. man |
152 <dt><span class="section"><a href="#id2950901">12.4. Chained pipelines</a></span></dt> |
152 </a> |
153 <dt><span class="section"><a href="#id2950934">12.5. Redirect to multiple outputs</a></span></dt> |
153 </span> |
154 </dl></dd> |
154 </dt> |
155 <dt><span class="section"><a href="#id2950964">13. More Text Processing</a></span></dt> |
155 <dt> |
156 <dd><dl> |
156 <span class="section"> |
157 <dt><span class="section"><a href="#id2950973">13.1. grep</a></span></dt> |
157 <a href="#id2626065"> |
158 <dt><span class="section"><a href="#id2951162">13.2. tr</a></span></dt> |
158 4.3. info |
159 </dl></dd> |
159 </a> |
160 <dt><span class="section"><a href="#id2951309">14. Elementary Regex</a></span></dt> |
160 </span> |
161 <dd><dl><dt><span class="section"><a href="#id2951511">14.1. Lazy quantification</a></span></dt></dl></dd> |
161 </dt> |
162 <dt><span class="section"><a href="#id2951586">15. One Liners</a></span></dt> |
162 <dt> |
163 </dl> |
163 <span class="section"> |
164 </div> |
164 <a href="#id2626152"> |
165 <div class="section" title="1. Introducing Linux"> |
165 4.4. --help |
166 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
166 </a> |
167 <a name="id2704840"></a>1. Introducing Linux</h2></div></div></div> |
167 </span> |
168 <p><a name="ch9Using_Linux_Tools_1"></a>(Attribution : A significant chunk of the content under this section is based on data from Wikipedia and the Linux Documentation Project)</p> |
168 </dt> |
169 <p><a name="ch9Using_Linux_Tools_2"></a>Linux (usually pronounced ˈlɪnəks') is a generic term referring to Unix-like computer operating systems based on the Linux kernel, where a kernel is the intermediate layer between the hardware and the applications. The kernel is, on an abstract level, the core of (most) operating systems, that manages the various system resources. The development of the Linux OS is considered the basis for Free and Open Source Software (FOSS) collaboration since typically the underlying source code can be used, modified freely, and redistributed by anyone under the terms of the GNU (a recursive acronym for "GNU's Not Unix!") Global Public License (GPL) and other free software licences. This freedom to access and reuse various components of a system, is one of the primary reasons for the popularity of Linux.</p> |
169 </dl> |
170 <p><a name="ch9Using_Linux_Tools_3"></a>Linux is installed on a variety of computer hardware, that include mobile phones, embedded devices and supercomputers, but is infamous for its use in servers.</p> |
170 </dd> |
171 <p><a name="ch9Using_Linux_Tools_4"></a>The name "Linux" comes from the Linux kernel, originally written in 1991 by Linus Torvalds. The rest of the system usually comprises components such as the Apache HTTP Server, the X Window System, the GNOME and KDE desktop environments, and utilities and libraries from the GNU Project (announced in 1983 by Richard Stallman). Commonly-used applications with desktop Linux systems include the Mozilla Firefox web-browser and the OpenOffice.org office application suite. The GNU contribution is the basis for the Free Software Foundation's preferred name GNU/Linux. The kernel's mascot is a penguin named "Tux". Mozilla Firefox and OpenOffice.org are open-source projects which can be run on most Operating Systems, including proprietary ones.</p> |
171 <dt> |
172 <div class="section" title="1.1. Historical Background"> |
172 <span class="section"> |
173 <div class="titlepage"><div><div><h3 class="title"> |
173 <a href="#id2626205"> |
174 <a name="id2702510"></a>1.1. Historical Background</h3></div></div></div> |
174 5. Basic file handling |
175 <div class="section" title="1.1.1. Events leading to the creation"> |
175 </a> |
176 <div class="titlepage"><div><div><h4 class="title"> |
176 </span> |
177 <a name="id2893239"></a>1.1.1. Events leading to the creation</h4></div></div></div> |
177 </dt> |
178 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
178 <dd> |
179 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_5"></a>The Unix operating system was developed in the 1960s and released for public use in 1970. Its accessibility and portability caused it to be widely adopted, copied and modified by academic institutions and businesses. Its design became influential to authors of other systems. Other free operating systems include the Berkeley Software Distribution (BSD), developed at the University of California at Berkeley, and MINIX which was released by Andrew S. Tanenbaum. The development and adoption of BSD and MINIX were limited due to various reasons, and this lack of a widely-adopted and free kernel triggered Linus Torvalds into starting his project.</p></li> |
179 <dl> |
180 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_6"></a>In 1983, Richard Stallman started the GNU project with the goal of creating a free UNIX-like operating system. As part of this work, he wrote the GNU General Public License (GPL). By the early 1990s there was almost enough available software to create a full operating system. However, the GNU kernel, called Hurd, failed to attract enough attention from developers leaving GNU incomplete.</p></li> |
180 <dt> |
181 </ul></div> |
181 <span class="section"> |
182 </div> |
182 <a href="#id2626214"> |
183 <div class="section" title="1.1.2. The Creation of Linux"> |
183 5.1. cp |
184 <div class="titlepage"><div><div><h4 class="title"> |
184 </a> |
185 <a name="id2945768"></a>1.1.2. The Creation of Linux</h4></div></div></div> |
185 </span> |
186 <p><a name="ch9Using_Linux_Tools_7"></a>In 1991, Linus Torvalds began a project at the University of Helsinki that later became the Linux kernel. It was initially a terminal (command-line) emulator, which Torvalds used to access the large UNIX servers of the university. He wrote the program targeting just the hardware he was using and independent of an operating system because he wanted to use the functions of his computer with an 80386 processor. Development was done on Minix using the GNU C compiler. This application is still the main choice for compiling Linux today (although the code can be built with other compilers, such as the Intel C Compiler).</p> |
186 </dt> |
187 <p><a name="ch9Using_Linux_Tools_8"></a>Torvalds continues to direct the development of the kernel. Stallman heads the Free Software Foundation, which in turn supports the GNU components. Finally, individuals and corporations develop third-party non-GNU components, which constitute a vast body of work and including kernel modules, and user applications and libraries. Linux vendors and communities combine and distribute the kernel, GNU components, and non-GNU components, with additional package management software in the form of Linux distributions.</p> |
187 <dt> |
188 </div> |
188 <span class="section"> |
189 </div> |
189 <a href="#id2626526"> |
190 <div class="section" title="1.2. Design and Implications"> |
190 5.2. mv |
191 <div class="titlepage"><div><div><h3 class="title"> |
191 </a> |
192 <a name="id2945802"></a>1.2. Design and Implications</h3></div></div></div> |
192 </span> |
193 <p><a name="ch9Using_Linux_Tools_9"></a>A Linux-based system is a modular Unix-like operating system, deriving much of its basic design from principles established in Unix earlier. Such a system uses a monolithic kernel, called the Linux kernel, which handles process control, networking, and peripheral and file system access. Device drivers are integrated directly with the kernel. Separate projects that interface with the kernel provide much of the system's higher-level functionality. The GNU userland is an important part of most Linux-based systems, providing the most common implementation of the C library, a popular shell, and many of the common Unix tools which carry out many basic operating system tasks. The graphical user interface (or GUI) used by most Linux systems is based on the "X Window System".</p> |
193 </dt> |
194 <div class="section" title="1.2.1. User Interface"> |
194 <dt> |
195 <div class="titlepage"><div><div><h4 class="title"> |
195 <span class="section"> |
196 <a name="id2945827"></a>1.2.1. User Interface</h4></div></div></div> |
196 <a href="#id2626659"> |
197 <p><a name="ch9Using_Linux_Tools_a"></a>Users can control a Linux-based system through a command line interface (or CLI), a graphical user interface (or GUI), or through controls attached to the associated hardware (this is common for embedded systems). For desktop systems, the default mode is usually the GUI. On desktop machines, "KDE", "GNOME" and "Xfce" are the most popular user interfaces,though a variety of additional user interfaces exist. Most popular user interfaces run on top of the "X Window System" (or X), which enables a graphical application running on one machine to be displayed and controlled from another in a network.</p> |
197 5.3. rm |
198 <p><a name="ch9Using_Linux_Tools_b"></a>A Linux system also provides a CLI of some sort through a shell, which is the traditional way of interacting with a Unix system. A Linux distribution specialized for servers may use the CLI as its only interface. A “headless system” (system run without even a monitor) can be controlled by the command line via a remote-control protocol such as SSH or telnet. The CLI is particularly suited for automation of repetitive or delayed tasks, and provides very simple inter-process communication. A graphical terminal emulator program is often used to access the CLI from a Linux desktop.</p> |
198 </a> |
199 </div> |
199 </span> |
200 <div class="section" title="1.2.2. Development"> |
200 </dt> |
201 <div class="titlepage"><div><div><h4 class="title"> |
201 </dl> |
202 <a name="id2945881"></a>1.2.2. Development</h4></div></div></div> |
202 </dd> |
203 <p><a name="ch9Using_Linux_Tools_c"></a>The primary difference between Linux and many other popular contemporary operating systems is that the Linux kernel and other components are free and open source software. Linux is not the only such operating system, although it is by far the most widely used. Some free and open source software licenses are based on the principle of "copyleft", a kind of reciprocity: any work derived from a copyleft piece of software must also be copyleft itself. The most common free software license, the GNU GPL, is a form of copyleft, and is used for the Linux kernel and many of the components from the GNU project.</p> |
203 <dt> |
204 <p><a name="ch9Using_Linux_Tools_d"></a>Linux based distributions are intended by developers for interoperability with other operating systems and established computing standards. Linux systems adhere to POSIX, SUS, ISO and ANSI standards where possible, although to date only one Linux distribution has been POSIX.1 certified, Linux-FT.Free software projects, although developed in a collaborative fashion, are often produced independently of each other. The fact that the software licenses explicitly permit redistribution, however, provides a basis for larger scale projects that collect the software produced by stand-alone projects and make it available all at once in the form of a Linux distribution.</p> |
204 <span class="section"> |
205 <p><a name="ch9Using_Linux_Tools_e"></a>A Linux distribution, commonly called a "distro", is a project that manages a remote collection of system software and application software packages available for download and installation through a network connection. This allows the user to adapt the operating system to his/her specific needs. Distributions are maintained by individuals, loose-knit teams, volunteer organizations, and commercial entities. A distribution can be installed using a CD that contains distribution-specific software for initial system installation and configuration. A package manager such as Synaptic or YAST allows later package upgrades and installations. A distribution is responsible for the default configuration of the installed Linux kernel, general system security, and more generally integration of the different software packages into a coherent whole.</p> |
205 <a href="#id2626827"> |
206 </div> |
206 6. Command Line Arguments |
207 <div class="section" title="1.2.3. Community"> |
207 </a> |
208 <div class="titlepage"><div><div><h4 class="title"> |
208 </span> |
209 <a name="id2945948"></a>1.2.3. Community</h4></div></div></div> |
209 </dt> |
210 <p><a name="ch9Using_Linux_Tools_f"></a>A distribution is largely driven by its developer and user communities. Some vendors develop and fund their distributions on a volunteer basis. Examples include Debian and the Debian-based, Ubuntu. Others maintain a community version of their commercial distributions, as Red Hat does with Fedora.</p> |
210 <dt> |
211 <p><a name="ch9Using_Linux_Tools_10"></a>In many cities and regions, local associations known as Linux Users Groups (LUGs) seek to promote their preferred distribution and by extension free software. They hold meetings and provide free demonstrations, training, technical support, and operating system installation to new users. Many Internet communities also provide support to Linux users and developers. Most distributions and free software / open source projects have IRC (Internet Relay Chat) chatrooms or newsgroups. Online forums are another means for support. Linux distributions host mailing lists; commonly there will be a specific topic such as usage or development for a given list. All these can be found simply by running an appropriate search on Google.</p> |
211 <span class="section"> |
212 <p><a name="ch9Using_Linux_Tools_11"></a>Although Linux distributions are generally available without charge, several large corporations sell, support, and contribute to the development of the components of the system and of free software. These include Dell, IBM, HP, Oracle, Sun Microsystems, Novell, Nokia. A number of corporations, notably Red Hat, have built their entire business around Linux distributions.</p> |
212 <a href="#id2626920"> |
213 </div> |
213 7. Basic Text Processing |
214 <div class="section" title="1.2.4. Can I make a profit out of running a business involving Linux?"> |
214 </a> |
215 <div class="titlepage"><div><div><h4 class="title"> |
215 </span> |
216 <a name="id2945993"></a>1.2.4. Can I make a profit out of running a business involving Linux?</h4></div></div></div> |
216 </dt> |
217 <p><a name="ch9Using_Linux_Tools_12"></a>The answer is, "Yes!". The free software licenses, on which the various software packages of a distribution built on the Linux kernel are based, explicitly accommodate and encourage commercialization; the relationship between a Linux distribution as a whole and individual vendors may be seen as symbiotic. One common business model of commercial suppliers is charging for support, especially for business users. A number of companies also offer a specialized business version of their distribution, which adds proprietary support packages and tools to administer higher numbers of installations or to simplify administrative tasks. Another business model is to give away the software in order to sell hardware. Examples of corporations that are extensively (and sometimes exclusively) open-source and Linux-powered , with successful revenue generation models involving these, are Google, SUN, Mozilla, etc.</p> |
217 <dd> |
218 </div> |
218 <dl> |
219 <div class="section" title="1.2.5. Programming on Linux"> |
219 <dt> |
220 <div class="titlepage"><div><div><h4 class="title"> |
220 <span class="section"> |
221 <a name="id2946039"></a>1.2.5. Programming on Linux</h4></div></div></div> |
221 <a href="#id2626929"> |
222 <p><a name="ch9Using_Linux_Tools_13"></a>Most Linux distributions support dozens of programming languages. The most common collection of utilities for building both Linux applications and operating system programs is found within the GNU toolchain, which includes the GNU Compiler Collection (GCC) and the GNU build system. Amongst others, GCC provides compilers for Ada, C, C++, Java, and Fortran. The Linux kernel itself is written to be compiled with GCC. Proprietary compilers for Linux include the Intel C++ Compiler, Sun Studio, and IBM XL C/C++ Compiler.</p> |
222 7.1. head |
223 <p><a name="ch9Using_Linux_Tools_14"></a>Most distributions also include support for PHP, Perl, Ruby, Python and other dynamic languages. Examples of languages that are less common, but still supported, are C# via the Mono project, sponsored by Novell, and Scheme. A number of Java Virtual Machines and development kits run on Linux, including the original Sun Microsystems JVM (HotSpot), and IBM's J2SE RE, as well as many open-source projects like Kaffe.</p> |
223 </a> |
224 <p><a name="ch9Using_Linux_Tools_15"></a>The two main frameworks for developing graphical applications are those of GNOME and KDE. These projects are based on the GTK+ and Qt widget toolkits, respectively, which can also be used independently of the larger framework. Both support a wide variety of languages. There are a number of Integrated Development Environments (IDEs) available including Anjuta, Code::Blocks, Eclipse, KDevelop, Lazarus, MonoDevelop, NetBeans, and Omnis Studio while the long-established editors Vim and Emacs remain popular.</p> |
224 </span> |
225 </div> |
225 </dt> |
226 </div> |
226 <dt> |
227 <div class="section" title="1.3. Reasons for Using Linux"> |
227 <span class="section"> |
228 <div class="titlepage"><div><div><h3 class="title"> |
228 <a href="#id2627016"> |
229 <a name="id2946086"></a>1.3. Reasons for Using Linux</h3></div></div></div> |
229 7.2. tail |
230 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_16"></a>Linux is free:</p></li></ul></div> |
230 </a> |
231 <p><a name="ch9Using_Linux_Tools_17"></a>As in "free beer". Linux can be downloaded in its entirety from the Internet completely for free. No registration fees, no costs per user, free updates, and freely available source code in case you want to change the behavior of your system. |
231 </span> |
|
232 </dt> |
|
233 <dt> |
|
234 <span class="section"> |
|
235 <a href="#id2627144"> |
|
236 7.3. cut |
|
237 </a> |
|
238 </span> |
|
239 </dt> |
|
240 <dt> |
|
241 <span class="section"> |
|
242 <a href="#id2627231"> |
|
243 7.4. paste |
|
244 </a> |
|
245 </span> |
|
246 </dt> |
|
247 </dl> |
|
248 </dd> |
|
249 <dt> |
|
250 <span class="section"> |
|
251 <a href="#id2627351"> |
|
252 8. Shell Meta Characters |
|
253 </a> |
|
254 </span> |
|
255 </dt> |
|
256 <dt> |
|
257 <span class="section"> |
|
258 <a href="#id2627457"> |
|
259 9. Looking At Files |
|
260 </a> |
|
261 </span> |
|
262 </dt> |
|
263 <dd> |
|
264 <dl> |
|
265 <dt> |
|
266 <span class="section"> |
|
267 <a href="#id2627466"> |
|
268 9.1. cat |
|
269 </a> |
|
270 </span> |
|
271 </dt> |
|
272 <dt> |
|
273 <span class="section"> |
|
274 <a href="#id2627654"> |
|
275 9.2. more |
|
276 </a> |
|
277 </span> |
|
278 </dt> |
|
279 <dt> |
|
280 <span class="section"> |
|
281 <a href="#id2627765"> |
|
282 9.3. less |
|
283 </a> |
|
284 </span> |
|
285 </dt> |
|
286 </dl> |
|
287 </dd> |
|
288 <dt> |
|
289 <span class="section"> |
|
290 <a href="#id2628006"> |
|
291 10. Directory Structure |
|
292 </a> |
|
293 </span> |
|
294 </dt> |
|
295 <dd> |
|
296 <dl> |
|
297 <dt> |
|
298 <span class="section"> |
|
299 <a href="#id2628270"> |
|
300 10.1. man hier |
|
301 </a> |
|
302 </span> |
|
303 </dt> |
|
304 <dt> |
|
305 <span class="section"> |
|
306 <a href="#id2628287"> |
|
307 10.2. ls -l |
|
308 </a> |
|
309 </span> |
|
310 </dt> |
|
311 </dl> |
|
312 </dd> |
|
313 <dt> |
|
314 <span class="section"> |
|
315 <a href="#id2628310"> |
|
316 11. Permissions and Ownership |
|
317 </a> |
|
318 </span> |
|
319 </dt> |
|
320 <dd> |
|
321 <dl> |
|
322 <dt> |
|
323 <span class="section"> |
|
324 <a href="#id2628319"> |
|
325 11.1. chmod |
|
326 </a> |
|
327 </span> |
|
328 </dt> |
|
329 </dl> |
|
330 </dd> |
|
331 <dt> |
|
332 <span class="section"> |
|
333 <a href="#id2623759"> |
|
334 12. Redirection and Piping |
|
335 </a> |
|
336 </span> |
|
337 </dt> |
|
338 <dd> |
|
339 <dl> |
|
340 <dt> |
|
341 <span class="section"> |
|
342 <a href="#id2629245"> |
|
343 12.1. Redirecting standard input and standard output |
|
344 </a> |
|
345 </span> |
|
346 </dt> |
|
347 <dt> |
|
348 <span class="section"> |
|
349 <a href="#id2629314"> |
|
350 12.2. Piping |
|
351 </a> |
|
352 </span> |
|
353 </dt> |
|
354 <dt> |
|
355 <span class="section"> |
|
356 <a href="#id2629376"> |
|
357 12.3. Redirecting to and from the standard file handles |
|
358 </a> |
|
359 </span> |
|
360 </dt> |
|
361 <dt> |
|
362 <span class="section"> |
|
363 <a href="#id2629569"> |
|
364 12.4. Chained pipelines |
|
365 </a> |
|
366 </span> |
|
367 </dt> |
|
368 <dt> |
|
369 <span class="section"> |
|
370 <a href="#id2629602"> |
|
371 12.5. Redirect to multiple outputs |
|
372 </a> |
|
373 </span> |
|
374 </dt> |
|
375 </dl> |
|
376 </dd> |
|
377 <dt> |
|
378 <span class="section"> |
|
379 <a href="#id2629633"> |
|
380 13. More Text Processing |
|
381 </a> |
|
382 </span> |
|
383 </dt> |
|
384 <dd> |
|
385 <dl> |
|
386 <dt> |
|
387 <span class="section"> |
|
388 <a href="#id2629642"> |
|
389 13.1. grep |
|
390 </a> |
|
391 </span> |
|
392 </dt> |
|
393 <dt> |
|
394 <span class="section"> |
|
395 <a href="#id2629831"> |
|
396 13.2. tr |
|
397 </a> |
|
398 </span> |
|
399 </dt> |
|
400 </dl> |
|
401 </dd> |
|
402 <dt> |
|
403 <span class="section"> |
|
404 <a href="#id2629978"> |
|
405 14. Elementary Regex |
|
406 </a> |
|
407 </span> |
|
408 </dt> |
|
409 <dd> |
|
410 <dl> |
|
411 <dt> |
|
412 <span class="section"> |
|
413 <a href="#id2630180"> |
|
414 14.1. Lazy quantification |
|
415 </a> |
|
416 </span> |
|
417 </dt> |
|
418 </dl> |
|
419 </dd> |
|
420 <dt> |
|
421 <span class="section"> |
|
422 <a href="#id2630255"> |
|
423 15. One Liners |
|
424 </a> |
|
425 </span> |
|
426 </dt> |
|
427 </dl> |
|
428 </dd> |
|
429 </dl> |
|
430 </div> |
|
431 <div class="article"> |
|
432 <div class="titlepage"> |
|
433 <div> |
|
434 <div> |
|
435 <h2 class="title" id="id2573418"> |
|
436 </h2> |
|
437 </div> |
|
438 <div> |
|
439 <h2 class="title" id="id2573418"> |
|
440 </h2> |
|
441 </div> |
|
442 </div> |
|
443 <hr /> |
|
444 </div> |
|
445 <div class="toc"> |
|
446 <p> |
|
447 <b> |
|
448 Table of Contents |
|
449 </b> |
|
450 </p> |
|
451 <dl> |
|
452 <dt> |
|
453 <span class="section"> |
|
454 <a href="#id2383509"> |
|
455 1. Introducing Linux |
|
456 </a> |
|
457 </span> |
|
458 </dt> |
|
459 <dd> |
|
460 <dl> |
|
461 <dt> |
|
462 <span class="section"> |
|
463 <a href="#id2381179"> |
|
464 1.1. Historical Background |
|
465 </a> |
|
466 </span> |
|
467 </dt> |
|
468 <dt> |
|
469 <span class="section"> |
|
470 <a href="#id2624471"> |
|
471 1.2. Design and Implications |
|
472 </a> |
|
473 </span> |
|
474 </dt> |
|
475 <dt> |
|
476 <span class="section"> |
|
477 <a href="#id2624755"> |
|
478 1.3. Reasons for Using Linux |
|
479 </a> |
|
480 </span> |
|
481 </dt> |
|
482 </dl> |
|
483 </dd> |
|
484 <dt> |
|
485 <span class="section"> |
|
486 <a href="#id2624896"> |
|
487 2. Getting Started |
|
488 </a> |
|
489 </span> |
|
490 </dt> |
|
491 <dd> |
|
492 <dl> |
|
493 <dt> |
|
494 <span class="section"> |
|
495 <a href="#id2624904"> |
|
496 2.1. Logging in, activating the user interface and logging out |
|
497 </a> |
|
498 </span> |
|
499 </dt> |
|
500 </dl> |
|
501 </dd> |
|
502 <dt> |
|
503 <span class="section"> |
|
504 <a href="#id2625184"> |
|
505 3. Basic Commands |
|
506 </a> |
|
507 </span> |
|
508 </dt> |
|
509 <dd> |
|
510 <dl> |
|
511 <dt> |
|
512 <span class="section"> |
|
513 <a href="#id2625193"> |
|
514 3.1. ls |
|
515 </a> |
|
516 </span> |
|
517 </dt> |
|
518 <dt> |
|
519 <span class="section"> |
|
520 <a href="#id2625356"> |
|
521 3.2. date |
|
522 </a> |
|
523 </span> |
|
524 </dt> |
|
525 <dt> |
|
526 <span class="section"> |
|
527 <a href="#id2625416"> |
|
528 3.3. cd |
|
529 </a> |
|
530 </span> |
|
531 </dt> |
|
532 <dt> |
|
533 <span class="section"> |
|
534 <a href="#id2625517"> |
|
535 3.4. who |
|
536 </a> |
|
537 </span> |
|
538 </dt> |
|
539 <dt> |
|
540 <span class="section"> |
|
541 <a href="#id2625600"> |
|
542 3.5. mkdir |
|
543 </a> |
|
544 </span> |
|
545 </dt> |
|
546 </dl> |
|
547 </dd> |
|
548 <dt> |
|
549 <span class="section"> |
|
550 <a href="#id2625768"> |
|
551 4. Getting Help |
|
552 </a> |
|
553 </span> |
|
554 </dt> |
|
555 <dd> |
|
556 <dl> |
|
557 <dt> |
|
558 <span class="section"> |
|
559 <a href="#id2625776"> |
|
560 4.1. apropos and whatis |
|
561 </a> |
|
562 </span> |
|
563 </dt> |
|
564 <dt> |
|
565 <span class="section"> |
|
566 <a href="#id2625871"> |
|
567 4.2. man |
|
568 </a> |
|
569 </span> |
|
570 </dt> |
|
571 <dt> |
|
572 <span class="section"> |
|
573 <a href="#id2626065"> |
|
574 4.3. info |
|
575 </a> |
|
576 </span> |
|
577 </dt> |
|
578 <dt> |
|
579 <span class="section"> |
|
580 <a href="#id2626152"> |
|
581 4.4. --help |
|
582 </a> |
|
583 </span> |
|
584 </dt> |
|
585 </dl> |
|
586 </dd> |
|
587 <dt> |
|
588 <span class="section"> |
|
589 <a href="#id2626205"> |
|
590 5. Basic file handling |
|
591 </a> |
|
592 </span> |
|
593 </dt> |
|
594 <dd> |
|
595 <dl> |
|
596 <dt> |
|
597 <span class="section"> |
|
598 <a href="#id2626214"> |
|
599 5.1. cp |
|
600 </a> |
|
601 </span> |
|
602 </dt> |
|
603 <dt> |
|
604 <span class="section"> |
|
605 <a href="#id2626526"> |
|
606 5.2. mv |
|
607 </a> |
|
608 </span> |
|
609 </dt> |
|
610 <dt> |
|
611 <span class="section"> |
|
612 <a href="#id2626659"> |
|
613 5.3. rm |
|
614 </a> |
|
615 </span> |
|
616 </dt> |
|
617 </dl> |
|
618 </dd> |
|
619 <dt> |
|
620 <span class="section"> |
|
621 <a href="#id2626827"> |
|
622 6. Command Line Arguments |
|
623 </a> |
|
624 </span> |
|
625 </dt> |
|
626 <dt> |
|
627 <span class="section"> |
|
628 <a href="#id2626920"> |
|
629 7. Basic Text Processing |
|
630 </a> |
|
631 </span> |
|
632 </dt> |
|
633 <dd> |
|
634 <dl> |
|
635 <dt> |
|
636 <span class="section"> |
|
637 <a href="#id2626929"> |
|
638 7.1. head |
|
639 </a> |
|
640 </span> |
|
641 </dt> |
|
642 <dt> |
|
643 <span class="section"> |
|
644 <a href="#id2627016"> |
|
645 7.2. tail |
|
646 </a> |
|
647 </span> |
|
648 </dt> |
|
649 <dt> |
|
650 <span class="section"> |
|
651 <a href="#id2627144"> |
|
652 7.3. cut |
|
653 </a> |
|
654 </span> |
|
655 </dt> |
|
656 <dt> |
|
657 <span class="section"> |
|
658 <a href="#id2627231"> |
|
659 7.4. paste |
|
660 </a> |
|
661 </span> |
|
662 </dt> |
|
663 </dl> |
|
664 </dd> |
|
665 <dt> |
|
666 <span class="section"> |
|
667 <a href="#id2627351"> |
|
668 8. Shell Meta Characters |
|
669 </a> |
|
670 </span> |
|
671 </dt> |
|
672 <dt> |
|
673 <span class="section"> |
|
674 <a href="#id2627457"> |
|
675 9. Looking At Files |
|
676 </a> |
|
677 </span> |
|
678 </dt> |
|
679 <dd> |
|
680 <dl> |
|
681 <dt> |
|
682 <span class="section"> |
|
683 <a href="#id2627466"> |
|
684 9.1. cat |
|
685 </a> |
|
686 </span> |
|
687 </dt> |
|
688 <dt> |
|
689 <span class="section"> |
|
690 <a href="#id2627654"> |
|
691 9.2. more |
|
692 </a> |
|
693 </span> |
|
694 </dt> |
|
695 <dt> |
|
696 <span class="section"> |
|
697 <a href="#id2627765"> |
|
698 9.3. less |
|
699 </a> |
|
700 </span> |
|
701 </dt> |
|
702 </dl> |
|
703 </dd> |
|
704 <dt> |
|
705 <span class="section"> |
|
706 <a href="#id2628006"> |
|
707 10. Directory Structure |
|
708 </a> |
|
709 </span> |
|
710 </dt> |
|
711 <dd> |
|
712 <dl> |
|
713 <dt> |
|
714 <span class="section"> |
|
715 <a href="#id2628270"> |
|
716 10.1. man hier |
|
717 </a> |
|
718 </span> |
|
719 </dt> |
|
720 <dt> |
|
721 <span class="section"> |
|
722 <a href="#id2628287"> |
|
723 10.2. ls -l |
|
724 </a> |
|
725 </span> |
|
726 </dt> |
|
727 </dl> |
|
728 </dd> |
|
729 <dt> |
|
730 <span class="section"> |
|
731 <a href="#id2628310"> |
|
732 11. Permissions and Ownership |
|
733 </a> |
|
734 </span> |
|
735 </dt> |
|
736 <dd> |
|
737 <dl> |
|
738 <dt> |
|
739 <span class="section"> |
|
740 <a href="#id2628319"> |
|
741 11.1. chmod |
|
742 </a> |
|
743 </span> |
|
744 </dt> |
|
745 </dl> |
|
746 </dd> |
|
747 <dt> |
|
748 <span class="section"> |
|
749 <a href="#id2623759"> |
|
750 12. Redirection and Piping |
|
751 </a> |
|
752 </span> |
|
753 </dt> |
|
754 <dd> |
|
755 <dl> |
|
756 <dt> |
|
757 <span class="section"> |
|
758 <a href="#id2629245"> |
|
759 12.1. Redirecting standard input and standard output |
|
760 </a> |
|
761 </span> |
|
762 </dt> |
|
763 <dt> |
|
764 <span class="section"> |
|
765 <a href="#id2629314"> |
|
766 12.2. Piping |
|
767 </a> |
|
768 </span> |
|
769 </dt> |
|
770 <dt> |
|
771 <span class="section"> |
|
772 <a href="#id2629376"> |
|
773 12.3. Redirecting to and from the standard file handles |
|
774 </a> |
|
775 </span> |
|
776 </dt> |
|
777 <dt> |
|
778 <span class="section"> |
|
779 <a href="#id2629569"> |
|
780 12.4. Chained pipelines |
|
781 </a> |
|
782 </span> |
|
783 </dt> |
|
784 <dt> |
|
785 <span class="section"> |
|
786 <a href="#id2629602"> |
|
787 12.5. Redirect to multiple outputs |
|
788 </a> |
|
789 </span> |
|
790 </dt> |
|
791 </dl> |
|
792 </dd> |
|
793 <dt> |
|
794 <span class="section"> |
|
795 <a href="#id2629633"> |
|
796 13. More Text Processing |
|
797 </a> |
|
798 </span> |
|
799 </dt> |
|
800 <dd> |
|
801 <dl> |
|
802 <dt> |
|
803 <span class="section"> |
|
804 <a href="#id2629642"> |
|
805 13.1. grep |
|
806 </a> |
|
807 </span> |
|
808 </dt> |
|
809 <dt> |
|
810 <span class="section"> |
|
811 <a href="#id2629831"> |
|
812 13.2. tr |
|
813 </a> |
|
814 </span> |
|
815 </dt> |
|
816 </dl> |
|
817 </dd> |
|
818 <dt> |
|
819 <span class="section"> |
|
820 <a href="#id2629978"> |
|
821 14. Elementary Regex |
|
822 </a> |
|
823 </span> |
|
824 </dt> |
|
825 <dd> |
|
826 <dl> |
|
827 <dt> |
|
828 <span class="section"> |
|
829 <a href="#id2630180"> |
|
830 14.1. Lazy quantification |
|
831 </a> |
|
832 </span> |
|
833 </dt> |
|
834 </dl> |
|
835 </dd> |
|
836 <dt> |
|
837 <span class="section"> |
|
838 <a href="#id2630255"> |
|
839 15. One Liners |
|
840 </a> |
|
841 </span> |
|
842 </dt> |
|
843 </dl> |
|
844 </div> |
|
845 <div class="section" title="1.Introducing Linux"> |
|
846 <div class="titlepage"> |
|
847 <div> |
|
848 <div> |
|
849 <h2 class="title" style="clear: both"> |
|
850 <a name="id2383509"> |
|
851 </a> |
|
852 1.Introducing Linux |
|
853 </h2> |
|
854 </div> |
|
855 </div> |
|
856 </div> |
|
857 <p id="ch9Using_Linux_Tools_1"> |
|
858 (Attribution : A significant chunk of the content under this section is based on data from Wikipedia and the Linux Documentation Project) |
|
859 </p> |
|
860 <p id="ch9Using_Linux_Tools_2"> |
|
861 Linux (usually pronounced lnks') is a generic term referring to Unix-like computer operating systems based on the Linux kernel, where a kernel is the intermediate layer between the hardware and the applications. The kernel is, on an abstract level, the core of (most) operating systems, that manages the various system resources. The development of the Linux OS is considered the basis for Free and Open Source Software (FOSS) collaboration since typically the underlying source code can be used, modified freely, and redistributed by anyone under the terms of the GNU (a recursive acronym for "GNU's Not Unix!") Global Public License (GPL) and other free software licences. This freedom to access and reuse various components of a system, is one of the primary reasons for the popularity of Linux. |
|
862 </p> |
|
863 <p id="ch9Using_Linux_Tools_3"> |
|
864 Linux is installed on a variety of computer hardware, that include mobile phones, embedded devices and supercomputers, but is infamous for its use in servers. |
|
865 </p> |
|
866 <p id="ch9Using_Linux_Tools_4"> |
|
867 The name "Linux" comes from the Linux kernel, originally written in 1991 by Linus Torvalds. The rest of the system usually comprises components such as the Apache HTTP Server, the X Window System, the GNOME and KDE desktop environments, and utilities and libraries from the GNU Project (announced in 1983 by Richard Stallman). Commonly-used applications with desktop Linux systems include the Mozilla Firefox web-browser and the OpenOffice.org office application suite. The GNU contribution is the basis for the Free Software Foundation's preferred name GNU/Linux. The kernel's mascot is a penguin named "Tux". Mozilla Firefox and OpenOffice.org are open-source projects which can be run on most Operating Systems, including proprietary ones. |
|
868 </p> |
|
869 <div class="section" title="1.1.Historical Background"> |
|
870 <div class="titlepage"> |
|
871 <div> |
|
872 <div> |
|
873 <h3 class="title"> |
|
874 <a name="id2381179"> |
|
875 </a> |
|
876 1.1.Historical Background |
|
877 </h3> |
|
878 </div> |
|
879 </div> |
|
880 </div> |
|
881 <div class="section" title="1.1.1.Events leading to the creation"> |
|
882 <div class="titlepage"> |
|
883 <div> |
|
884 <div> |
|
885 <h4 class="title"> |
|
886 <a name="id2571908"> |
|
887 </a> |
|
888 1.1.1.Events leading to the creation |
|
889 </h4> |
|
890 </div> |
|
891 </div> |
|
892 </div> |
|
893 <div class="itemizedlist"> |
|
894 <ul class="itemizedlist" type="*"> |
|
895 <li class="listitem" style="list-style-type: *"> |
|
896 <p id="ch9Using_Linux_Tools_5"> |
|
897 The Unix operating system was developed in the 1960s and released for public use in 1970. Its accessibility and portability caused it to be widely adopted, copied and modified by academic institutions and businesses. Its design became influential to authors of other systems. Other free operating systems include the Berkeley Software Distribution (BSD), developed at the University of California at Berkeley, and MINIX which was released by Andrew S. Tanenbaum. The development and adoption of BSD and MINIX were limited due to various reasons, and this lack of a widely-adopted and free kernel triggered Linus Torvalds into starting his project. |
|
898 </p> |
|
899 </li> |
|
900 <li class="listitem" style="list-style-type: *"> |
|
901 <p id="ch9Using_Linux_Tools_6"> |
|
902 In 1983, Richard Stallman started the GNU project with the goal of creating a free UNIX-like operating system. As part of this work, he wrote the GNU General Public License (GPL). By the early 1990s there was almost enough available software to create a full operating system. However, the GNU kernel, called Hurd, failed to attract enough attention from developers leaving GNU incomplete. |
|
903 </p> |
|
904 </li> |
|
905 </ul> |
|
906 </div> |
|
907 </div> |
|
908 <div class="section" title="1.1.2.The Creation of Linux"> |
|
909 <div class="titlepage"> |
|
910 <div> |
|
911 <div> |
|
912 <h4 class="title"> |
|
913 <a name="id2624436"> |
|
914 </a> |
|
915 1.1.2.The Creation of Linux |
|
916 </h4> |
|
917 </div> |
|
918 </div> |
|
919 </div> |
|
920 <p id="ch9Using_Linux_Tools_7"> |
|
921 In 1991, Linus Torvalds began a project at the University of Helsinki that later became the Linux kernel. It was initially a terminal (command-line) emulator, which Torvalds used to access the large UNIX servers of the university. He wrote the program targeting just the hardware he was using and independent of an operating system because he wanted to use the functions of his computer with an 80386 processor. Development was done on Minix using the GNU C compiler. This application is still the main choice for compiling Linux today (although the code can be built with other compilers, such as the Intel C Compiler). |
|
922 </p> |
|
923 <p id="ch9Using_Linux_Tools_8"> |
|
924 Torvalds continues to direct the development of the kernel. Stallman heads the Free Software Foundation, which in turn supports the GNU components. Finally, individuals and corporations develop third-party non-GNU components, which constitute a vast body of work and including kernel modules, and user applications and libraries. Linux vendors and communities combine and distribute the kernel, GNU components, and non-GNU components, with additional package management software in the form of Linux distributions. |
|
925 </p> |
|
926 </div> |
|
927 </div> |
|
928 <div class="section" title="1.2.Design and Implications"> |
|
929 <div class="titlepage"> |
|
930 <div> |
|
931 <div> |
|
932 <h3 class="title"> |
|
933 <a name="id2624471"> |
|
934 </a> |
|
935 1.2.Design and Implications |
|
936 </h3> |
|
937 </div> |
|
938 </div> |
|
939 </div> |
|
940 <p id="ch9Using_Linux_Tools_9"> |
|
941 A Linux-based system is a modular Unix-like operating system, deriving much of its basic design from principles established in Unix earlier. Such a system uses a monolithic kernel, called the Linux kernel, which handles process control, networking, and peripheral and file system access. Device drivers are integrated directly with the kernel. Separate projects that interface with the kernel provide much of the system's higher-level functionality. The GNU userland is an important part of most Linux-based systems, providing the most common implementation of the C library, a popular shell, and many of the common Unix tools which carry out many basic operating system tasks. The graphical user interface (or GUI) used by most Linux systems is based on the "X Window System". |
|
942 </p> |
|
943 <div class="section" title="1.2.1.User Interface"> |
|
944 <div class="titlepage"> |
|
945 <div> |
|
946 <div> |
|
947 <h4 class="title"> |
|
948 <a name="id2624496"> |
|
949 </a> |
|
950 1.2.1.User Interface |
|
951 </h4> |
|
952 </div> |
|
953 </div> |
|
954 </div> |
|
955 <p id="ch9Using_Linux_Tools_a"> |
|
956 Users can control a Linux-based system through a command line interface (or CLI), a graphical user interface (or GUI), or through controls attached to the associated hardware (this is common for embedded systems). For desktop systems, the default mode is usually the GUI. On desktop machines, "KDE", "GNOME" and "Xfce" are the most popular user interfaces,though a variety of additional user interfaces exist. Most popular user interfaces run on top of the "X Window System" (or X), which enables a graphical application running on one machine to be displayed and controlled from another in a network. |
|
957 </p> |
|
958 <p id="ch9Using_Linux_Tools_b"> |
|
959 A Linux system also provides a CLI of some sort through a shell, which is the traditional way of interacting with a Unix system. A Linux distribution specialized for servers may use the CLI as its only interface. A headless system (system run without even a monitor) can be controlled by the command line via a remote-control protocol such as SSH or telnet. The CLI is particularly suited for automation of repetitive or delayed tasks, and provides very simple inter-process communication. A graphical terminal emulator program is often used to access the CLI from a Linux desktop. |
|
960 </p> |
|
961 </div> |
|
962 <div class="section" title="1.2.2.Development"> |
|
963 <div class="titlepage"> |
|
964 <div> |
|
965 <div> |
|
966 <h4 class="title"> |
|
967 <a name="id2624550"> |
|
968 </a> |
|
969 1.2.2.Development |
|
970 </h4> |
|
971 </div> |
|
972 </div> |
|
973 </div> |
|
974 <p id="ch9Using_Linux_Tools_c"> |
|
975 The primary difference between Linux and many other popular contemporary operating systems is that the Linux kernel and other components are free and open source software. Linux is not the only such operating system, although it is by far the most widely used. Some free and open source software licenses are based on the principle of "copyleft", a kind of reciprocity: any work derived from a copyleft piece of software must also be copyleft itself. The most common free software license, the GNU GPL, is a form of copyleft, and is used for the Linux kernel and many of the components from the GNU project. |
|
976 </p> |
|
977 <p id="ch9Using_Linux_Tools_d"> |
|
978 Linux based distributions are intended by developers for interoperability with other operating systems and established computing standards. Linux systems adhere to POSIX, SUS, ISO and ANSI standards where possible, although to date only one Linux distribution has been POSIX.1 certified, Linux-FT.Free software projects, although developed in a collaborative fashion, are often produced independently of each other. The fact that the software licenses explicitly permit redistribution, however, provides a basis for larger scale projects that collect the software produced by stand-alone projects and make it available all at once in the form of a Linux distribution. |
|
979 </p> |
|
980 <p id="ch9Using_Linux_Tools_e"> |
|
981 A Linux distribution, commonly called a "distro", is a project that manages a remote collection of system software and application software packages available for download and installation through a network connection. This allows the user to adapt the operating system to his/her specific needs. Distributions are maintained by individuals, loose-knit teams, volunteer organizations, and commercial entities. A distribution can be installed using a CD that contains distribution-specific software for initial system installation and configuration. A package manager such as Synaptic or YAST allows later package upgrades and installations. A distribution is responsible for the default configuration of the installed Linux kernel, general system security, and more generally integration of the different software packages into a coherent whole. |
|
982 </p> |
|
983 </div> |
|
984 <div class="section" title="1.2.3.Community"> |
|
985 <div class="titlepage"> |
|
986 <div> |
|
987 <div> |
|
988 <h4 class="title"> |
|
989 <a name="id2624617"> |
|
990 </a> |
|
991 1.2.3.Community |
|
992 </h4> |
|
993 </div> |
|
994 </div> |
|
995 </div> |
|
996 <p id="ch9Using_Linux_Tools_f"> |
|
997 A distribution is largely driven by its developer and user communities. Some vendors develop and fund their distributions on a volunteer basis. Examples include Debian and the Debian-based, Ubuntu. Others maintain a community version of their commercial distributions, as Red Hat does with Fedora. |
|
998 </p> |
|
999 <p id="ch9Using_Linux_Tools_10"> |
|
1000 In many cities and regions, local associations known as Linux Users Groups (LUGs) seek to promote their preferred distribution and by extension free software. They hold meetings and provide free demonstrations, training, technical support, and operating system installation to new users. Many Internet communities also provide support to Linux users and developers. Most distributions and free software / open source projects have IRC (Internet Relay Chat) chatrooms or newsgroups. Online forums are another means for support. Linux distributions host mailing lists; commonly there will be a specific topic such as usage or development for a given list. All these can be found simply by running an appropriate search on Google. |
|
1001 </p> |
|
1002 <p id="ch9Using_Linux_Tools_11"> |
|
1003 Although Linux distributions are generally available without charge, several large corporations sell, support, and contribute to the development of the components of the system and of free software. These include Dell, IBM, HP, Oracle, Sun Microsystems, Novell, Nokia. A number of corporations, notably Red Hat, have built their entire business around Linux distributions. |
|
1004 </p> |
|
1005 </div> |
|
1006 <div class="section" title="1.2.4.Can I make a profit out of running a business involving Linux?"> |
|
1007 <div class="titlepage"> |
|
1008 <div> |
|
1009 <div> |
|
1010 <h4 class="title"> |
|
1011 <a name="id2624662"> |
|
1012 </a> |
|
1013 1.2.4.Can I make a profit out of running a business involving Linux? |
|
1014 </h4> |
|
1015 </div> |
|
1016 </div> |
|
1017 </div> |
|
1018 <p id="ch9Using_Linux_Tools_12"> |
|
1019 The answer is, "Yes!". The free software licenses, on which the various software packages of a distribution built on the Linux kernel are based, explicitly accommodate and encourage commercialization; the relationship between a Linux distribution as a whole and individual vendors may be seen as symbiotic. One common business model of commercial suppliers is charging for support, especially for business users. A number of companies also offer a specialized business version of their distribution, which adds proprietary support packages and tools to administer higher numbers of installations or to simplify administrative tasks. Another business model is to give away the software in order to sell hardware. Examples of corporations that are extensively (and sometimes exclusively) open-source and Linux-powered , with successful revenue generation models involving these, are Google, SUN, Mozilla, etc. |
|
1020 </p> |
|
1021 </div> |
|
1022 <div class="section" title="1.2.5.Programming on Linux"> |
|
1023 <div class="titlepage"> |
|
1024 <div> |
|
1025 <div> |
|
1026 <h4 class="title"> |
|
1027 <a name="id2624708"> |
|
1028 </a> |
|
1029 1.2.5.Programming on Linux |
|
1030 </h4> |
|
1031 </div> |
|
1032 </div> |
|
1033 </div> |
|
1034 <p id="ch9Using_Linux_Tools_13"> |
|
1035 Most Linux distributions support dozens of programming languages. The most common collection of utilities for building both Linux applications and operating system programs is found within the GNU toolchain, which includes the GNU Compiler Collection (GCC) and the GNU build system. Amongst others, GCC provides compilers for Ada, C, C++, Java, and Fortran. The Linux kernel itself is written to be compiled with GCC. Proprietary compilers for Linux include the Intel C++ Compiler, Sun Studio, and IBM XL C/C++ Compiler. |
|
1036 </p> |
|
1037 <p id="ch9Using_Linux_Tools_14"> |
|
1038 Most distributions also include support for PHP, Perl, Ruby, Python and other dynamic languages. Examples of languages that are less common, but still supported, are C# via the Mono project, sponsored by Novell, and Scheme. A number of Java Virtual Machines and development kits run on Linux, including the original Sun Microsystems JVM (HotSpot), and IBM's J2SE RE, as well as many open-source projects like Kaffe. |
|
1039 </p> |
|
1040 <p id="ch9Using_Linux_Tools_15"> |
|
1041 The two main frameworks for developing graphical applications are those of GNOME and KDE. These projects are based on the GTK+ and Qt widget toolkits, respectively, which can also be used independently of the larger framework. Both support a wide variety of languages. There are a number of Integrated Development Environments (IDEs) available including Anjuta, Code::Blocks, Eclipse, KDevelop, Lazarus, MonoDevelop, NetBeans, and Omnis Studio while the long-established editors Vim and Emacs remain popular. |
|
1042 </p> |
|
1043 </div> |
|
1044 </div> |
|
1045 <div class="section" title="1.3.Reasons for Using Linux"> |
|
1046 <div class="titlepage"> |
|
1047 <div> |
|
1048 <div> |
|
1049 <h3 class="title"> |
|
1050 <a name="id2624755"> |
|
1051 </a> |
|
1052 1.3.Reasons for Using Linux |
|
1053 </h3> |
|
1054 </div> |
|
1055 </div> |
|
1056 </div> |
|
1057 <div class="itemizedlist"> |
|
1058 <ul class="itemizedlist" type="*"> |
|
1059 <li class="listitem" style="list-style-type: *"> |
|
1060 <p id="ch9Using_Linux_Tools_16"> |
|
1061 Linux is free: |
|
1062 </p> |
|
1063 </li> |
|
1064 </ul> |
|
1065 </div> |
|
1066 <p id="ch9Using_Linux_Tools_17"> |
|
1067 As in "free beer". Linux can be downloaded in its entirety from the Internet completely for free. No registration fees, no costs per user, free updates, and freely available source code in case you want to change the behavior of your system. |
232 Most of all, Linux is free as in "free speech": |
1068 Most of all, Linux is free as in "free speech": |
233 The license commonly used is the GNU Public License (GPL). The license says that anybody who may want to do so, has the right to change Linux and eventually to redistribute a changed version, on the one condition that the code is still available after redistribution. In practice, you are free to grab a kernel image and sell the new code, as long as your customers can still have a copy of that code.</p> |
1069 The license commonly used is the GNU Public License (GPL). The license says that anybody who may want to do so, has the right to change Linux and eventually to redistribute a changed version, on the one condition that the code is still available after redistribution. In practice, you are free to grab a kernel image and sell the new code, as long as your customers can still have a copy of that code. |
234 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_18"></a>Linux is portable to any hardware platform:</p></li></ul></div> |
1070 </p> |
235 <p><a name="ch9Using_Linux_Tools_19"></a>A vendor, who wants to sell a new type of computer and who does not know what kind of OS his/her new machine will run, can take a Linux kernel and make it work on his/her hardware, because documentation related to this activity is freely available.</p> |
1071 <div class="itemizedlist"> |
236 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_1a"></a>Linux was made to keep on running:</p></li></ul></div> |
1072 <ul class="itemizedlist" type="*"> |
237 <p><a name="ch9Using_Linux_Tools_1b"></a>As with UNIX, a Linux system expects to run without rebooting all the time. That is why a lot of tasks are being executed at night or scheduled automatically for other times, resulting in higher availability during busier periods and a more balanced use of the hardware. This property allows for Linux to be applicable to environments where people do not have the time or the possibility to control their systems constantly.</p> |
1073 <li class="listitem" style="list-style-type: *"> |
238 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_1c"></a>Linux is secure and versatile:</p></li></ul></div> |
1074 <p id="ch9Using_Linux_Tools_18"> |
239 <p><a name="ch9Using_Linux_Tools_1d"></a>The security model used in Linux is based on the UNIX idea of security, which is known to be robust and of proven quality. But Linux is not only safe from attacks from the Internet: it will adapt equally to other situations, utilizing the same high standards for security.</p> |
1075 Linux is portable to any hardware platform: |
240 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_1e"></a>Linux is scalable:</p></li></ul></div> |
1076 </p> |
241 <p><a name="ch9Using_Linux_Tools_1f"></a>From a Palmtop with 2 MB of memory to a petabyte storage cluster with hundreds of nodes: add or remove the appropriate packages and Linux fits all. One does not need a supercomputer anymore,because you can use Linux to do big things using the building blocks provided with the system. If one wants to do little things, such as making an operating system for an embedded processor or just recycling your old 486, Linux will do that as well.</p> |
1077 </li> |
242 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_20"></a>The Linux OS and Linux applications have very short debug−times:</p></li></ul></div> |
1078 </ul> |
243 <p><a name="ch9Using_Linux_Tools_21"></a>Because Linux has been developed and tested by thousands of people, both errors and people to fix them are found very quickly. It often happens that there are only a couple of hours between discovery and fixing of a bug.</p> |
1079 </div> |
244 </div> |
1080 <p id="ch9Using_Linux_Tools_19"> |
245 </div> |
1081 A vendor, who wants to sell a new type of computer and who does not know what kind of OS his/her new machine will run, can take a Linux kernel and make it work on his/her hardware, because documentation related to this activity is freely available. |
246 <div class="section" title="2. Getting Started"> |
1082 </p> |
247 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1083 <div class="itemizedlist"> |
248 <a name="id2946227"></a>2. Getting Started</h2></div></div></div> |
1084 <ul class="itemizedlist" type="*"> |
249 <div class="section" title="2.1. Logging in, activating the user interface and logging out"> |
1085 <li class="listitem" style="list-style-type: *"> |
250 <div class="titlepage"><div><div><h3 class="title"> |
1086 <p id="ch9Using_Linux_Tools_1a"> |
251 <a name="id2946236"></a>2.1. Logging in, activating the user interface and logging out</h3></div></div></div> |
1087 Linux was made to keep on running: |
252 <p><a name="ch9Using_Linux_Tools_22"></a>In order to work on a Linux system directly, one needs to provide a user name and password. You always need to authenticate to the system. Most PC−based Linux systems have two basic modes for a system to run in: either quick and clean in text console mode,which includes with mouse, multitasking and multi−user features, or in graphical console mode, which looks better but eats more system resources.</p> |
1088 </p> |
253 <div class="section" title="2.1.1. Graphical Mode"> |
1089 </li> |
254 <div class="titlepage"><div><div><h4 class="title"> |
1090 </ul> |
255 <a name="id2946266"></a>2.1.1. Graphical Mode</h4></div></div></div> |
1091 </div> |
256 <p><a name="ch9Using_Linux_Tools_23"></a>This is the default nowadays on most desktop computers. You know you will be connecting to the system using graphical mode when you are first asked for your user name, and then to type your password.</p> |
1092 <p id="ch9Using_Linux_Tools_1b"> |
257 <p><a name="ch9Using_Linux_Tools_24"></a>To log in, make sure the mouse pointer is in the login window, provide your user name and password to the system and click <span class="emphasis"><em>OK</em></span> or press <span class="emphasis"><em>Enter</em></span>. |
1093 As with UNIX, a Linux system expects to run without rebooting all the time. That is why a lot of tasks are being executed at night or scheduled automatically for other times, resulting in higher availability during busier periods and a more balanced use of the hardware. This property allows for Linux to be applicable to environments where people do not have the time or the possibility to control their systems constantly. |
258 It is generally considered a bad idea to connect (graphically) using the root user name, the system adminstrator's account, since the use of graphics includes running a lot of extra programs, in root's case with a lot of extra permissions. To keep all risks as low as possible, use a normal user account to connect graphically. But there are enough risks to keep this in mind as a general advice, for all use of the root account: only log in as root when extra privileges are required.</p> |
1094 </p> |
259 <p><a name="ch9Using_Linux_Tools_25"></a>After entering your user name/password combination, it can take a little while before the graphical environment is started, depending on the CPU speed of your computer, on the software you use and on your personal settings.</p> |
1095 <div class="itemizedlist"> |
260 <p><a name="ch9Using_Linux_Tools_26"></a>To continue, you will need to open a <span class="emphasis"><em>terminal window</em></span> or <span class="emphasis"><em>xterm</em></span> for short (X being the name for the underlying software supporting the graphical environment). This program can be found in the <span class="emphasis"><em>Applications−>Utilities->System Tools</em></span> or <span class="emphasis"><em>Internet menu</em></span>, depending on what window manager you are using. There might be icons that you can use as a shortcut to get an <span class="emphasis"><em>xterm</em></span> window as well, and clicking the right mouse button on the desktop background will usually present you with a menu containing a terminal window application.</p> |
1096 <ul class="itemizedlist" type="*"> |
261 <p><a name="ch9Using_Linux_Tools_27"></a>While browsing the menus, you will notice that a lot of things can be done without entering commands via the keyboard. For most users, the good old point−n−click method of dealing with the computer will do. But for those who want to enter the "heart" of the system, a tool stronger than a mouse will be required to handle the various tasks. This tool is the shell, and when in graphical mode, we activate our shell by opening a terminal window.</p> |
1097 <li class="listitem" style="list-style-type: *"> |
262 <p><a name="ch9Using_Linux_Tools_28"></a>A terminal window should always show a command prompt when you open one. This terminal shows a standard prompt, which displays the user's login name, and the current working directory, represented by the twiddle (~)</p> |
1098 <p id="ch9Using_Linux_Tools_1c"> |
263 <p><a name="ch9Using_Linux_Tools_29"></a>Another common form for a prompt is this one: |
1099 Linux is secure and versatile: |
264 [</p> |
1100 </p> |
265 <div class="reference"> |
1101 </li> |
266 <div class="titlepage"><hr></div>user@host</div> |
1102 </ul> |
267 <p> dir]</p> |
1103 </div> |
268 <p><a name="ch9Using_Linux_Tools_2a"></a>In the above example, <span class="emphasis"><em>user</em></span> will be your login name, <span class="emphasis"><em>hosts</em></span> the name of the machine you are working on, and <span class="emphasis"><em>dir</em></span> an indication of your current location in the file system. Prompts can display all kinds of information, but they are not part of the commands you are giving to your system. To disconnect from the system in graphical mode, you need to close all terminal windows and other applications. After that, hit the <span class="emphasis"><em>logout</em></span> icon or find <span class="emphasis"><em>Log Out</em></span> in the menu. Closing everything is not really necessary, and the system can do this for you, but session management might put all currently open applications back on your screen when you connect again, which takes longer and is not always the desired effect. However, this behavior is configurable.</p> |
1104 <p id="ch9Using_Linux_Tools_1d"> |
269 <p><a name="ch9Using_Linux_Tools_2b"></a>When you see the login screen again, asking to enter user name and password, logout was successful.</p> |
1105 The security model used in Linux is based on the UNIX idea of security, which is known to be robust and of proven quality. But Linux is not only safe from attacks from the Internet: it will adapt equally to other situations, utilizing the same high standards for security. |
270 </div> |
1106 </p> |
271 <div class="section" title="2.1.2. Text Mode"> |
1107 <div class="itemizedlist"> |
272 <div class="titlepage"><div><div><h4 class="title"> |
1108 <ul class="itemizedlist" type="*"> |
273 <a name="id2946426"></a>2.1.2. Text Mode</h4></div></div></div> |
1109 <li class="listitem" style="list-style-type: *"> |
274 <p><a name="ch9Using_Linux_Tools_2c"></a>One is in text mode when the whole screen is black, showing (in most cases white) characters. A text mode login screen typically shows some information about the machine you are working on, the name of the machine and a prompt waiting for you to log in.</p> |
1110 <p id="ch9Using_Linux_Tools_1e"> |
275 <p><a name="ch9Using_Linux_Tools_2d"></a>The login is different from a graphical login, in that you have to hit the <span class="emphasis"><em>Enter</em></span> key after providing your user name, because there are no buttons on the screen that you can click with the mouse. Then you should type your password, followed by another <span class="emphasis"><em>Enter</em></span>. You will not see any indication that you are entering something, not even an asterisk, and you won't see the cursor move. But this is normal on Linux and is done for security |
1111 Linux is scalable: |
276 reasons.</p> |
1112 </p> |
277 <p><a name="ch9Using_Linux_Tools_2e"></a>When the system has accepted you as a valid user, you may get some more information, called the <span class="emphasis"><em>message of the day</em></span>, which can be anything. Additionally, it is popular on UNIX systems to display a fortune cookie, which contains some general wise or unwise (this is up to you) thoughts. After that, you will be given a shell, indicated with the same prompt that you would get in graphical mode.</p> |
1113 </li> |
278 <p><a name="ch9Using_Linux_Tools_2f"></a>Also in text mode: log in as root only to do setup and configuration that absolutely requires administrator privileges, such as adding users, installing software packages, and performing network and other system configuration. Once you are finished, immediately leave the special account and resume your work as a non−privileged user.</p> |
1114 </ul> |
279 <p><a name="ch9Using_Linux_Tools_30"></a>Logging out is done by entering the <span class="emphasis"><em>logout</em></span> command, followed by Enter. You are successfully disconnected from the system when you see the login screen again.Don't power−off the computer after logging out. It is not meant to be shut off without application of the proper procedures for halting the system. Powering it off without going through the halting process might cause severe damage!</p> |
1115 </div> |
280 </div> |
1116 <p id="ch9Using_Linux_Tools_1f"> |
281 </div> |
1117 From a Palmtop with 2 MB of memory to a petabyte storage cluster with hundreds of nodes: add or remove the appropriate packages and Linux fits all. One does not need a supercomputer anymore,because you can use Linux to do big things using the building blocks provided with the system. If one wants to do little things, such as making an operating system for an embedded processor or just recycling your old 486, Linux will do that as well. |
282 </div> |
1118 </p> |
283 <div class="section" title="3. Basic Commands"> |
1119 <div class="itemizedlist"> |
284 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1120 <ul class="itemizedlist" type="*"> |
285 <a name="id2946515"></a>3. Basic Commands</h2></div></div></div> |
1121 <li class="listitem" style="list-style-type: *"> |
286 <div class="section" title="3.1. ls"> |
1122 <p id="ch9Using_Linux_Tools_20"> |
287 <div class="titlepage"><div><div><h3 class="title"> |
1123 The Linux OS and Linux applications have very short debugtimes: |
288 <a name="id2946524"></a>3.1. ls</h3></div></div></div> |
1124 </p> |
289 <p><a name="ch9Using_Linux_Tools_31"></a>When invoked without any arguments, <span class="emphasis"><em>ls</em></span> lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. The name <span class="emphasis"><em>ls</em></span> is derived from <span class="emphasis"><em>list segments</em></span> which was used in earlier systems.</p> |
1125 </li> |
290 <p><a name="ch9Using_Linux_Tools_32"></a>Files whose names start with "." are not listed, unless the <span class="emphasis"><em>-a</em></span> flag is specified or the files are specified explicitly.</p> |
1126 </ul> |
291 <p><a name="ch9Using_Linux_Tools_33"></a>Without options, <span class="emphasis"><em>ls</em></span> displays files in a bare format. This bare format however makes it difficult to establish the type, permissions, and size of the files. The most common options to reveal this information or change the list of files are:</p> |
1127 </div> |
292 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1128 <p id="ch9Using_Linux_Tools_21"> |
293 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_34"></a><span class="emphasis"><em>-l</em></span> long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename</p></li> |
1129 Because Linux has been developed and tested by thousands of people, both errors and people to fix them are found very quickly. It often happens that there are only a couple of hours between discovery and fixing of a bug. |
294 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_35"></a><span class="emphasis"><em>-F</em></span> appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.</p></li> |
1130 </p> |
295 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_36"></a><span class="emphasis"><em>-a</em></span> lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list.</p></li> |
1131 </div> |
296 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_37"></a><span class="emphasis"><em>-R</em></span> recursively lists subdirectories. The command ls -R / would therefore list all files.</p></li> |
1132 </div> |
297 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_38"></a><span class="emphasis"><em>-d</em></span> shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory.</p></li> |
1133 <div class="section" title="2.Getting Started"> |
298 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_39"></a><span class="emphasis"><em>-t</em></span> sort the list of files by modification time.</p></li> |
1134 <div class="titlepage"> |
299 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_3a"></a><span class="emphasis"><em>-h</em></span> print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)</p></li> |
1135 <div> |
300 </ul></div> |
1136 <div> |
301 <p><a name="ch9Using_Linux_Tools_3b"></a>In some environments, providing the option <span class="emphasis"><em>--color</em></span> (for GNU ls) or <span class="emphasis"><em>-G</em></span> (FreeBSD ls) causes ls to highlight different types of files with different colors, instead of with characters as <span class="emphasis"><em>-F</em></span> would. To determine what color to use for a file, GNU <span class="emphasis"><em>ls</em></span> checks the Unix file type, the file permissions, and the file extension, while FreeBSD <span class="emphasis"><em>ls</em></span> checks only the Unix file type and file permissions.:</p> |
1137 <h2 class="title" style="clear: both"> |
302 <pre class="programlisting"> |
1138 <a name="id2624896"> |
303 $ ls |
1139 </a> |
|
1140 2.Getting Started |
|
1141 </h2> |
|
1142 </div> |
|
1143 </div> |
|
1144 </div> |
|
1145 <div class="section" title="2.1.Logging in, activating the user interface and logging out"> |
|
1146 <div class="titlepage"> |
|
1147 <div> |
|
1148 <div> |
|
1149 <h3 class="title"> |
|
1150 <a name="id2624904"> |
|
1151 </a> |
|
1152 2.1.Logging in, activating the user interface and logging out |
|
1153 </h3> |
|
1154 </div> |
|
1155 </div> |
|
1156 </div> |
|
1157 <p id="ch9Using_Linux_Tools_22"> |
|
1158 In order to work on a Linux system directly, one needs to provide a user name and password. You always need to authenticate to the system. Most PCbased Linux systems have two basic modes for a system to run in: either quick and clean in text console mode,which includes with mouse, multitasking and multiuser features, or in graphical console mode, which looks better but eats more system resources. |
|
1159 </p> |
|
1160 <div class="section" title="2.1.1.Graphical Mode"> |
|
1161 <div class="titlepage"> |
|
1162 <div> |
|
1163 <div> |
|
1164 <h4 class="title"> |
|
1165 <a name="id2624935"> |
|
1166 </a> |
|
1167 2.1.1.Graphical Mode |
|
1168 </h4> |
|
1169 </div> |
|
1170 </div> |
|
1171 </div> |
|
1172 <p id="ch9Using_Linux_Tools_23"> |
|
1173 This is the default nowadays on most desktop computers. You know you will be connecting to the system using graphical mode when you are first asked for your user name, and then to type your password. |
|
1174 </p> |
|
1175 <p id="ch9Using_Linux_Tools_24"> |
|
1176 To log in, make sure the mouse pointer is in the login window, provide your user name and password to the system and click |
|
1177 <span class="emphasis"> |
|
1178 <em> |
|
1179 OK |
|
1180 </em> |
|
1181 </span> |
|
1182 or press |
|
1183 <span class="emphasis"> |
|
1184 <em> |
|
1185 Enter |
|
1186 </em> |
|
1187 </span> |
|
1188 . |
|
1189 It is generally considered a bad idea to connect (graphically) using the root user name, the system adminstrator's account, since the use of graphics includes running a lot of extra programs, in root's case with a lot of extra permissions. To keep all risks as low as possible, use a normal user account to connect graphically. But there are enough risks to keep this in mind as a general advice, for all use of the root account: only log in as root when extra privileges are required. |
|
1190 </p> |
|
1191 <p id="ch9Using_Linux_Tools_25"> |
|
1192 After entering your user name/password combination, it can take a little while before the graphical environment is started, depending on the CPU speed of your computer, on the software you use and on your personal settings. |
|
1193 </p> |
|
1194 <p id="ch9Using_Linux_Tools_26"> |
|
1195 To continue, you will need to open a |
|
1196 <span class="emphasis"> |
|
1197 <em> |
|
1198 terminal window |
|
1199 </em> |
|
1200 </span> |
|
1201 or |
|
1202 <span class="emphasis"> |
|
1203 <em> |
|
1204 xterm |
|
1205 </em> |
|
1206 </span> |
|
1207 for short (X being the name for the underlying software supporting the graphical environment). This program can be found in the |
|
1208 <span class="emphasis"> |
|
1209 <em> |
|
1210 Applications>Utilities->System Tools |
|
1211 </em> |
|
1212 </span> |
|
1213 or |
|
1214 <span class="emphasis"> |
|
1215 <em> |
|
1216 Internet menu |
|
1217 </em> |
|
1218 </span> |
|
1219 , depending on what window manager you are using. There might be icons that you can use as a shortcut to get an |
|
1220 <span class="emphasis"> |
|
1221 <em> |
|
1222 xterm |
|
1223 </em> |
|
1224 </span> |
|
1225 window as well, and clicking the right mouse button on the desktop background will usually present you with a menu containing a terminal window application. |
|
1226 </p> |
|
1227 <p id="ch9Using_Linux_Tools_27"> |
|
1228 While browsing the menus, you will notice that a lot of things can be done without entering commands via the keyboard. For most users, the good old pointnclick method of dealing with the computer will do. But for those who want to enter the "heart" of the system, a tool stronger than a mouse will be required to handle the various tasks. This tool is the shell, and when in graphical mode, we activate our shell by opening a terminal window. |
|
1229 </p> |
|
1230 <p id="ch9Using_Linux_Tools_28"> |
|
1231 A terminal window should always show a command prompt when you open one. This terminal shows a standard prompt, which displays the user's login name, and the current working directory, represented by the twiddle (~) |
|
1232 </p> |
|
1233 <p id="ch9Using_Linux_Tools_29"> |
|
1234 Another common form for a prompt is this one: |
|
1235 [ |
|
1236 </p> |
|
1237 <div class="reference"> |
|
1238 <div class="titlepage"> |
|
1239 <hr /> |
|
1240 </div> |
|
1241 user@host |
|
1242 </div> |
|
1243 <p> |
|
1244 dir] |
|
1245 </p> |
|
1246 <p id="ch9Using_Linux_Tools_2a"> |
|
1247 In the above example, |
|
1248 <span class="emphasis"> |
|
1249 <em> |
|
1250 user |
|
1251 </em> |
|
1252 </span> |
|
1253 will be your login name, |
|
1254 <span class="emphasis"> |
|
1255 <em> |
|
1256 hosts |
|
1257 </em> |
|
1258 </span> |
|
1259 the name of the machine you are working on, and |
|
1260 <span class="emphasis"> |
|
1261 <em> |
|
1262 dir |
|
1263 </em> |
|
1264 </span> |
|
1265 an indication of your current location in the file system. Prompts can display all kinds of information, but they are not part of the commands you are giving to your system. To disconnect from the system in graphical mode, you need to close all terminal windows and other applications. After that, hit the |
|
1266 <span class="emphasis"> |
|
1267 <em> |
|
1268 logout |
|
1269 </em> |
|
1270 </span> |
|
1271 icon or find |
|
1272 <span class="emphasis"> |
|
1273 <em> |
|
1274 Log Out |
|
1275 </em> |
|
1276 </span> |
|
1277 in the menu. Closing everything is not really necessary, and the system can do this for you, but session management might put all currently open applications back on your screen when you connect again, which takes longer and is not always the desired effect. However, this behavior is configurable. |
|
1278 </p> |
|
1279 <p id="ch9Using_Linux_Tools_2b"> |
|
1280 When you see the login screen again, asking to enter user name and password, logout was successful. |
|
1281 </p> |
|
1282 </div> |
|
1283 <div class="section" title="2.1.2.Text Mode"> |
|
1284 <div class="titlepage"> |
|
1285 <div> |
|
1286 <div> |
|
1287 <h4 class="title"> |
|
1288 <a name="id2625094"> |
|
1289 </a> |
|
1290 2.1.2.Text Mode |
|
1291 </h4> |
|
1292 </div> |
|
1293 </div> |
|
1294 </div> |
|
1295 <p id="ch9Using_Linux_Tools_2c"> |
|
1296 One is in text mode when the whole screen is black, showing (in most cases white) characters. A text mode login screen typically shows some information about the machine you are working on, the name of the machine and a prompt waiting for you to log in. |
|
1297 </p> |
|
1298 <p id="ch9Using_Linux_Tools_2d"> |
|
1299 The login is different from a graphical login, in that you have to hit the |
|
1300 <span class="emphasis"> |
|
1301 <em> |
|
1302 Enter |
|
1303 </em> |
|
1304 </span> |
|
1305 key after providing your user name, because there are no buttons on the screen that you can click with the mouse. Then you should type your password, followed by another |
|
1306 <span class="emphasis"> |
|
1307 <em> |
|
1308 Enter |
|
1309 </em> |
|
1310 </span> |
|
1311 . You will not see any indication that you are entering something, not even an asterisk, and you won't see the cursor move. But this is normal on Linux and is done for security |
|
1312 reasons. |
|
1313 </p> |
|
1314 <p id="ch9Using_Linux_Tools_2e"> |
|
1315 When the system has accepted you as a valid user, you may get some more information, called the |
|
1316 <span class="emphasis"> |
|
1317 <em> |
|
1318 message of the day |
|
1319 </em> |
|
1320 </span> |
|
1321 , which can be anything. Additionally, it is popular on UNIX systems to display a fortune cookie, which contains some general wise or unwise (this is up to you) thoughts. After that, you will be given a shell, indicated with the same prompt that you would get in graphical mode. |
|
1322 </p> |
|
1323 <p id="ch9Using_Linux_Tools_2f"> |
|
1324 Also in text mode: log in as root only to do setup and configuration that absolutely requires administrator privileges, such as adding users, installing software packages, and performing network and other system configuration. Once you are finished, immediately leave the special account and resume your work as a nonprivileged user. |
|
1325 </p> |
|
1326 <p id="ch9Using_Linux_Tools_30"> |
|
1327 Logging out is done by entering the |
|
1328 <span class="emphasis"> |
|
1329 <em> |
|
1330 logout |
|
1331 </em> |
|
1332 </span> |
|
1333 command, followed by Enter. You are successfully disconnected from the system when you see the login screen again.Don't poweroff the computer after logging out. It is not meant to be shut off without application of the proper procedures for halting the system. Powering it off without going through the halting process might cause severe damage! |
|
1334 </p> |
|
1335 </div> |
|
1336 </div> |
|
1337 </div> |
|
1338 <div class="section" title="3.Basic Commands"> |
|
1339 <div class="titlepage"> |
|
1340 <div> |
|
1341 <div> |
|
1342 <h2 class="title" style="clear: both"> |
|
1343 <a name="id2625184"> |
|
1344 </a> |
|
1345 3.Basic Commands |
|
1346 </h2> |
|
1347 </div> |
|
1348 </div> |
|
1349 </div> |
|
1350 <div class="section" title="3.1.ls"> |
|
1351 <div class="titlepage"> |
|
1352 <div> |
|
1353 <div> |
|
1354 <h3 class="title"> |
|
1355 <a name="id2625193"> |
|
1356 </a> |
|
1357 3.1.ls |
|
1358 </h3> |
|
1359 </div> |
|
1360 </div> |
|
1361 </div> |
|
1362 <p id="ch9Using_Linux_Tools_31"> |
|
1363 When invoked without any arguments, |
|
1364 <span class="emphasis"> |
|
1365 <em> |
|
1366 ls |
|
1367 </em> |
|
1368 </span> |
|
1369 lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. The name |
|
1370 <span class="emphasis"> |
|
1371 <em> |
|
1372 ls |
|
1373 </em> |
|
1374 </span> |
|
1375 is derived from |
|
1376 <span class="emphasis"> |
|
1377 <em> |
|
1378 list segments |
|
1379 </em> |
|
1380 </span> |
|
1381 which was used in earlier systems. |
|
1382 </p> |
|
1383 <p id="ch9Using_Linux_Tools_32"> |
|
1384 Files whose names start with "." are not listed, unless the |
|
1385 <span class="emphasis"> |
|
1386 <em> |
|
1387 -a |
|
1388 </em> |
|
1389 </span> |
|
1390 flag is specified or the files are specified explicitly. |
|
1391 </p> |
|
1392 <p id="ch9Using_Linux_Tools_33"> |
|
1393 Without options, |
|
1394 <span class="emphasis"> |
|
1395 <em> |
|
1396 ls |
|
1397 </em> |
|
1398 </span> |
|
1399 displays files in a bare format. This bare format however makes it difficult to establish the type, permissions, and size of the files. The most common options to reveal this information or change the list of files are: |
|
1400 </p> |
|
1401 <div class="itemizedlist"> |
|
1402 <ul class="itemizedlist" type="*"> |
|
1403 <li class="listitem" style="list-style-type: *"> |
|
1404 <p id="ch9Using_Linux_Tools_34"> |
|
1405 <span class="emphasis"> |
|
1406 <em> |
|
1407 -l |
|
1408 </em> |
|
1409 </span> |
|
1410 long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename |
|
1411 </p> |
|
1412 </li> |
|
1413 <li class="listitem" style="list-style-type: *"> |
|
1414 <p id="ch9Using_Linux_Tools_35"> |
|
1415 <span class="emphasis"> |
|
1416 <em> |
|
1417 -F |
|
1418 </em> |
|
1419 </span> |
|
1420 appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix. |
|
1421 </p> |
|
1422 </li> |
|
1423 <li class="listitem" style="list-style-type: *"> |
|
1424 <p id="ch9Using_Linux_Tools_36"> |
|
1425 <span class="emphasis"> |
|
1426 <em> |
|
1427 -a |
|
1428 </em> |
|
1429 </span> |
|
1430 lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list. |
|
1431 </p> |
|
1432 </li> |
|
1433 <li class="listitem" style="list-style-type: *"> |
|
1434 <p id="ch9Using_Linux_Tools_37"> |
|
1435 <span class="emphasis"> |
|
1436 <em> |
|
1437 -R |
|
1438 </em> |
|
1439 </span> |
|
1440 recursively lists subdirectories. The command ls -R / would therefore list all files. |
|
1441 </p> |
|
1442 </li> |
|
1443 <li class="listitem" style="list-style-type: *"> |
|
1444 <p id="ch9Using_Linux_Tools_38"> |
|
1445 <span class="emphasis"> |
|
1446 <em> |
|
1447 -d |
|
1448 </em> |
|
1449 </span> |
|
1450 shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory. |
|
1451 </p> |
|
1452 </li> |
|
1453 <li class="listitem" style="list-style-type: *"> |
|
1454 <p id="ch9Using_Linux_Tools_39"> |
|
1455 <span class="emphasis"> |
|
1456 <em> |
|
1457 -t |
|
1458 </em> |
|
1459 </span> |
|
1460 sort the list of files by modification time. |
|
1461 </p> |
|
1462 </li> |
|
1463 <li class="listitem" style="list-style-type: *"> |
|
1464 <p id="ch9Using_Linux_Tools_3a"> |
|
1465 <span class="emphasis"> |
|
1466 <em> |
|
1467 -h |
|
1468 </em> |
|
1469 </span> |
|
1470 print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.) |
|
1471 </p> |
|
1472 </li> |
|
1473 </ul> |
|
1474 </div> |
|
1475 <p id="ch9Using_Linux_Tools_3b"> |
|
1476 In some environments, providing the option |
|
1477 <span class="emphasis"> |
|
1478 <em> |
|
1479 --color |
|
1480 </em> |
|
1481 </span> |
|
1482 (for GNU ls) or |
|
1483 <span class="emphasis"> |
|
1484 <em> |
|
1485 -G |
|
1486 </em> |
|
1487 </span> |
|
1488 (FreeBSD ls) causes ls to highlight different types of files with different colors, instead of with characters as |
|
1489 <span class="emphasis"> |
|
1490 <em> |
|
1491 -F |
|
1492 </em> |
|
1493 </span> |
|
1494 would. To determine what color to use for a file, GNU |
|
1495 <span class="emphasis"> |
|
1496 <em> |
|
1497 ls |
|
1498 </em> |
|
1499 </span> |
|
1500 checks the Unix file type, the file permissions, and the file extension, while FreeBSD |
|
1501 <span class="emphasis"> |
|
1502 <em> |
|
1503 ls |
|
1504 </em> |
|
1505 </span> |
|
1506 checks only the Unix file type and file permissions.: |
|
1507 </p> |
|
1508 <pre class="programlisting"> |
|
1509 $ ls |
304 jeeves.rst psmith.html blandings.html |
1510 jeeves.rst psmith.html blandings.html |
305 $ ls -l |
1511 $ ls -l |
306 drwxr--r-- 1 plum editors 4096 jeeves |
1512 drwxr--r-- 1 plum editors 4096 jeeves |
307 -rw-r--r-- 1 plum editors 30405 psmith |
1513 -rw-r--r-- 1 plum editors 30405 psmith |
308 -r-xr-xr-x 1 plum plum 8460 blandings</pre> |
1514 -r-xr-xr-x 1 plum plum 8460 blandings |
309 <p><a name="ch9Using_Linux_Tools_3c"></a>Here "$" actually is the beginning of the prompt. This is typical in most Unix-based systems.</p> |
1515 </pre> |
310 </div> |
1516 <p id="ch9Using_Linux_Tools_3c"> |
311 <div class="section" title="3.2. date"> |
1517 Here "$" actually is the beginning of the prompt. This is typical in most Unix-based systems. |
312 <div class="titlepage"><div><div><h3 class="title"> |
1518 </p> |
313 <a name="id2946687"></a>3.2. date</h3></div></div></div> |
1519 </div> |
314 <p><a name="ch9Using_Linux_Tools_3d"></a>The Unix date command displays the time and date. The super-user can use it to set the system clock.</p> |
1520 <div class="section" title="3.2.date"> |
315 <p><a name="ch9Using_Linux_Tools_3e"></a>With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example:</p> |
1521 <div class="titlepage"> |
316 <pre class="programlisting"> |
1522 <div> |
317 $date |
1523 <div> |
318 Tue Sep 8 12:01:45 IST 2009</pre> |
1524 <h3 class="title"> |
319 <p><a name="ch9Using_Linux_Tools_3f"></a>On some systems to set the current date and time to September 8, 2004 01:22 you type:</p> |
1525 <a name="id2625356"> |
320 <pre class="programlisting"> |
1526 </a> |
321 $date --set="20040908 01:22"</pre> |
1527 3.2.date |
322 <p><a name="ch9Using_Linux_Tools_40"></a>In order to view the various options for the <span class="emphasis"><em>date</em></span> command, type:</p> |
1528 </h3> |
323 <pre class="programlisting"> |
1529 </div> |
324 $man date</pre> |
1530 </div> |
325 <p><a name="ch9Using_Linux_Tools_41"></a>This will take you to the "Manual" page comprising of all the details on the <span class="emphasis"><em>date</em></span> command. You can return to the terminal from the "man" page by pressing the <span class="emphasis"><em>Esc</em></span> key in the keyboard and typing ":q" in that order.</p> |
1531 </div> |
326 </div> |
1532 <p id="ch9Using_Linux_Tools_3d"> |
327 <div class="section" title="3.3. cd"> |
1533 The Unix date command displays the time and date. The super-user can use it to set the system clock. |
328 <div class="titlepage"><div><div><h3 class="title"> |
1534 </p> |
329 <a name="id2946747"></a>3.3. cd</h3></div></div></div> |
1535 <p id="ch9Using_Linux_Tools_3e"> |
330 <p><a name="ch9Using_Linux_Tools_42"></a>This stands for "change directory". When one wants to go up to the parent directory, bypassing the tree of directories one has entered, “ cd ..” can be used.</p> |
1536 With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example: |
331 <p><a name="ch9Using_Linux_Tools_43"></a>One dot '.' represents the current directory while two dots '..' represent the parent directory.</p> |
1537 </p> |
332 <p><a name="ch9Using_Linux_Tools_44"></a>“ cd -” will return you to the previous directory (a bit like an “undo”).</p> |
1538 <pre class="programlisting"> |
333 <p><a name="ch9Using_Linux_Tools_45"></a>You can also use cd absolute path or cd relative path (see below):</p> |
1539 $date |
334 <p><a name="ch9Using_Linux_Tools_46"></a>Absolute paths:</p> |
1540 Tue Sep 8 12:01:45 IST 2009 |
335 <p><a name="ch9Using_Linux_Tools_47"></a>An “ absolute path” is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down.</p> |
1541 </pre> |
336 <p><a name="ch9Using_Linux_Tools_48"></a>For example to get to /boot/grub you would type:</p> |
1542 <p id="ch9Using_Linux_Tools_3f"> |
337 <pre class="programlisting"> |
1543 On some systems to set the current date and time to September 8, 2004 01:22 you type: |
338 $cd /boot/grub</pre> |
1544 </p> |
339 <p><a name="ch9Using_Linux_Tools_49"></a>This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command).</p> |
1545 <pre class="programlisting"> |
340 <p><a name="ch9Using_Linux_Tools_4a"></a>Relative paths:</p> |
1546 $date --set="20040908 01:22" |
341 <p><a name="ch9Using_Linux_Tools_4b"></a>A “ relative path” doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem.</p> |
1547 </pre> |
342 <p><a name="ch9Using_Linux_Tools_4c"></a>For example if you are in root's home directory and want to get to /root/music, you type:</p> |
1548 <p id="ch9Using_Linux_Tools_40"> |
343 <pre class="programlisting"> |
1549 In order to view the various options for the |
344 $ cd music</pre> |
1550 <span class="emphasis"> |
345 <p><a name="ch9Using_Linux_Tools_4d"></a>Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward.</p> |
1551 <em> |
346 </div> |
1552 date |
347 <div class="section" title="3.4. who"> |
1553 </em> |
348 <div class="titlepage"><div><div><h3 class="title"> |
1554 </span> |
349 <a name="id2946849"></a>3.4. who</h3></div></div></div> |
1555 command, type: |
350 <p><a name="ch9Using_Linux_Tools_4e"></a>The standard Unix command <span class="emphasis"><em>who</em></span> displays a list of users who are currently logged into a computer.</p> |
1556 </p> |
351 <p><a name="ch9Using_Linux_Tools_4f"></a>The <span class="emphasis"><em>who</em></span> command is related to the command <span class="emphasis"><em>w</em></span>, which provides the same information but also displays additional data and statistics.:</p> |
1557 <pre class="programlisting"> |
352 <pre class="programlisting"> |
1558 $man date |
353 $who |
1559 </pre> |
|
1560 <p id="ch9Using_Linux_Tools_41"> |
|
1561 This will take you to the "Manual" page comprising of all the details on the |
|
1562 <span class="emphasis"> |
|
1563 <em> |
|
1564 date |
|
1565 </em> |
|
1566 </span> |
|
1567 command. You can return to the terminal from the "man" page by pressing the |
|
1568 <span class="emphasis"> |
|
1569 <em> |
|
1570 Esc |
|
1571 </em> |
|
1572 </span> |
|
1573 key in the keyboard and typing ":q" in that order. |
|
1574 </p> |
|
1575 </div> |
|
1576 <div class="section" title="3.3.cd"> |
|
1577 <div class="titlepage"> |
|
1578 <div> |
|
1579 <div> |
|
1580 <h3 class="title"> |
|
1581 <a name="id2625416"> |
|
1582 </a> |
|
1583 3.3.cd |
|
1584 </h3> |
|
1585 </div> |
|
1586 </div> |
|
1587 </div> |
|
1588 <p id="ch9Using_Linux_Tools_42"> |
|
1589 This stands for "change directory". When one wants to go up to the parent directory, bypassing the tree of directories one has entered, cd .. can be used. |
|
1590 </p> |
|
1591 <p id="ch9Using_Linux_Tools_43"> |
|
1592 One dot '.' represents the current directory while two dots '..' represent the parent directory. |
|
1593 </p> |
|
1594 <p id="ch9Using_Linux_Tools_44"> |
|
1595 cd - will return you to the previous directory (a bit like an undo). |
|
1596 </p> |
|
1597 <p id="ch9Using_Linux_Tools_45"> |
|
1598 You can also use cd absolute path or cd relative path (see below): |
|
1599 </p> |
|
1600 <p id="ch9Using_Linux_Tools_46"> |
|
1601 Absolute paths: |
|
1602 </p> |
|
1603 <p id="ch9Using_Linux_Tools_47"> |
|
1604 An absolute path is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down. |
|
1605 </p> |
|
1606 <p id="ch9Using_Linux_Tools_48"> |
|
1607 For example to get to /boot/grub you would type: |
|
1608 </p> |
|
1609 <pre class="programlisting"> |
|
1610 $cd /boot/grub |
|
1611 </pre> |
|
1612 <p id="ch9Using_Linux_Tools_49"> |
|
1613 This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command). |
|
1614 </p> |
|
1615 <p id="ch9Using_Linux_Tools_4a"> |
|
1616 Relative paths: |
|
1617 </p> |
|
1618 <p id="ch9Using_Linux_Tools_4b"> |
|
1619 A relative path doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem. |
|
1620 </p> |
|
1621 <p id="ch9Using_Linux_Tools_4c"> |
|
1622 For example if you are in root's home directory and want to get to /root/music, you type: |
|
1623 </p> |
|
1624 <pre class="programlisting"> |
|
1625 $ cd music |
|
1626 </pre> |
|
1627 <p id="ch9Using_Linux_Tools_4d"> |
|
1628 Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward. |
|
1629 </p> |
|
1630 </div> |
|
1631 <div class="section" title="3.4.who"> |
|
1632 <div class="titlepage"> |
|
1633 <div> |
|
1634 <div> |
|
1635 <h3 class="title"> |
|
1636 <a name="id2625517"> |
|
1637 </a> |
|
1638 3.4.who |
|
1639 </h3> |
|
1640 </div> |
|
1641 </div> |
|
1642 </div> |
|
1643 <p id="ch9Using_Linux_Tools_4e"> |
|
1644 The standard Unix command |
|
1645 <span class="emphasis"> |
|
1646 <em> |
|
1647 who |
|
1648 </em> |
|
1649 </span> |
|
1650 displays a list of users who are currently logged into a computer. |
|
1651 </p> |
|
1652 <p id="ch9Using_Linux_Tools_4f"> |
|
1653 The |
|
1654 <span class="emphasis"> |
|
1655 <em> |
|
1656 who |
|
1657 </em> |
|
1658 </span> |
|
1659 command is related to the command |
|
1660 <span class="emphasis"> |
|
1661 <em> |
|
1662 w |
|
1663 </em> |
|
1664 </span> |
|
1665 , which provides the same information but also displays additional data and statistics.: |
|
1666 </p> |
|
1667 <pre class="programlisting"> |
|
1668 $who |
354 beeblebrox tty7 2009-09-08 10:50 (:0) |
1669 beeblebrox tty7 2009-09-08 10:50 (:0) |
355 beeblebrox pts/0 2009-09-08 11:25 (:0.0) |
1670 beeblebrox pts/0 2009-09-08 11:25 (:0.0) |
356 dumbledore pts/1 2009-09-08 18:11 (potter.xyz.in) |
1671 dumbledore pts/1 2009-09-08 18:11 (potter.xyz.in) |
357 beeblebrox pts/2 2009-09-08 18:53 (:0.0)</pre> |
1672 beeblebrox pts/2 2009-09-08 18:53 (:0.0) |
358 <p><a name="ch9Using_Linux_Tools_50"></a>The command can be invoked with the arguments <span class="emphasis"><em>am i</em></span> or <span class="emphasis"><em>am I</em></span> (so it is invoked as <span class="emphasis"><em>who am i</em></span> or * who am I*), showing information about the current terminal only (see the <span class="emphasis"><em>-m</em></span> option below, of which this invocation is equivalent).</p> |
1673 </pre> |
359 <p><a name="ch9Using_Linux_Tools_51"></a>In order to find out the various options that can be appended to the <span class="emphasis"><em>who</em></span> command, check the <span class="emphasis"><em>man</em></span> page by typing out the following in the terminal:</p> |
1674 <p id="ch9Using_Linux_Tools_50"> |
360 <pre class="programlisting"> |
1675 The command can be invoked with the arguments |
361 $man who</pre> |
1676 <span class="emphasis"> |
362 <p><a name="ch9Using_Linux_Tools_52"></a>This will take you to the "Manual" page containing details about the <span class="emphasis"><em>who</em></span> command</p> |
1677 <em> |
363 </div> |
1678 am i |
364 <div class="section" title="3.5. mkdir"> |
1679 </em> |
365 <div class="titlepage"><div><div><h3 class="title"> |
1680 </span> |
366 <a name="id2946932"></a>3.5. mkdir</h3></div></div></div> |
1681 or |
367 <p><a name="ch9Using_Linux_Tools_53"></a>This command is used to make a new directory. Normal usage is as straightforward as follows:</p> |
1682 <span class="emphasis"> |
368 <pre class="programlisting"> |
1683 <em> |
369 $mkdir name_of_directory</pre> |
1684 am I |
370 <p><a name="ch9Using_Linux_Tools_54"></a>Where <span class="emphasis"><em>name_of_directory</em></span> is the name of the directory one wants to create. When typed as above (ie. normal usage), the new directory would be created within the current directory. On Unix, multiple directories can be specified, and <span class="emphasis"><em>mkdir</em></span> will try to create all of them.</p> |
1685 </em> |
371 <div class="section" title="3.5.1. Options"> |
1686 </span> |
372 <div class="titlepage"><div><div><h4 class="title"> |
1687 (so it is invoked as |
373 <a name="id2946964"></a>3.5.1. Options</h4></div></div></div> |
1688 <span class="emphasis"> |
374 <p><a name="ch9Using_Linux_Tools_55"></a>On Unix-like operating systems, <span class="emphasis"><em>mkdir</em></span> takes options. Three of the most common options are:</p> |
1689 <em> |
375 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1690 who am i |
376 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_56"></a><span class="emphasis"><em>-p</em></span>: will also create all directories leading up to the given directory that do not exist already. If the given directory already exists, ignore the error.</p></li> |
1691 </em> |
377 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_57"></a><span class="emphasis"><em>-v</em></span>: display each directory that mkdir creates. Most often used with -p.</p></li> |
1692 </span> |
378 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_58"></a><span class="emphasis"><em>-m</em></span>: specify the octal permissions of directories created by mkdir.</p></li> |
1693 or * who am I*), showing information about the current terminal only (see the |
379 </ul></div> |
1694 <span class="emphasis"> |
380 <p><a name="ch9Using_Linux_Tools_59"></a><span class="emphasis"><em>-p</em></span> is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts.</p> |
1695 <em> |
381 </div> |
1696 -m |
382 <div class="section" title="3.5.2. Examples"> |
1697 </em> |
383 <div class="titlepage"><div><div><h4 class="title"> |
1698 </span> |
384 <a name="id2947023"></a>3.5.2. Examples</h4></div></div></div> |
1699 option below, of which this invocation is equivalent). |
385 <p><a name="ch9Using_Linux_Tools_5a"></a>An example of <span class="emphasis"><em>-p</em></span> in action is:</p> |
1700 </p> |
386 <pre class="programlisting"> |
1701 <p id="ch9Using_Linux_Tools_51"> |
387 $mkdir -p /tmp/a/b/c</pre> |
1702 In order to find out the various options that can be appended to the |
388 <p><a name="ch9Using_Linux_Tools_5b"></a>If <span class="emphasis"><em>/tmp/a</em></span> exists but <span class="emphasis"><em>/tmp/a/b</em></span> does not, mkdir will create <span class="emphasis"><em>/tmp/a/b</em></span> before creating <span class="emphasis"><em>/tmp/a/b/c</em></span>.</p> |
1703 <span class="emphasis"> |
389 <p><a name="ch9Using_Linux_Tools_5c"></a>And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself):</p> |
1704 <em> |
390 <pre class="programlisting"> |
1705 who |
391 $mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}</pre> |
1706 </em> |
392 <p><a name="ch9Using_Linux_Tools_5d"></a>This will create:</p>tmpdir - branches<div class="itemizedlist"><ul class="itemizedlist" type="*"> |
1707 </span> |
393 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_5e"></a>tag</p></li> |
1708 command, check the |
394 <li class="listitem" style="list-style-type: *">trunk - sources - includes<div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_5f"></a>docs</p></li></ul></div> |
1709 <span class="emphasis"> |
395 </li> |
1710 <em> |
396 </ul></div> |
1711 man |
397 </div> |
1712 </em> |
398 </div> |
1713 </span> |
399 </div> |
1714 page by typing out the following in the terminal: |
400 <div class="section" title="4. Getting Help"> |
1715 </p> |
401 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
1716 <pre class="programlisting"> |
402 <a name="id2947099"></a>4. Getting Help</h2></div></div></div> |
1717 $man who |
403 <div class="section" title="4.1. apropos and whatis"> |
1718 </pre> |
404 <div class="titlepage"><div><div><h3 class="title"> |
1719 <p id="ch9Using_Linux_Tools_52"> |
405 <a name="id2947108"></a>4.1. apropos and whatis</h3></div></div></div> |
1720 This will take you to the "Manual" page containing details about the |
406 <p><a name="ch9Using_Linux_Tools_60"></a>This is a command to search the manual pages files in Unix and Unix-like operating systems.</p> |
1721 <span class="emphasis"> |
407 <pre class="programlisting"> |
1722 <em> |
408 $ apropos grep |
1723 who |
|
1724 </em> |
|
1725 </span> |
|
1726 command |
|
1727 </p> |
|
1728 </div> |
|
1729 <div class="section" title="3.5.mkdir"> |
|
1730 <div class="titlepage"> |
|
1731 <div> |
|
1732 <div> |
|
1733 <h3 class="title"> |
|
1734 <a name="id2625600"> |
|
1735 </a> |
|
1736 3.5.mkdir |
|
1737 </h3> |
|
1738 </div> |
|
1739 </div> |
|
1740 </div> |
|
1741 <p id="ch9Using_Linux_Tools_53"> |
|
1742 This command is used to make a new directory. Normal usage is as straightforward as follows: |
|
1743 </p> |
|
1744 <pre class="programlisting"> |
|
1745 $mkdir name_of_directory |
|
1746 </pre> |
|
1747 <p id="ch9Using_Linux_Tools_54"> |
|
1748 Where |
|
1749 <span class="emphasis"> |
|
1750 <em> |
|
1751 name_of_directory |
|
1752 </em> |
|
1753 </span> |
|
1754 is the name of the directory one wants to create. When typed as above (ie. normal usage), the new directory would be created within the current directory. On Unix, multiple directories can be specified, and |
|
1755 <span class="emphasis"> |
|
1756 <em> |
|
1757 mkdir |
|
1758 </em> |
|
1759 </span> |
|
1760 will try to create all of them. |
|
1761 </p> |
|
1762 <div class="section" title="3.5.1.Options"> |
|
1763 <div class="titlepage"> |
|
1764 <div> |
|
1765 <div> |
|
1766 <h4 class="title"> |
|
1767 <a name="id2625632"> |
|
1768 </a> |
|
1769 3.5.1.Options |
|
1770 </h4> |
|
1771 </div> |
|
1772 </div> |
|
1773 </div> |
|
1774 <p id="ch9Using_Linux_Tools_55"> |
|
1775 On Unix-like operating systems, |
|
1776 <span class="emphasis"> |
|
1777 <em> |
|
1778 mkdir |
|
1779 </em> |
|
1780 </span> |
|
1781 takes options. Three of the most common options are: |
|
1782 </p> |
|
1783 <div class="itemizedlist"> |
|
1784 <ul class="itemizedlist" type="*"> |
|
1785 <li class="listitem" style="list-style-type: *"> |
|
1786 <p id="ch9Using_Linux_Tools_56"> |
|
1787 <span class="emphasis"> |
|
1788 <em> |
|
1789 -p |
|
1790 </em> |
|
1791 </span> |
|
1792 : will also create all directories leading up to the given directory that do not exist already. If the given directory already exists, ignore the error. |
|
1793 </p> |
|
1794 </li> |
|
1795 <li class="listitem" style="list-style-type: *"> |
|
1796 <p id="ch9Using_Linux_Tools_57"> |
|
1797 <span class="emphasis"> |
|
1798 <em> |
|
1799 -v |
|
1800 </em> |
|
1801 </span> |
|
1802 : display each directory that mkdir creates. Most often used with -p. |
|
1803 </p> |
|
1804 </li> |
|
1805 <li class="listitem" style="list-style-type: *"> |
|
1806 <p id="ch9Using_Linux_Tools_58"> |
|
1807 <span class="emphasis"> |
|
1808 <em> |
|
1809 -m |
|
1810 </em> |
|
1811 </span> |
|
1812 : specify the octal permissions of directories created by mkdir. |
|
1813 </p> |
|
1814 </li> |
|
1815 </ul> |
|
1816 </div> |
|
1817 <p id="ch9Using_Linux_Tools_59"> |
|
1818 <span class="emphasis"> |
|
1819 <em> |
|
1820 -p |
|
1821 </em> |
|
1822 </span> |
|
1823 is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts. |
|
1824 </p> |
|
1825 </div> |
|
1826 <div class="section" title="3.5.2.Examples"> |
|
1827 <div class="titlepage"> |
|
1828 <div> |
|
1829 <div> |
|
1830 <h4 class="title"> |
|
1831 <a name="id2625692"> |
|
1832 </a> |
|
1833 3.5.2.Examples |
|
1834 </h4> |
|
1835 </div> |
|
1836 </div> |
|
1837 </div> |
|
1838 <p id="ch9Using_Linux_Tools_5a"> |
|
1839 An example of |
|
1840 <span class="emphasis"> |
|
1841 <em> |
|
1842 -p |
|
1843 </em> |
|
1844 </span> |
|
1845 in action is: |
|
1846 </p> |
|
1847 <pre class="programlisting"> |
|
1848 $mkdir -p /tmp/a/b/c |
|
1849 </pre> |
|
1850 <p id="ch9Using_Linux_Tools_5b"> |
|
1851 If |
|
1852 <span class="emphasis"> |
|
1853 <em> |
|
1854 /tmp/a |
|
1855 </em> |
|
1856 </span> |
|
1857 exists but |
|
1858 <span class="emphasis"> |
|
1859 <em> |
|
1860 /tmp/a/b |
|
1861 </em> |
|
1862 </span> |
|
1863 does not, mkdir will create |
|
1864 <span class="emphasis"> |
|
1865 <em> |
|
1866 /tmp/a/b |
|
1867 </em> |
|
1868 </span> |
|
1869 before creating |
|
1870 <span class="emphasis"> |
|
1871 <em> |
|
1872 /tmp/a/b/c |
|
1873 </em> |
|
1874 </span> |
|
1875 . |
|
1876 </p> |
|
1877 <p id="ch9Using_Linux_Tools_5c"> |
|
1878 And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself): |
|
1879 </p> |
|
1880 <pre class="programlisting"> |
|
1881 $mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags} |
|
1882 </pre> |
|
1883 <p id="ch9Using_Linux_Tools_5d"> |
|
1884 This will create: |
|
1885 </p> |
|
1886 tmpdir - branches |
|
1887 <div class="itemizedlist"> |
|
1888 <ul class="itemizedlist" type="*"> |
|
1889 <li class="listitem" style="list-style-type: *"> |
|
1890 <p id="ch9Using_Linux_Tools_5e"> |
|
1891 tag |
|
1892 </p> |
|
1893 </li> |
|
1894 <li class="listitem" style="list-style-type: *"> |
|
1895 trunk - sources - includes |
|
1896 <div class="itemizedlist"> |
|
1897 <ul class="itemizedlist" type="*"> |
|
1898 <li class="listitem" style="list-style-type: *"> |
|
1899 <p id="ch9Using_Linux_Tools_5f"> |
|
1900 docs |
|
1901 </p> |
|
1902 </li> |
|
1903 </ul> |
|
1904 </div> |
|
1905 </li> |
|
1906 </ul> |
|
1907 </div> |
|
1908 </div> |
|
1909 </div> |
|
1910 </div> |
|
1911 <div class="section" title="4.Getting Help"> |
|
1912 <div class="titlepage"> |
|
1913 <div> |
|
1914 <div> |
|
1915 <h2 class="title" style="clear: both"> |
|
1916 <a name="id2625768"> |
|
1917 </a> |
|
1918 4.Getting Help |
|
1919 </h2> |
|
1920 </div> |
|
1921 </div> |
|
1922 </div> |
|
1923 <div class="section" title="4.1.apropos and whatis"> |
|
1924 <div class="titlepage"> |
|
1925 <div> |
|
1926 <div> |
|
1927 <h3 class="title"> |
|
1928 <a name="id2625776"> |
|
1929 </a> |
|
1930 4.1.apropos and whatis |
|
1931 </h3> |
|
1932 </div> |
|
1933 </div> |
|
1934 </div> |
|
1935 <p id="ch9Using_Linux_Tools_60"> |
|
1936 This is a command to search the manual pages files in Unix and Unix-like operating systems. |
|
1937 </p> |
|
1938 <pre class="programlisting"> |
|
1939 $ apropos grep |
409 egrep egrep (1) Search a file for a pattern using full regular expressions |
1940 egrep egrep (1) Search a file for a pattern using full regular expressions |
410 fgrep fgrep (1) Search a file for a fixed-character string |
1941 fgrep fgrep (1) Search a file for a fixed-character string |
411 fmlgrep fmlgrep (1) Search a file for a pattern |
1942 fmlgrep fmlgrep (1) Search a file for a pattern |
412 grep grep (1) Search a file for a pattern |
1943 grep grep (1) Search a file for a pattern |
413 gzgrep gzgrep (1) Search a possibly compressed file for a regular expression |
1944 gzgrep gzgrep (1) Search a possibly compressed file for a regular expression |
414 nisgrep nismatch (1) Utilities for searching NIS+ tables |
1945 nisgrep nismatch (1) Utilities for searching NIS+ tables |
415 pgrep pgrep (1) Find or signal a process by name or other attribute |
1946 pgrep pgrep (1) Find or signal a process by name or other attribute |
416 zgrep zgrep (1) Search a possibly compressed file for a regular expression |
1947 zgrep zgrep (1) Search a possibly compressed file for a regular expression |
417 ...</pre> |
1948 ... |
418 <p><a name="ch9Using_Linux_Tools_61"></a>In this example, the user uses <span class="emphasis"><em>apropos</em></span> to search for the string "grep", and apropos returns the indicated <span class="emphasis"><em>man</em></span> pages that include the term "grep".</p> |
1949 </pre> |
419 <p><a name="ch9Using_Linux_Tools_62"></a>A short index of explanations for commands is available using the <span class="emphasis"><em>whatis</em></span> command, like in the examples below:</p> |
1950 <p id="ch9Using_Linux_Tools_61"> |
420 <pre class="programlisting"> |
1951 In this example, the user uses |
421 $whatis ls |
1952 <span class="emphasis"> |
422 ls (1) - list directory contents</pre> |
1953 <em> |
423 <p><a name="ch9Using_Linux_Tools_63"></a>This displays short information about a command, and the first section in the collection of man pages that contains an appropriate page.</p> |
1954 apropos |
424 <p><a name="ch9Using_Linux_Tools_64"></a>If you don't know where to get started and which man page to read, <span class="emphasis"><em>apropos</em></span> gives more information. Say that you do not know how to start a browser, then you could enter the following command:</p> |
1955 </em> |
425 <pre class="programlisting"> |
1956 </span> |
426 $apropos browser |
1957 to search for the string "grep", and apropos returns the indicated |
|
1958 <span class="emphasis"> |
|
1959 <em> |
|
1960 man |
|
1961 </em> |
|
1962 </span> |
|
1963 pages that include the term "grep". |
|
1964 </p> |
|
1965 <p id="ch9Using_Linux_Tools_62"> |
|
1966 A short index of explanations for commands is available using the |
|
1967 <span class="emphasis"> |
|
1968 <em> |
|
1969 whatis |
|
1970 </em> |
|
1971 </span> |
|
1972 command, like in the examples below: |
|
1973 </p> |
|
1974 <pre class="programlisting"> |
|
1975 $whatis ls |
|
1976 ls (1) - list directory contents |
|
1977 </pre> |
|
1978 <p id="ch9Using_Linux_Tools_63"> |
|
1979 This displays short information about a command, and the first section in the collection of man pages that contains an appropriate page. |
|
1980 </p> |
|
1981 <p id="ch9Using_Linux_Tools_64"> |
|
1982 If you don't know where to get started and which man page to read, |
|
1983 <span class="emphasis"> |
|
1984 <em> |
|
1985 apropos |
|
1986 </em> |
|
1987 </span> |
|
1988 gives more information. Say that you do not know how to start a browser, then you could enter the following command: |
|
1989 </p> |
|
1990 <pre class="programlisting"> |
|
1991 $apropos browser |
427 gmusicbrowser (1) - Jukebox for large collections of audio files |
1992 gmusicbrowser (1) - Jukebox for large collections of audio files |
428 infobrowser (1) - read Info documents |
1993 infobrowser (1) - read Info documents |
429 libsmbclient (7) - An extension library for browsers and that can be used... |
1994 libsmbclient (7) - An extension library for browsers and that can be used... |
430 opera (1) - a standards-compliant graphical Web browser |
1995 opera (1) - a standards-compliant graphical Web browser |
431 sensible-browser (1) - sensible editing, paging, and web browsing |
1996 sensible-browser (1) - sensible editing, paging, and web browsing |
432 smbtree (1) - A text based smb network browser |
1997 smbtree (1) - A text based smb network browser |
433 tvtk_doc (1) - A GUI based TVTK documentation search browser. |
1998 tvtk_doc (1) - A GUI based TVTK documentation search browser. |
434 viewres (1) - graphical class browser for Xt |
1999 viewres (1) - graphical class browser for Xt |
435 w3m (1) - a text based Web browser and pager |
2000 w3m (1) - a text based Web browser and pager |
436 www-browser (1) - a text based Web browser and pager |
2001 www-browser (1) - a text based Web browser and pager |
437 ...</pre> |
2002 ... |
438 </div> |
2003 </pre> |
439 <div class="section" title="4.2. man"> |
2004 </div> |
440 <div class="titlepage"><div><div><h3 class="title"> |
2005 <div class="section" title="4.2.man"> |
441 <a name="id2947202"></a>4.2. man</h3></div></div></div> |
2006 <div class="titlepage"> |
442 <p><a name="ch9Using_Linux_Tools_65"></a>Man pages (short for "manual pages") are the extensive documentation that comes preinstalled with almost all substantial Unix and Unix-like operating systems. The Unix command used to display them is <span class="emphasis"><em>man</em></span>. Each page is a self-contained document.</p> |
2007 <div> |
443 <p><a name="ch9Using_Linux_Tools_66"></a>To read a manual page for a Unix command, one can use:</p> |
2008 <div> |
444 <pre class="programlisting"> |
2009 <h3 class="title"> |
445 $ man <command_name></pre> |
2010 <a name="id2625871"> |
446 <p><a name="ch9Using_Linux_Tools_67"></a>at a shell prompt; for example, "man ftp". In order to simplify navigation through the output, <span class="emphasis"><em>man</em></span> generally uses the less terminal pager.</p> |
2011 </a> |
447 <p><a name="ch9Using_Linux_Tools_68"></a>Pages are traditionally referred to using the notation "name(section)"; for example, ftp(1). The same page name may appear in more than one section of the manual, this can occur when the names of system calls, user commands, or macro packages coincide. Two examples are <span class="emphasis"><em>man(1)</em></span> and <span class="emphasis"><em>man(7)</em></span>, or <span class="emphasis"><em>exit(2)</em></span> and <span class="emphasis"><em>exit(3)</em></span>. The syntax for accessing the non-default manual section varies between different man implementations. On Linux and <span class="emphasis"><em>BSD, for example, the syntax for reading *printf(3)</em></span> is:</p> |
2012 4.2.man |
448 <pre class="programlisting"> |
2013 </h3> |
449 $man 3 printf</pre> |
2014 </div> |
450 <p><a name="ch9Using_Linux_Tools_69"></a>Another example:</p> |
2015 </div> |
451 <pre class="programlisting"> |
2016 </div> |
452 $man man</pre> |
2017 <p id="ch9Using_Linux_Tools_65"> |
453 <p><a name="ch9Using_Linux_Tools_6a"></a>The previous example will take you to the "Manual" page entry about manual pages!</p> |
2018 Man pages (short for "manual pages") are the extensive documentation that comes preinstalled with almost all substantial Unix and Unix-like operating systems. The Unix command used to display them is |
454 <div class="section" title="4.2.1. Layout"> |
2019 <span class="emphasis"> |
455 <div class="titlepage"><div><div><h4 class="title"> |
2020 <em> |
456 <a name="id2947285"></a>4.2.1. Layout</h4></div></div></div> |
2021 man |
457 <p><a name="ch9Using_Linux_Tools_6b"></a>All man pages follow a common layout that is optimized for presentation on a simple ASCII text display, possibly without any form of highlighting or font control. Sections present may include:</p>NAME<p><a name="ch9Using_Linux_Tools_6c"></a>The name of the command or function, followed by a one-line description of what it does.</p>SYNOPSIS<p><a name="ch9Using_Linux_Tools_6d"></a>In the case of a command, you get a formal description of how to run it and what command line options it takes. For program functions, a list of the parameters the function takes and which header file contains its definition. For experienced users, this may be all the documentation they need.</p>DESCRIPTION<p><a name="ch9Using_Linux_Tools_6e"></a>A textual description of the functioning of the command or function.</p>EXAMPLES<p><a name="ch9Using_Linux_Tools_6f"></a>Some examples of common usage.</p>SEE ALSO<p><a name="ch9Using_Linux_Tools_70"></a>A list of related commands or functions.</p> |
2022 </em> |
458 <p><a name="ch9Using_Linux_Tools_71"></a>Other sections may be present, but these are not well standardized across man pages. Common examples include: OPTIONS, EXIT STATUS, ENVIRONMENT, KNOWN BUGS, FILES, AUTHOR, REPORTING BUGS, HISTORY and COPYRIGHT.</p> |
2023 </span> |
459 <p><a name="ch9Using_Linux_Tools_72"></a>These days virtually every Unix command line application comes with its man page, and many Unix users perceive a lack of man pages as a sign of low quality; indeed, some projects, such as Debian, go out of their way to write man pages for programs lacking one. Few alternatives to <span class="emphasis"><em>man</em></span> have enjoyed much popularity, with the possible exception of the GNU project's "info" system, an early and simple hypertext system.</p> |
2024 . Each page is a self-contained document. |
460 <p><a name="ch9Using_Linux_Tools_73"></a>However, the format of a single page for each application, the lack of classification within the sections and the relatively unsophisticated formatting facilities have motivated the development of alternative documentation systems, such as the previously mentioned "info" system.</p> |
2025 </p> |
461 <p><a name="ch9Using_Linux_Tools_74"></a>Most Unix GUI applications (particularly those built using the GNOME and KDE development environments) now provide end-user documentation in HTML and include embedded HTML viewers such as yelp for reading the help within the application.</p> |
2026 <p id="ch9Using_Linux_Tools_66"> |
462 <p><a name="ch9Using_Linux_Tools_75"></a>Usually the man pages are written in English. Translations into other languages can be also available on the system.</p> |
2027 To read a manual page for a Unix command, one can use: |
463 <p><a name="ch9Using_Linux_Tools_76"></a>The default format of the man pages is troff, with either the macro package man (appearance oriented) or on some systems mdoc (semantic oriented). This makes it possible to typeset a man page to PostScript, PDF and various other formats for viewing or printing.</p> |
2028 </p> |
464 </div> |
2029 <pre class="programlisting"> |
465 </div> |
2030 $ man <command_name> |
466 <div class="section" title="4.3. info"> |
2031 </pre> |
467 <div class="titlepage"><div><div><h3 class="title"> |
2032 <p id="ch9Using_Linux_Tools_67"> |
468 <a name="id2947396"></a>4.3. info</h3></div></div></div> |
2033 at a shell prompt; for example, "man ftp". In order to simplify navigation through the output, |
469 <p><a name="ch9Using_Linux_Tools_77"></a><span class="emphasis"><em>info</em></span> is a software utility which forms a hypertextual, multipage documentation and help viewer working on a command line interface, useful when there is no GUI available.</p> |
2034 <span class="emphasis"> |
470 <p><a name="ch9Using_Linux_Tools_78"></a>The syntax is</p> |
2035 <em> |
471 <pre class="programlisting"> |
2036 man |
472 $ info <command_name></pre> |
2037 </em> |
473 <p><a name="ch9Using_Linux_Tools_79"></a><span class="emphasis"><em>info</em></span> processes info files, which are Texinfo formatted files, and presents the documentation as a tree, with simple commands to traverse the tree and to follow cross references. For instance</p> |
2038 </span> |
474 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
2039 generally uses the less terminal pager. |
475 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_7a"></a><span class="emphasis"><em>n</em></span> goes to the next page.</p></li> |
2040 </p> |
476 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_7b"></a><span class="emphasis"><em>p</em></span> goes to the previous page.</p></li> |
2041 <p id="ch9Using_Linux_Tools_68"> |
477 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_7c"></a><span class="emphasis"><em>u</em></span> goes to the upper page.</p></li> |
2042 Pages are traditionally referred to using the notation "name(section)"; for example, ftp(1). The same page name may appear in more than one section of the manual, this can occur when the names of system calls, user commands, or macro packages coincide. Two examples are |
478 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_7d"></a><span class="emphasis"><em>l</em></span> goes to the last(visited) node</p></li> |
2043 <span class="emphasis"> |
479 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_7e"></a>To follow a cross reference, the cursor can be moved over a link (a word preceded by a *) and enter pressed.</p></li> |
2044 <em> |
480 </ul></div> |
2045 man(1) |
481 <p><a name="ch9Using_Linux_Tools_7f"></a>info was initially written for use with GNU/Linux and then ported to other Unix-like operating systems.</p> |
2046 </em> |
482 </div> |
2047 </span> |
483 <div class="section" title="4.4. --help"> |
2048 and |
484 <div class="titlepage"><div><div><h3 class="title"> |
2049 <span class="emphasis"> |
485 <a name="id2947484"></a>4.4. --help</h3></div></div></div> |
2050 <em> |
486 <p><a name="ch9Using_Linux_Tools_80"></a>Most GNU commands support the --help, which gives a short explanation about how to use the command and a list of available options. Below is the output of this option with the <span class="emphasis"><em>cat</em></span> command:</p> |
2051 man(7) |
487 <pre class="programlisting"> |
2052 </em> |
488 $ userprompt@host: cat --help |
2053 </span> |
|
2054 , or |
|
2055 <span class="emphasis"> |
|
2056 <em> |
|
2057 exit(2) |
|
2058 </em> |
|
2059 </span> |
|
2060 and |
|
2061 <span class="emphasis"> |
|
2062 <em> |
|
2063 exit(3) |
|
2064 </em> |
|
2065 </span> |
|
2066 . The syntax for accessing the non-default manual section varies between different man implementations. On Linux and |
|
2067 <span class="emphasis"> |
|
2068 <em> |
|
2069 BSD, for example, the syntax for reading *printf(3) |
|
2070 </em> |
|
2071 </span> |
|
2072 is: |
|
2073 </p> |
|
2074 <pre class="programlisting"> |
|
2075 $man 3 printf |
|
2076 </pre> |
|
2077 <p id="ch9Using_Linux_Tools_69"> |
|
2078 Another example: |
|
2079 </p> |
|
2080 <pre class="programlisting"> |
|
2081 $man man |
|
2082 </pre> |
|
2083 <p id="ch9Using_Linux_Tools_6a"> |
|
2084 The previous example will take you to the "Manual" page entry about manual pages! |
|
2085 </p> |
|
2086 <div class="section" title="4.2.1.Layout"> |
|
2087 <div class="titlepage"> |
|
2088 <div> |
|
2089 <div> |
|
2090 <h4 class="title"> |
|
2091 <a name="id2625954"> |
|
2092 </a> |
|
2093 4.2.1.Layout |
|
2094 </h4> |
|
2095 </div> |
|
2096 </div> |
|
2097 </div> |
|
2098 <p id="ch9Using_Linux_Tools_6b"> |
|
2099 All man pages follow a common layout that is optimized for presentation on a simple ASCII text display, possibly without any form of highlighting or font control. Sections present may include: |
|
2100 </p> |
|
2101 NAME |
|
2102 <p id="ch9Using_Linux_Tools_6c"> |
|
2103 The name of the command or function, followed by a one-line description of what it does. |
|
2104 </p> |
|
2105 SYNOPSIS |
|
2106 <p id="ch9Using_Linux_Tools_6d"> |
|
2107 In the case of a command, you get a formal description of how to run it and what command line options it takes. For program functions, a list of the parameters the function takes and which header file contains its definition. For experienced users, this may be all the documentation they need. |
|
2108 </p> |
|
2109 DESCRIPTION |
|
2110 <p id="ch9Using_Linux_Tools_6e"> |
|
2111 A textual description of the functioning of the command or function. |
|
2112 </p> |
|
2113 EXAMPLES |
|
2114 <p id="ch9Using_Linux_Tools_6f"> |
|
2115 Some examples of common usage. |
|
2116 </p> |
|
2117 SEE ALSO |
|
2118 <p id="ch9Using_Linux_Tools_70"> |
|
2119 A list of related commands or functions. |
|
2120 </p> |
|
2121 <p id="ch9Using_Linux_Tools_71"> |
|
2122 Other sections may be present, but these are not well standardized across man pages. Common examples include: OPTIONS, EXIT STATUS, ENVIRONMENT, KNOWN BUGS, FILES, AUTHOR, REPORTING BUGS, HISTORY and COPYRIGHT. |
|
2123 </p> |
|
2124 <p id="ch9Using_Linux_Tools_72"> |
|
2125 These days virtually every Unix command line application comes with its man page, and many Unix users perceive a lack of man pages as a sign of low quality; indeed, some projects, such as Debian, go out of their way to write man pages for programs lacking one. Few alternatives to |
|
2126 <span class="emphasis"> |
|
2127 <em> |
|
2128 man |
|
2129 </em> |
|
2130 </span> |
|
2131 have enjoyed much popularity, with the possible exception of the GNU project's "info" system, an early and simple hypertext system. |
|
2132 </p> |
|
2133 <p id="ch9Using_Linux_Tools_73"> |
|
2134 However, the format of a single page for each application, the lack of classification within the sections and the relatively unsophisticated formatting facilities have motivated the development of alternative documentation systems, such as the previously mentioned "info" system. |
|
2135 </p> |
|
2136 <p id="ch9Using_Linux_Tools_74"> |
|
2137 Most Unix GUI applications (particularly those built using the GNOME and KDE development environments) now provide end-user documentation in HTML and include embedded HTML viewers such as yelp for reading the help within the application. |
|
2138 </p> |
|
2139 <p id="ch9Using_Linux_Tools_75"> |
|
2140 Usually the man pages are written in English. Translations into other languages can be also available on the system. |
|
2141 </p> |
|
2142 <p id="ch9Using_Linux_Tools_76"> |
|
2143 The default format of the man pages is troff, with either the macro package man (appearance oriented) or on some systems mdoc (semantic oriented). This makes it possible to typeset a man page to PostScript, PDF and various other formats for viewing or printing. |
|
2144 </p> |
|
2145 </div> |
|
2146 </div> |
|
2147 <div class="section" title="4.3.info"> |
|
2148 <div class="titlepage"> |
|
2149 <div> |
|
2150 <div> |
|
2151 <h3 class="title"> |
|
2152 <a name="id2626065"> |
|
2153 </a> |
|
2154 4.3.info |
|
2155 </h3> |
|
2156 </div> |
|
2157 </div> |
|
2158 </div> |
|
2159 <p id="ch9Using_Linux_Tools_77"> |
|
2160 <span class="emphasis"> |
|
2161 <em> |
|
2162 info |
|
2163 </em> |
|
2164 </span> |
|
2165 is a software utility which forms a hypertextual, multipage documentation and help viewer working on a command line interface, useful when there is no GUI available. |
|
2166 </p> |
|
2167 <p id="ch9Using_Linux_Tools_78"> |
|
2168 The syntax is |
|
2169 </p> |
|
2170 <pre class="programlisting"> |
|
2171 $ info <command_name> |
|
2172 </pre> |
|
2173 <p id="ch9Using_Linux_Tools_79"> |
|
2174 <span class="emphasis"> |
|
2175 <em> |
|
2176 info |
|
2177 </em> |
|
2178 </span> |
|
2179 processes info files, which are Texinfo formatted files, and presents the documentation as a tree, with simple commands to traverse the tree and to follow cross references. For instance |
|
2180 </p> |
|
2181 <div class="itemizedlist"> |
|
2182 <ul class="itemizedlist" type="*"> |
|
2183 <li class="listitem" style="list-style-type: *"> |
|
2184 <p id="ch9Using_Linux_Tools_7a"> |
|
2185 <span class="emphasis"> |
|
2186 <em> |
|
2187 n |
|
2188 </em> |
|
2189 </span> |
|
2190 goes to the next page. |
|
2191 </p> |
|
2192 </li> |
|
2193 <li class="listitem" style="list-style-type: *"> |
|
2194 <p id="ch9Using_Linux_Tools_7b"> |
|
2195 <span class="emphasis"> |
|
2196 <em> |
|
2197 p |
|
2198 </em> |
|
2199 </span> |
|
2200 goes to the previous page. |
|
2201 </p> |
|
2202 </li> |
|
2203 <li class="listitem" style="list-style-type: *"> |
|
2204 <p id="ch9Using_Linux_Tools_7c"> |
|
2205 <span class="emphasis"> |
|
2206 <em> |
|
2207 u |
|
2208 </em> |
|
2209 </span> |
|
2210 goes to the upper page. |
|
2211 </p> |
|
2212 </li> |
|
2213 <li class="listitem" style="list-style-type: *"> |
|
2214 <p id="ch9Using_Linux_Tools_7d"> |
|
2215 <span class="emphasis"> |
|
2216 <em> |
|
2217 l |
|
2218 </em> |
|
2219 </span> |
|
2220 goes to the last(visited) node |
|
2221 </p> |
|
2222 </li> |
|
2223 <li class="listitem" style="list-style-type: *"> |
|
2224 <p id="ch9Using_Linux_Tools_7e"> |
|
2225 To follow a cross reference, the cursor can be moved over a link (a word preceded by a *) and enter pressed. |
|
2226 </p> |
|
2227 </li> |
|
2228 </ul> |
|
2229 </div> |
|
2230 <p id="ch9Using_Linux_Tools_7f"> |
|
2231 info was initially written for use with GNU/Linux and then ported to other Unix-like operating systems. |
|
2232 </p> |
|
2233 </div> |
|
2234 <div class="section" title="4.4.--help"> |
|
2235 <div class="titlepage"> |
|
2236 <div> |
|
2237 <div> |
|
2238 <h3 class="title"> |
|
2239 <a name="id2626152"> |
|
2240 </a> |
|
2241 4.4.--help |
|
2242 </h3> |
|
2243 </div> |
|
2244 </div> |
|
2245 </div> |
|
2246 <p id="ch9Using_Linux_Tools_80"> |
|
2247 Most GNU commands support the --help, which gives a short explanation about how to use the command and a list of available options. Below is the output of this option with the |
|
2248 <span class="emphasis"> |
|
2249 <em> |
|
2250 cat |
|
2251 </em> |
|
2252 </span> |
|
2253 command: |
|
2254 </p> |
|
2255 <pre class="programlisting"> |
|
2256 $ userprompt@host: cat --help |
489 Usage: cat [OPTION] [FILE]... |
2257 Usage: cat [OPTION] [FILE]... |
490 Concatenate FILE(s), or standard input, to standard output. |
2258 Concatenate FILE(s), or standard input, to standard output. |
491 |
2259 |
492 -A, --show-all equivalent to -vET |
2260 -A, --show-all equivalent to -vET |
493 -b, --number-nonblank number nonempty output lines |
2261 -b, --number-nonblank number nonempty output lines |
506 |
2274 |
507 Examples: |
2275 Examples: |
508 cat f - g Output f's contents, then standard input, then g's contents. |
2276 cat f - g Output f's contents, then standard input, then g's contents. |
509 cat Copy standard input to standard output. |
2277 cat Copy standard input to standard output. |
510 |
2278 |
511 Report bugs to <bug-coreutils@gnu.org>.</pre> |
2279 Report bugs to <bug-coreutils@gnu.org>. |
512 </div> |
2280 </pre> |
513 </div> |
2281 </div> |
514 <div class="section" title="5. Basic file handling"> |
2282 </div> |
515 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
2283 <div class="section" title="5.Basic file handling"> |
516 <a name="id2947536"></a>5. Basic file handling</h2></div></div></div> |
2284 <div class="titlepage"> |
517 <div class="section" title="5.1. cp"> |
2285 <div> |
518 <div class="titlepage"><div><div><h3 class="title"> |
2286 <div> |
519 <a name="id2947545"></a>5.1. cp</h3></div></div></div> |
2287 <h2 class="title" style="clear: both"> |
520 <p><a name="ch9Using_Linux_Tools_81"></a><span class="emphasis"><em>cp</em></span> is the command entered in a Unix shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name.</p> |
2288 <a name="id2626205"> |
521 <div class="section" title="5.1.1. Usage"> |
2289 </a> |
522 <div class="titlepage"><div><div><h4 class="title"> |
2290 5.Basic file handling |
523 <a name="id2947563"></a>5.1.1. Usage</h4></div></div></div> |
2291 </h2> |
524 <p><a name="ch9Using_Linux_Tools_82"></a>To copy a file to another file:</p> |
2292 </div> |
525 <pre class="programlisting"> |
2293 </div> |
526 $ cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile</pre> |
2294 </div> |
527 <p><a name="ch9Using_Linux_Tools_83"></a>To copy a file to a directory:</p> |
2295 <div class="section" title="5.1.cp"> |
528 <pre class="programlisting"> |
2296 <div class="titlepage"> |
529 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory</pre> |
2297 <div> |
530 <p><a name="ch9Using_Linux_Tools_84"></a>To copy a directory to a directory:</p> |
2298 <div> |
531 <pre class="programlisting"> |
2299 <h3 class="title"> |
532 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } |
2300 <a name="id2626214"> |
533 SourceDirectory ... TargetDirectory</pre> |
2301 </a> |
534 </div> |
2302 5.1.cp |
535 <div class="section" title="5.1.2. Flags"> |
2303 </h3> |
536 <div class="titlepage"><div><div><h4 class="title"> |
2304 </div> |
537 <a name="id2947597"></a>5.1.2. Flags</h4></div></div></div> |
2305 </div> |
538 <p><a name="ch9Using_Linux_Tools_85"></a><span class="emphasis"><em>-f</em></span> (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command.</p> |
2306 </div> |
539 <p><a name="ch9Using_Linux_Tools_86"></a><span class="emphasis"><em>-P</em></span> – makes the cp command copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point.</p> |
2307 <p id="ch9Using_Linux_Tools_81"> |
540 <p><a name="ch9Using_Linux_Tools_87"></a><span class="emphasis"><em>-i</em></span> (interactive) – prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.</p> |
2308 <span class="emphasis"> |
541 <p><a name="ch9Using_Linux_Tools_88"></a><span class="emphasis"><em>-p</em></span> (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:</p> |
2309 <em> |
542 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
2310 cp |
543 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_89"></a>The time of the last data modification and the time of the last access.</p></li> |
2311 </em> |
544 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_8a"></a>The user ID and group ID (only if it has permissions to do this)</p></li> |
2312 </span> |
545 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_8b"></a>The file permission bits and the SUID and SGID bits.</p></li> |
2313 is the command entered in a Unix shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name. |
546 </ul></div> |
2314 </p> |
547 <p><a name="ch9Using_Linux_Tools_8c"></a><span class="emphasis"><em>-R</em></span> (recursive) – copy directories (recursively copying all the contents)</p> |
2315 <div class="section" title="5.1.1.Usage"> |
548 </div> |
2316 <div class="titlepage"> |
549 <div class="section" title="5.1.3. Examples"> |
2317 <div> |
550 <div class="titlepage"><div><div><h4 class="title"> |
2318 <div> |
551 <a name="id2947695"></a>5.1.3. Examples</h4></div></div></div> |
2319 <h4 class="title"> |
552 <p><a name="ch9Using_Linux_Tools_8d"></a>To make a copy of a file in the current directory, enter:</p> |
2320 <a name="id2626231"> |
553 <pre class="programlisting"> |
2321 </a> |
554 $ cp prog.c prog.bak</pre> |
2322 5.1.1.Usage |
555 <p><a name="ch9Using_Linux_Tools_8e"></a>This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file.</p> |
2323 </h4> |
556 <p><a name="ch9Using_Linux_Tools_8f"></a>To copy a file in your current directory into another directory, enter:</p> |
2324 </div> |
557 <pre class="programlisting"> |
2325 </div> |
558 $ cp zaphod /home/books/hhgg</pre> |
2326 </div> |
559 <p><a name="ch9Using_Linux_Tools_90"></a>This copies the jones file to /home/books/hhgg/zaphod.</p> |
2327 <p id="ch9Using_Linux_Tools_82"> |
560 <p><a name="ch9Using_Linux_Tools_91"></a>To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:</p> |
2328 To copy a file to another file: |
561 <pre class="programlisting"> |
2329 </p> |
562 $ cp -p martin_luther_king martin_luther_king.jr</pre> |
2330 <pre class="programlisting"> |
563 <p><a name="ch9Using_Linux_Tools_92"></a>This copies the <span class="emphasis"><em>martin_luther_king</em></span> file to the <span class="emphasis"><em>martin_luther_king.jr</em></span> file. Instead of creating the file with the current date and time stamp, the system gives the <span class="emphasis"><em>martin_luther_king.jr</em></span> file the same date and time as the <span class="emphasis"><em>martin_luther_king</em></span> file. The <span class="emphasis"><em>martin_luther_king.jr</em></span> file also inherits the <span class="emphasis"><em>martin_luther_king</em></span> file's access control protection.</p> |
2331 $ cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile |
564 <p><a name="ch9Using_Linux_Tools_93"></a>To copy all the files in a directory to a new directory, enter:</p> |
2332 </pre> |
565 <pre class="programlisting"> |
2333 <p id="ch9Using_Linux_Tools_83"> |
566 $ cp /home/galactica/clients/* /home/hhgg/customers</pre> |
2334 To copy a file to a directory: |
567 <p><a name="ch9Using_Linux_Tools_94"></a>This copies only the files in the clients directory to the customers directory.</p> |
2335 </p> |
568 <p><a name="ch9Using_Linux_Tools_95"></a>To copy a directory, including all its files and subdirectories, to another directory, enter:</p> |
2336 <pre class="programlisting"> |
569 <p><a name="ch9Using_Linux_Tools_96"></a>$ cp -R /home/hhgg/clients /home/hhgg/customers</p> |
2337 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory |
570 <p><a name="ch9Using_Linux_Tools_97"></a>This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory.</p> |
2338 </pre> |
571 <p><a name="ch9Using_Linux_Tools_98"></a>To copy a specific set of files of any extension to another directory, enter:</p> |
2339 <p id="ch9Using_Linux_Tools_84"> |
572 <pre class="programlisting"> |
2340 To copy a directory to a directory: |
573 $ cp zaphod arthur ford /home/hhgg/clients</pre> |
2341 </p> |
574 <p><a name="ch9Using_Linux_Tools_99"></a>This copies the <span class="emphasis"><em>zaphod</em></span>, <span class="emphasis"><em>arthur</em></span>, and <span class="emphasis"><em>ford</em></span> files in your current working directory to the /home/hhgg/clients directory.</p> |
2342 <pre class="programlisting"> |
575 <p><a name="ch9Using_Linux_Tools_9a"></a>To use pattern-matching characters to copy files, enter:</p> |
2343 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } |
576 <pre class="programlisting"> |
2344 SourceDirectory ... TargetDirectory |
577 $ cp programs/*.py .</pre> |
2345 </pre> |
578 <p><a name="ch9Using_Linux_Tools_9b"></a>This copies the files in the programs directory that end with <span class="emphasis"><em>.py</em></span> to the current directory, signified by the single "." (dot). You must type a space between the <span class="emphasis"><em>py</em></span> and the final dot.</p> |
2346 </div> |
579 </div> |
2347 <div class="section" title="5.1.2.Flags"> |
580 </div> |
2348 <div class="titlepage"> |
581 <div class="section" title="5.2. mv"> |
2349 <div> |
582 <div class="titlepage"><div><div><h3 class="title"> |
2350 <div> |
583 <a name="id2947858"></a>5.2. mv</h3></div></div></div> |
2351 <h4 class="title"> |
584 <p><a name="ch9Using_Linux_Tools_9c"></a><span class="emphasis"><em>mv</em></span> (short for move) is a Unix command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. If possible (i.e. when the original and new files are on the same file system), <span class="emphasis"><em>mv</em></span> will rename the file instead. Write permission is required on all directories being modified.</p> |
2352 <a name="id2626266"> |
585 <div class="section" title="5.2.1. Conflicting existing file"> |
2353 </a> |
586 <div class="titlepage"><div><div><h4 class="title"> |
2354 5.1.2.Flags |
587 <a name="id2947882"></a>5.2.1. Conflicting existing file</h4></div></div></div> |
2355 </h4> |
588 <p><a name="ch9Using_Linux_Tools_9d"></a>In all cases, when a file is moved to have the name of an existing file (in the same directory), the existing file is deleted. If the existing file is not writable but is in a directory that is writable, then the mv command asks for confirmation if possible (i.e. if run from a terminal) before proceeding, unless the -f (force) option is used.</p> |
2356 </div> |
589 </div> |
2357 </div> |
590 <div class="section" title="5.2.2. Differences with copy and delete"> |
2358 </div> |
591 <div class="titlepage"><div><div><h4 class="title"> |
2359 <p id="ch9Using_Linux_Tools_85"> |
592 <a name="id2947901"></a>5.2.2. Differences with copy and delete</h4></div></div></div> |
2360 <span class="emphasis"> |
593 <p><a name="ch9Using_Linux_Tools_9e"></a>Note that, usually, when moving files within the same volume, moving (and/or renaming) is not the same as simply copying and then deleting the original. When moving a file, the link is simply removed from the old parent directory and added to the new parent directory. However, the file itself is untouched (i.e. it has the same inodes and resides at the same place on the disk). For example, you cannot copy a file you cannot read, but you can move (and/or rename) it (provided you have write permission to its old and new parent directories). Also, suppose there is a non-empty directory you do not have write permission to. You cannot delete this directory (since you cannot delete its contents); but you can move (and/or rename) it. Also, since moving between filenames on a single volume does not involve copying, it is faster and does not place strain of lots of reads and writes on the disk. Moving files across different volumes, however, does necessitate copying and deleting.</p> |
2361 <em> |
594 </div> |
2362 -f |
595 <div class="section" title="5.2.3. Examples"> |
2363 </em> |
596 <div class="titlepage"><div><div><h4 class="title"> |
2364 </span> |
597 <a name="id2947932"></a>5.2.3. Examples</h4></div></div></div> |
2365 (force) specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command. |
598 <pre class="programlisting"> |
2366 </p> |
599 $ mv myfile mynewfilename renames a file |
2367 <p id="ch9Using_Linux_Tools_86"> |
|
2368 <span class="emphasis"> |
|
2369 <em> |
|
2370 -P |
|
2371 </em> |
|
2372 </span> |
|
2373 makes the cp command copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point. |
|
2374 </p> |
|
2375 <p id="ch9Using_Linux_Tools_87"> |
|
2376 <span class="emphasis"> |
|
2377 <em> |
|
2378 -i |
|
2379 </em> |
|
2380 </span> |
|
2381 (interactive) prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file. |
|
2382 </p> |
|
2383 <p id="ch9Using_Linux_Tools_88"> |
|
2384 <span class="emphasis"> |
|
2385 <em> |
|
2386 -p |
|
2387 </em> |
|
2388 </span> |
|
2389 (preserve) duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory: |
|
2390 </p> |
|
2391 <div class="itemizedlist"> |
|
2392 <ul class="itemizedlist" type="*"> |
|
2393 <li class="listitem" style="list-style-type: *"> |
|
2394 <p id="ch9Using_Linux_Tools_89"> |
|
2395 The time of the last data modification and the time of the last access. |
|
2396 </p> |
|
2397 </li> |
|
2398 <li class="listitem" style="list-style-type: *"> |
|
2399 <p id="ch9Using_Linux_Tools_8a"> |
|
2400 The user ID and group ID (only if it has permissions to do this) |
|
2401 </p> |
|
2402 </li> |
|
2403 <li class="listitem" style="list-style-type: *"> |
|
2404 <p id="ch9Using_Linux_Tools_8b"> |
|
2405 The file permission bits and the SUID and SGID bits. |
|
2406 </p> |
|
2407 </li> |
|
2408 </ul> |
|
2409 </div> |
|
2410 <p id="ch9Using_Linux_Tools_8c"> |
|
2411 <span class="emphasis"> |
|
2412 <em> |
|
2413 -R |
|
2414 </em> |
|
2415 </span> |
|
2416 (recursive) copy directories (recursively copying all the contents) |
|
2417 </p> |
|
2418 </div> |
|
2419 <div class="section" title="5.1.3.Examples"> |
|
2420 <div class="titlepage"> |
|
2421 <div> |
|
2422 <div> |
|
2423 <h4 class="title"> |
|
2424 <a name="id2626363"> |
|
2425 </a> |
|
2426 5.1.3.Examples |
|
2427 </h4> |
|
2428 </div> |
|
2429 </div> |
|
2430 </div> |
|
2431 <p id="ch9Using_Linux_Tools_8d"> |
|
2432 To make a copy of a file in the current directory, enter: |
|
2433 </p> |
|
2434 <pre class="programlisting"> |
|
2435 $ cp prog.c prog.bak |
|
2436 </pre> |
|
2437 <p id="ch9Using_Linux_Tools_8e"> |
|
2438 This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file. |
|
2439 </p> |
|
2440 <p id="ch9Using_Linux_Tools_8f"> |
|
2441 To copy a file in your current directory into another directory, enter: |
|
2442 </p> |
|
2443 <pre class="programlisting"> |
|
2444 $ cp zaphod /home/books/hhgg |
|
2445 </pre> |
|
2446 <p id="ch9Using_Linux_Tools_90"> |
|
2447 This copies the jones file to /home/books/hhgg/zaphod. |
|
2448 </p> |
|
2449 <p id="ch9Using_Linux_Tools_91"> |
|
2450 To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter: |
|
2451 </p> |
|
2452 <pre class="programlisting"> |
|
2453 $ cp -p martin_luther_king martin_luther_king.jr |
|
2454 </pre> |
|
2455 <p id="ch9Using_Linux_Tools_92"> |
|
2456 This copies the |
|
2457 <span class="emphasis"> |
|
2458 <em> |
|
2459 martin_luther_king |
|
2460 </em> |
|
2461 </span> |
|
2462 file to the |
|
2463 <span class="emphasis"> |
|
2464 <em> |
|
2465 martin_luther_king.jr |
|
2466 </em> |
|
2467 </span> |
|
2468 file. Instead of creating the file with the current date and time stamp, the system gives the |
|
2469 <span class="emphasis"> |
|
2470 <em> |
|
2471 martin_luther_king.jr |
|
2472 </em> |
|
2473 </span> |
|
2474 file the same date and time as the |
|
2475 <span class="emphasis"> |
|
2476 <em> |
|
2477 martin_luther_king |
|
2478 </em> |
|
2479 </span> |
|
2480 file. The |
|
2481 <span class="emphasis"> |
|
2482 <em> |
|
2483 martin_luther_king.jr |
|
2484 </em> |
|
2485 </span> |
|
2486 file also inherits the |
|
2487 <span class="emphasis"> |
|
2488 <em> |
|
2489 martin_luther_king |
|
2490 </em> |
|
2491 </span> |
|
2492 file's access control protection. |
|
2493 </p> |
|
2494 <p id="ch9Using_Linux_Tools_93"> |
|
2495 To copy all the files in a directory to a new directory, enter: |
|
2496 </p> |
|
2497 <pre class="programlisting"> |
|
2498 $ cp /home/galactica/clients/* /home/hhgg/customers |
|
2499 </pre> |
|
2500 <p id="ch9Using_Linux_Tools_94"> |
|
2501 This copies only the files in the clients directory to the customers directory. |
|
2502 </p> |
|
2503 <p id="ch9Using_Linux_Tools_95"> |
|
2504 To copy a directory, including all its files and subdirectories, to another directory, enter: |
|
2505 </p> |
|
2506 <p id="ch9Using_Linux_Tools_96"> |
|
2507 $ cp -R /home/hhgg/clients /home/hhgg/customers |
|
2508 </p> |
|
2509 <p id="ch9Using_Linux_Tools_97"> |
|
2510 This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. |
|
2511 </p> |
|
2512 <p id="ch9Using_Linux_Tools_98"> |
|
2513 To copy a specific set of files of any extension to another directory, enter: |
|
2514 </p> |
|
2515 <pre class="programlisting"> |
|
2516 $ cp zaphod arthur ford /home/hhgg/clients |
|
2517 </pre> |
|
2518 <p id="ch9Using_Linux_Tools_99"> |
|
2519 This copies the |
|
2520 <span class="emphasis"> |
|
2521 <em> |
|
2522 zaphod |
|
2523 </em> |
|
2524 </span> |
|
2525 , |
|
2526 <span class="emphasis"> |
|
2527 <em> |
|
2528 arthur |
|
2529 </em> |
|
2530 </span> |
|
2531 , and |
|
2532 <span class="emphasis"> |
|
2533 <em> |
|
2534 ford |
|
2535 </em> |
|
2536 </span> |
|
2537 files in your current working directory to the /home/hhgg/clients directory. |
|
2538 </p> |
|
2539 <p id="ch9Using_Linux_Tools_9a"> |
|
2540 To use pattern-matching characters to copy files, enter: |
|
2541 </p> |
|
2542 <pre class="programlisting"> |
|
2543 $ cp programs/*.py . |
|
2544 </pre> |
|
2545 <p id="ch9Using_Linux_Tools_9b"> |
|
2546 This copies the files in the programs directory that end with |
|
2547 <span class="emphasis"> |
|
2548 <em> |
|
2549 .py |
|
2550 </em> |
|
2551 </span> |
|
2552 to the current directory, signified by the single "." (dot). You must type a space between the |
|
2553 <span class="emphasis"> |
|
2554 <em> |
|
2555 py |
|
2556 </em> |
|
2557 </span> |
|
2558 and the final dot. |
|
2559 </p> |
|
2560 </div> |
|
2561 </div> |
|
2562 <div class="section" title="5.2.mv"> |
|
2563 <div class="titlepage"> |
|
2564 <div> |
|
2565 <div> |
|
2566 <h3 class="title"> |
|
2567 <a name="id2626526"> |
|
2568 </a> |
|
2569 5.2.mv |
|
2570 </h3> |
|
2571 </div> |
|
2572 </div> |
|
2573 </div> |
|
2574 <p id="ch9Using_Linux_Tools_9c"> |
|
2575 <span class="emphasis"> |
|
2576 <em> |
|
2577 mv |
|
2578 </em> |
|
2579 </span> |
|
2580 (short for move) is a Unix command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. If possible (i.e. when the original and new files are on the same file system), |
|
2581 <span class="emphasis"> |
|
2582 <em> |
|
2583 mv |
|
2584 </em> |
|
2585 </span> |
|
2586 will rename the file instead. Write permission is required on all directories being modified. |
|
2587 </p> |
|
2588 <div class="section" title="5.2.1.Conflicting existing file"> |
|
2589 <div class="titlepage"> |
|
2590 <div> |
|
2591 <div> |
|
2592 <h4 class="title"> |
|
2593 <a name="id2626550"> |
|
2594 </a> |
|
2595 5.2.1.Conflicting existing file |
|
2596 </h4> |
|
2597 </div> |
|
2598 </div> |
|
2599 </div> |
|
2600 <p id="ch9Using_Linux_Tools_9d"> |
|
2601 In all cases, when a file is moved to have the name of an existing file (in the same directory), the existing file is deleted. If the existing file is not writable but is in a directory that is writable, then the mv command asks for confirmation if possible (i.e. if run from a terminal) before proceeding, unless the -f (force) option is used. |
|
2602 </p> |
|
2603 </div> |
|
2604 <div class="section" title="5.2.2.Differences with copy and delete"> |
|
2605 <div class="titlepage"> |
|
2606 <div> |
|
2607 <div> |
|
2608 <h4 class="title"> |
|
2609 <a name="id2626570"> |
|
2610 </a> |
|
2611 5.2.2.Differences with copy and delete |
|
2612 </h4> |
|
2613 </div> |
|
2614 </div> |
|
2615 </div> |
|
2616 <p id="ch9Using_Linux_Tools_9e"> |
|
2617 Note that, usually, when moving files within the same volume, moving (and/or renaming) is not the same as simply copying and then deleting the original. When moving a file, the link is simply removed from the old parent directory and added to the new parent directory. However, the file itself is untouched (i.e. it has the same inodes and resides at the same place on the disk). For example, you cannot copy a file you cannot read, but you can move (and/or rename) it (provided you have write permission to its old and new parent directories). Also, suppose there is a non-empty directory you do not have write permission to. You cannot delete this directory (since you cannot delete its contents); but you can move (and/or rename) it. Also, since moving between filenames on a single volume does not involve copying, it is faster and does not place strain of lots of reads and writes on the disk. Moving files across different volumes, however, does necessitate copying and deleting. |
|
2618 </p> |
|
2619 </div> |
|
2620 <div class="section" title="5.2.3.Examples"> |
|
2621 <div class="titlepage"> |
|
2622 <div> |
|
2623 <div> |
|
2624 <h4 class="title"> |
|
2625 <a name="id2626600"> |
|
2626 </a> |
|
2627 5.2.3.Examples |
|
2628 </h4> |
|
2629 </div> |
|
2630 </div> |
|
2631 </div> |
|
2632 <pre class="programlisting"> |
|
2633 $ mv myfile mynewfilename renames a file |
600 $ mv myfile otherfilename renames a file and deletes the existing file "myfile" |
2634 $ mv myfile otherfilename renames a file and deletes the existing file "myfile" |
601 $ mv myfile /myfile moves 'myfile' from the current directory to the root directory |
2635 $ mv myfile /myfile moves 'myfile' from the current directory to the root directory |
602 $ mv myfile dir/myfile moves 'myfile' to 'dir/myfile' relative to the current directory |
2636 $ mv myfile dir/myfile moves 'myfile' to 'dir/myfile' relative to the current directory |
603 $ mv myfile dir same as the previous command (the filename is implied to be the same) |
2637 $ mv myfile dir same as the previous command (the filename is implied to be the same) |
604 $ mv myfile dir/myfile2 moves 'myfile' to dir and renames it to 'myfile2' |
2638 $ mv myfile dir/myfile2 moves 'myfile' to dir and renames it to 'myfile2' |
605 $ mv foo bar baz dir moves multiple files to directory dir |
2639 $ mv foo bar baz dir moves multiple files to directory dir |
606 $ mv --help shows a very concise help about the syntax of the command |
2640 $ mv --help shows a very concise help about the syntax of the command |
607 $ man mv prints an extensive user manual for 'mv' in the terminal</pre> |
2641 $ man mv prints an extensive user manual for 'mv' in the terminal |
608 <p><a name="ch9Using_Linux_Tools_9f"></a>In all cases, the file or files being moved or renamed can be a directory.</p> |
2642 </pre> |
609 <p><a name="ch9Using_Linux_Tools_a0"></a>Note that when the command is called with two arguments (as <span class="emphasis"><em>mv name1 name2</em></span> or <span class="emphasis"><em>mv name1 /dir/name2</em></span>), it can have three different effects, depending on whether <span class="emphasis"><em>name2</em></span> does not exist, is an existing file, or is an existing directory. If the user intends to refer to an existing directory, <span class="emphasis"><em>/.</em></span> (or in some Unix versions <span class="emphasis"><em>/</em></span> is sufficient) may be appended to the name to force the system to check this. To move a file to a new directory, the directory must be created first.</p> |
2643 <p id="ch9Using_Linux_Tools_9f"> |
610 </div> |
2644 In all cases, the file or files being moved or renamed can be a directory. |
611 </div> |
2645 </p> |
612 <div class="section" title="5.3. rm"> |
2646 <p id="ch9Using_Linux_Tools_a0"> |
613 <div class="titlepage"><div><div><h3 class="title"> |
2647 Note that when the command is called with two arguments (as |
614 <a name="id2947990"></a>5.3. rm</h3></div></div></div> |
2648 <span class="emphasis"> |
615 <p><a name="ch9Using_Linux_Tools_a1"></a><span class="emphasis"><em>rm</em></span> (short for "remove") is one of several basic Unix command lines that operates on files. It is used to delete files from a filesystem. The data is not actually destroyed. Only the index listing where the file is stored is destroyed, and the storage is made available for reuse. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused.</p> |
2649 <em> |
616 <p><a name="ch9Using_Linux_Tools_a2"></a>Here's example to remove a file named "foo" from a directory, here shown with the -i option:</p> |
2650 mv name1 name2 |
617 <pre class="programlisting"> |
2651 </em> |
618 $ rm -i foo |
2652 </span> |
619 remove foo? y</pre> |
2653 or |
620 <div class="section" title="5.3.1. Options"> |
2654 <span class="emphasis"> |
621 <div class="titlepage"><div><div><h4 class="title"> |
2655 <em> |
622 <a name="id2948020"></a>5.3.1. Options</h4></div></div></div> |
2656 mv name1 /dir/name2 |
623 <p><a name="ch9Using_Linux_Tools_a3"></a>Common options that rm accepts include:</p> |
2657 </em> |
624 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
2658 </span> |
625 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_a4"></a><span class="emphasis"><em>-r</em></span>, which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive")</p></li> |
2659 ), it can have three different effects, depending on whether |
626 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_a5"></a><span class="emphasis"><em>-i</em></span>, which asks for every deletion to be confirmed ("interactive")</p></li> |
2660 <span class="emphasis"> |
627 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_a6"></a><span class="emphasis"><em>-f</em></span>, which ignores non-existent files and overrides any confirmation prompts ("force")</p></li> |
2661 <em> |
628 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_a7"></a><span class="emphasis"><em>-v</em></span>, which shows what is being removed as it happens ("verbose")</p></li> |
2662 name2 |
629 </ul></div> |
2663 </em> |
630 <p><a name="ch9Using_Linux_Tools_a8"></a><span class="emphasis"><em>rm</em></span> is often aliased to "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence).</p> |
2664 </span> |
631 <p><a name="ch9Using_Linux_Tools_a9"></a><span class="emphasis"><em>rm -rf</em></span> (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted.</p> |
2665 does not exist, is an existing file, or is an existing directory. If the user intends to refer to an existing directory, |
632 <p><a name="ch9Using_Linux_Tools_aa"></a><span class="emphasis"><em>rm</em></span> is often used in conjunction with xargs to supply a list of files to delete:</p> |
2666 <span class="emphasis"> |
633 <pre class="programlisting"> |
2667 <em> |
634 xargs rm < filelist</pre> |
2668 /. |
635 <p><a name="ch9Using_Linux_Tools_ab"></a>When <span class="emphasis"><em>rm</em></span> is used on a symbolic link, it deletes the link, but does not affect the target of the link.</p> |
2669 </em> |
636 </div> |
2670 </span> |
637 <div class="section" title="5.3.2. Permissions"> |
2671 (or in some Unix versions |
638 <div class="titlepage"><div><div><h4 class="title"> |
2672 <span class="emphasis"> |
639 <a name="id2948118"></a>5.3.2. Permissions</h4></div></div></div> |
2673 <em> |
640 <p><a name="ch9Using_Linux_Tools_ac"></a>Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.)</p> |
2674 / |
641 <p><a name="ch9Using_Linux_Tools_ad"></a>To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it.</p> |
2675 </em> |
642 <p><a name="ch9Using_Linux_Tools_ae"></a>If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file.</p> |
2676 </span> |
643 </div> |
2677 is sufficient) may be appended to the name to force the system to check this. To move a file to a new directory, the directory must be created first. |
644 </div> |
2678 </p> |
645 </div> |
2679 </div> |
646 <div class="section" title="6. Command Line Arguments"> |
2680 </div> |
647 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
2681 <div class="section" title="5.3.rm"> |
648 <a name="id2948158"></a>6. Command Line Arguments</h2></div></div></div> |
2682 <div class="titlepage"> |
649 <p><a name="ch9Using_Linux_Tools_af"></a>In computer command line interfaces, a command line argument is an argument sent to a program being called. In general, a program can take any number of command line arguments, which may be necessary for the program to run, or may even be ignored, depending on the function of that program.</p> |
2683 <div> |
650 <p><a name="ch9Using_Linux_Tools_b0"></a>For example, in Unix and Unix-like environments, an example of a command-line argument is:</p> |
2684 <div> |
651 <pre class="programlisting"> |
2685 <h3 class="title"> |
652 rm file.s</pre> |
2686 <a name="id2626659"> |
653 <p><a name="ch9Using_Linux_Tools_b1"></a>"file.s" is a command line argument which tells the program rm to remove the file "file.s".</p> |
2687 </a> |
654 <p><a name="ch9Using_Linux_Tools_b2"></a>Programming languages such as C, C++ and Java allow a program to interpret the command line arguments by handling them as string parameters in the main function.</p> |
2688 5.3.rm |
655 <p><a name="ch9Using_Linux_Tools_b3"></a>A command line option or simply <span class="emphasis"><em>option</em></span> (also known as a command line parameter, flag, or a switch) is an indication by a user that a computer program should change its default output.</p> |
2689 </h3> |
656 <p><a name="ch9Using_Linux_Tools_b4"></a>Long options are introduced via "--", and are typically whole words. For example, <span class="emphasis"><em>ls --long --classify --all</em></span>. Arguments to long options are provided with "=", as <span class="emphasis"><em>ls --block-size=1024</em></span>. Some Unix programs use long options with single dashes, for example MPlayer as in <span class="emphasis"><em>mplayer -nosound</em></span>.</p> |
2690 </div> |
657 <p><a name="ch9Using_Linux_Tools_b5"></a>Linux also uses "--" to terminate option lists. For example, an attempt to delete a file called <span class="emphasis"><em>-file1</em></span> by using <span class="emphasis"><em>rm -file1</em></span> may produce an error, since rm may interpret <span class="emphasis"><em>-file1</em></span> as a command line switch. Using <span class="emphasis"><em>rm -- -file1</em></span> removes ambiguity.</p> |
2691 </div> |
658 </div> |
2692 </div> |
659 <div class="section" title="7. Basic Text Processing"> |
2693 <p id="ch9Using_Linux_Tools_a1"> |
660 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
2694 <span class="emphasis"> |
661 <a name="id2948251"></a>7. Basic Text Processing</h2></div></div></div> |
2695 <em> |
662 <div class="section" title="7.1. head"> |
2696 rm |
663 <div class="titlepage"><div><div><h3 class="title"> |
2697 </em> |
664 <a name="id2948260"></a>7.1. head</h3></div></div></div> |
2698 </span> |
665 <p><a name="ch9Using_Linux_Tools_b6"></a><span class="emphasis"><em>head</em></span> is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is:</p> |
2699 (short for "remove") is one of several basic Unix command lines that operates on files. It is used to delete files from a filesystem. The data is not actually destroyed. Only the index listing where the file is stored is destroyed, and the storage is made available for reuse. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused. |
666 <pre class="programlisting"> |
2700 </p> |
667 $ head [options] <file_name></pre> |
2701 <p id="ch9Using_Linux_Tools_a2"> |
668 <p><a name="ch9Using_Linux_Tools_b7"></a>By default, <span class="emphasis"><em>head</em></span> will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of filename:</p> |
2702 Here's example to remove a file named "foo" from a directory, here shown with the -i option: |
669 <pre class="programlisting"> |
2703 </p> |
670 $ head -n 20 filename</pre> |
2704 <pre class="programlisting"> |
671 <p><a name="ch9Using_Linux_Tools_b8"></a>This displays the first 5 lines of all files starting with <span class="emphasis"><em>foo</em></span>:</p> |
2705 $ rm -i foo |
672 <pre class="programlisting"> |
2706 remove foo? y |
673 $ head -n 5 foo*</pre> |
2707 </pre> |
674 <p><a name="ch9Using_Linux_Tools_b9"></a>Some versions omit the n and just let you say -5.</p> |
2708 <div class="section" title="5.3.1.Options"> |
675 <div class="section" title="7.1.1. Flags"> |
2709 <div class="titlepage"> |
676 <div class="titlepage"><div><div><h4 class="title"> |
2710 <div> |
677 <a name="id2948311"></a>7.1.1. Flags</h4></div></div></div> |
2711 <div> |
678 <pre class="programlisting"> |
2712 <h4 class="title"> |
679 -c <x number of bytes> Copy first x number of bytes.</pre> |
2713 <a name="id2626689"> |
680 <p><a name="ch9Using_Linux_Tools_ba"></a>Other options: <span class="emphasis"><em>sed</em></span></p> |
2714 </a> |
681 <p><a name="ch9Using_Linux_Tools_bb"></a>Many early versions of Unix did not have this command, and so documentation and books had <span class="emphasis"><em>sed</em></span> do this job:</p> |
2715 5.3.1.Options |
682 <pre class="programlisting"> |
2716 </h4> |
683 sed 5q foo</pre> |
2717 </div> |
684 <p><a name="ch9Using_Linux_Tools_bc"></a>This says to print every line (implicit), and quit after the fifth.</p> |
2718 </div> |
685 </div> |
2719 </div> |
686 </div> |
2720 <p id="ch9Using_Linux_Tools_a3"> |
687 <div class="section" title="7.2. tail"> |
2721 Common options that rm accepts include: |
688 <div class="titlepage"><div><div><h3 class="title"> |
2722 </p> |
689 <a name="id2948347"></a>7.2. tail</h3></div></div></div> |
2723 <div class="itemizedlist"> |
690 <p><a name="ch9Using_Linux_Tools_bd"></a><span class="emphasis"><em>tail</em></span> is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data.</p> |
2724 <ul class="itemizedlist" type="*"> |
691 <p><a name="ch9Using_Linux_Tools_be"></a>The command-syntax is:</p> |
2725 <li class="listitem" style="list-style-type: *"> |
692 <pre class="programlisting"> |
2726 <p id="ch9Using_Linux_Tools_a4"> |
693 $ tail [options] <file_name></pre> |
2727 <span class="emphasis"> |
694 <p><a name="ch9Using_Linux_Tools_bf"></a>By default, <span class="emphasis"><em>tail</em></span> will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The following example shows the last 20 lines of filename:</p> |
2728 <em> |
695 <pre class="programlisting"> |
2729 -r |
696 $ tail -n 20 filename</pre> |
2730 </em> |
697 <p><a name="ch9Using_Linux_Tools_c0"></a>This example shows the last 15 bytes of all files starting with <span class="emphasis"><em>foo</em></span>:</p> |
2731 </span> |
698 <pre class="programlisting"> |
2732 , which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive") |
699 $ tail -c 15 foo*</pre> |
2733 </p> |
700 <p><a name="ch9Using_Linux_Tools_c1"></a>This example shows all lines of filename from the second line onwards:</p> |
2734 </li> |
701 <pre class="programlisting"> |
2735 <li class="listitem" style="list-style-type: *"> |
702 $ tail -n +2 filename</pre> |
2736 <p id="ch9Using_Linux_Tools_a5"> |
703 <p><a name="ch9Using_Linux_Tools_c2"></a>Using an older syntax (still used in Sun Solaris as the -n option is not supported), the last 20 lines and the last 50 bytes of filename can be shown with the following command:</p> |
2737 <span class="emphasis"> |
704 <pre class="programlisting"> |
2738 <em> |
705 $ tail -20 filename |
2739 -i |
706 $ tail -50c filename</pre> |
2740 </em> |
707 <p><a name="ch9Using_Linux_Tools_c3"></a>However this syntax is now obsolete and does not conform with the POSIX 1003.1-2001 standard. Even if still supported in current versions, when used with other options (like -f, see below), these switches could not work at all.</p> |
2741 </span> |
708 <div class="section" title="7.2.1. File monitoring"> |
2742 , which asks for every deletion to be confirmed ("interactive") |
709 <div class="titlepage"><div><div><h4 class="title"> |
2743 </p> |
710 <a name="id2948426"></a>7.2.1. File monitoring</h4></div></div></div> |
2744 </li> |
711 <p><a name="ch9Using_Linux_Tools_c4"></a><span class="emphasis"><em>tail</em></span> has a special command line option <span class="emphasis"><em>-f</em></span> (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages:</p> |
2745 <li class="listitem" style="list-style-type: *"> |
712 <pre class="programlisting"> |
2746 <p id="ch9Using_Linux_Tools_a6"> |
713 $ tail -f /var/adm/messages</pre> |
2747 <span class="emphasis"> |
714 <p><a name="ch9Using_Linux_Tools_c5"></a>To interrupt tail while it is monitoring, break-in with <span class="emphasis"><em>Ctrl+C</em></span>. This command can be run "in the background" with &, see job control.</p> |
2748 <em> |
715 <p><a name="ch9Using_Linux_Tools_c6"></a>If you have a command's result to monitor, you can use the <span class="emphasis"><em>watch</em></span> command.</p> |
2749 -f |
716 </div> |
2750 </em> |
717 </div> |
2751 </span> |
718 <div class="section" title="7.3. cut"> |
2752 , which ignores non-existent files and overrides any confirmation prompts ("force") |
719 <div class="titlepage"><div><div><h3 class="title"> |
2753 </p> |
720 <a name="id2948475"></a>7.3. cut</h3></div></div></div> |
2754 </li> |
721 <p><a name="ch9Using_Linux_Tools_c7"></a>In computing, <span class="emphasis"><em>cut</em></span> is a Unix command line utility which is used to extract sections from each line of input — usually from a file.</p> |
2755 <li class="listitem" style="list-style-type: *"> |
722 <p><a name="ch9Using_Linux_Tools_c8"></a>Extraction of line segments can typically be done by <span class="emphasis"><em>bytes (-b), characters (-c)</em></span>, or <span class="emphasis"><em>fields (-f)</em></span> separated by a <span class="emphasis"><em>delimiter (-d — the tab character by default)</em></span>. A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1.</p> |
2756 <p id="ch9Using_Linux_Tools_a7"> |
723 <p><a name="ch9Using_Linux_Tools_c9"></a>Assuming a file named file containing the lines:</p> |
2757 <span class="emphasis"> |
724 <pre class="programlisting"> |
2758 <em> |
725 foo:bar:baz:qux:quux |
2759 -v |
|
2760 </em> |
|
2761 </span> |
|
2762 , which shows what is being removed as it happens ("verbose") |
|
2763 </p> |
|
2764 </li> |
|
2765 </ul> |
|
2766 </div> |
|
2767 <p id="ch9Using_Linux_Tools_a8"> |
|
2768 <span class="emphasis"> |
|
2769 <em> |
|
2770 rm |
|
2771 </em> |
|
2772 </span> |
|
2773 is often aliased to "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence). |
|
2774 </p> |
|
2775 <p id="ch9Using_Linux_Tools_a9"> |
|
2776 <span class="emphasis"> |
|
2777 <em> |
|
2778 rm -rf |
|
2779 </em> |
|
2780 </span> |
|
2781 (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted. |
|
2782 </p> |
|
2783 <p id="ch9Using_Linux_Tools_aa"> |
|
2784 <span class="emphasis"> |
|
2785 <em> |
|
2786 rm |
|
2787 </em> |
|
2788 </span> |
|
2789 is often used in conjunction with xargs to supply a list of files to delete: |
|
2790 </p> |
|
2791 <pre class="programlisting"> |
|
2792 xargs rm < filelist |
|
2793 </pre> |
|
2794 <p id="ch9Using_Linux_Tools_ab"> |
|
2795 When |
|
2796 <span class="emphasis"> |
|
2797 <em> |
|
2798 rm |
|
2799 </em> |
|
2800 </span> |
|
2801 is used on a symbolic link, it deletes the link, but does not affect the target of the link. |
|
2802 </p> |
|
2803 </div> |
|
2804 <div class="section" title="5.3.2.Permissions"> |
|
2805 <div class="titlepage"> |
|
2806 <div> |
|
2807 <div> |
|
2808 <h4 class="title"> |
|
2809 <a name="id2626787"> |
|
2810 </a> |
|
2811 5.3.2.Permissions |
|
2812 </h4> |
|
2813 </div> |
|
2814 </div> |
|
2815 </div> |
|
2816 <p id="ch9Using_Linux_Tools_ac"> |
|
2817 Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.) |
|
2818 </p> |
|
2819 <p id="ch9Using_Linux_Tools_ad"> |
|
2820 To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it. |
|
2821 </p> |
|
2822 <p id="ch9Using_Linux_Tools_ae"> |
|
2823 If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file. |
|
2824 </p> |
|
2825 </div> |
|
2826 </div> |
|
2827 </div> |
|
2828 <div class="section" title="6.Command Line Arguments"> |
|
2829 <div class="titlepage"> |
|
2830 <div> |
|
2831 <div> |
|
2832 <h2 class="title" style="clear: both"> |
|
2833 <a name="id2626827"> |
|
2834 </a> |
|
2835 6.Command Line Arguments |
|
2836 </h2> |
|
2837 </div> |
|
2838 </div> |
|
2839 </div> |
|
2840 <p id="ch9Using_Linux_Tools_af"> |
|
2841 In computer command line interfaces, a command line argument is an argument sent to a program being called. In general, a program can take any number of command line arguments, which may be necessary for the program to run, or may even be ignored, depending on the function of that program. |
|
2842 </p> |
|
2843 <p id="ch9Using_Linux_Tools_b0"> |
|
2844 For example, in Unix and Unix-like environments, an example of a command-line argument is: |
|
2845 </p> |
|
2846 <pre class="programlisting"> |
|
2847 rm file.s |
|
2848 </pre> |
|
2849 <p id="ch9Using_Linux_Tools_b1"> |
|
2850 "file.s" is a command line argument which tells the program rm to remove the file "file.s". |
|
2851 </p> |
|
2852 <p id="ch9Using_Linux_Tools_b2"> |
|
2853 Programming languages such as C, C++ and Java allow a program to interpret the command line arguments by handling them as string parameters in the main function. |
|
2854 </p> |
|
2855 <p id="ch9Using_Linux_Tools_b3"> |
|
2856 A command line option or simply |
|
2857 <span class="emphasis"> |
|
2858 <em> |
|
2859 option |
|
2860 </em> |
|
2861 </span> |
|
2862 (also known as a command line parameter, flag, or a switch) is an indication by a user that a computer program should change its default output. |
|
2863 </p> |
|
2864 <p id="ch9Using_Linux_Tools_b4"> |
|
2865 Long options are introduced via "--", and are typically whole words. For example, |
|
2866 <span class="emphasis"> |
|
2867 <em> |
|
2868 ls --long --classify --all |
|
2869 </em> |
|
2870 </span> |
|
2871 . Arguments to long options are provided with "=", as |
|
2872 <span class="emphasis"> |
|
2873 <em> |
|
2874 ls --block-size=1024 |
|
2875 </em> |
|
2876 </span> |
|
2877 . Some Unix programs use long options with single dashes, for example MPlayer as in |
|
2878 <span class="emphasis"> |
|
2879 <em> |
|
2880 mplayer -nosound |
|
2881 </em> |
|
2882 </span> |
|
2883 . |
|
2884 </p> |
|
2885 <p id="ch9Using_Linux_Tools_b5"> |
|
2886 Linux also uses "--" to terminate option lists. For example, an attempt to delete a file called |
|
2887 <span class="emphasis"> |
|
2888 <em> |
|
2889 -file1 |
|
2890 </em> |
|
2891 </span> |
|
2892 by using |
|
2893 <span class="emphasis"> |
|
2894 <em> |
|
2895 rm -file1 |
|
2896 </em> |
|
2897 </span> |
|
2898 may produce an error, since rm may interpret |
|
2899 <span class="emphasis"> |
|
2900 <em> |
|
2901 -file1 |
|
2902 </em> |
|
2903 </span> |
|
2904 as a command line switch. Using |
|
2905 <span class="emphasis"> |
|
2906 <em> |
|
2907 rm -- -file1 |
|
2908 </em> |
|
2909 </span> |
|
2910 removes ambiguity. |
|
2911 </p> |
|
2912 </div> |
|
2913 <div class="section" title="7.Basic Text Processing"> |
|
2914 <div class="titlepage"> |
|
2915 <div> |
|
2916 <div> |
|
2917 <h2 class="title" style="clear: both"> |
|
2918 <a name="id2626920"> |
|
2919 </a> |
|
2920 7.Basic Text Processing |
|
2921 </h2> |
|
2922 </div> |
|
2923 </div> |
|
2924 </div> |
|
2925 <div class="section" title="7.1.head"> |
|
2926 <div class="titlepage"> |
|
2927 <div> |
|
2928 <div> |
|
2929 <h3 class="title"> |
|
2930 <a name="id2626929"> |
|
2931 </a> |
|
2932 7.1.head |
|
2933 </h3> |
|
2934 </div> |
|
2935 </div> |
|
2936 </div> |
|
2937 <p id="ch9Using_Linux_Tools_b6"> |
|
2938 <span class="emphasis"> |
|
2939 <em> |
|
2940 head |
|
2941 </em> |
|
2942 </span> |
|
2943 is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is: |
|
2944 </p> |
|
2945 <pre class="programlisting"> |
|
2946 $ head [options] <file_name> |
|
2947 </pre> |
|
2948 <p id="ch9Using_Linux_Tools_b7"> |
|
2949 By default, |
|
2950 <span class="emphasis"> |
|
2951 <em> |
|
2952 head |
|
2953 </em> |
|
2954 </span> |
|
2955 will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of filename: |
|
2956 </p> |
|
2957 <pre class="programlisting"> |
|
2958 $ head -n 20 filename |
|
2959 </pre> |
|
2960 <p id="ch9Using_Linux_Tools_b8"> |
|
2961 This displays the first 5 lines of all files starting with |
|
2962 <span class="emphasis"> |
|
2963 <em> |
|
2964 foo |
|
2965 </em> |
|
2966 </span> |
|
2967 : |
|
2968 </p> |
|
2969 <pre class="programlisting"> |
|
2970 $ head -n 5 foo* |
|
2971 </pre> |
|
2972 <p id="ch9Using_Linux_Tools_b9"> |
|
2973 Some versions omit the n and just let you say -5. |
|
2974 </p> |
|
2975 <div class="section" title="7.1.1.Flags"> |
|
2976 <div class="titlepage"> |
|
2977 <div> |
|
2978 <div> |
|
2979 <h4 class="title"> |
|
2980 <a name="id2626980"> |
|
2981 </a> |
|
2982 7.1.1.Flags |
|
2983 </h4> |
|
2984 </div> |
|
2985 </div> |
|
2986 </div> |
|
2987 <pre class="programlisting"> |
|
2988 -c <x number of bytes> Copy first x number of bytes. |
|
2989 </pre> |
|
2990 <p id="ch9Using_Linux_Tools_ba"> |
|
2991 Other options: |
|
2992 <span class="emphasis"> |
|
2993 <em> |
|
2994 sed |
|
2995 </em> |
|
2996 </span> |
|
2997 </p> |
|
2998 <p id="ch9Using_Linux_Tools_bb"> |
|
2999 Many early versions of Unix did not have this command, and so documentation and books had |
|
3000 <span class="emphasis"> |
|
3001 <em> |
|
3002 sed |
|
3003 </em> |
|
3004 </span> |
|
3005 do this job: |
|
3006 </p> |
|
3007 <pre class="programlisting"> |
|
3008 sed 5q foo |
|
3009 </pre> |
|
3010 <p id="ch9Using_Linux_Tools_bc"> |
|
3011 This says to print every line (implicit), and quit after the fifth. |
|
3012 </p> |
|
3013 </div> |
|
3014 </div> |
|
3015 <div class="section" title="7.2.tail"> |
|
3016 <div class="titlepage"> |
|
3017 <div> |
|
3018 <div> |
|
3019 <h3 class="title"> |
|
3020 <a name="id2627016"> |
|
3021 </a> |
|
3022 7.2.tail |
|
3023 </h3> |
|
3024 </div> |
|
3025 </div> |
|
3026 </div> |
|
3027 <p id="ch9Using_Linux_Tools_bd"> |
|
3028 <span class="emphasis"> |
|
3029 <em> |
|
3030 tail |
|
3031 </em> |
|
3032 </span> |
|
3033 is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data. |
|
3034 </p> |
|
3035 <p id="ch9Using_Linux_Tools_be"> |
|
3036 The command-syntax is: |
|
3037 </p> |
|
3038 <pre class="programlisting"> |
|
3039 $ tail [options] <file_name> |
|
3040 </pre> |
|
3041 <p id="ch9Using_Linux_Tools_bf"> |
|
3042 By default, |
|
3043 <span class="emphasis"> |
|
3044 <em> |
|
3045 tail |
|
3046 </em> |
|
3047 </span> |
|
3048 will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The following example shows the last 20 lines of filename: |
|
3049 </p> |
|
3050 <pre class="programlisting"> |
|
3051 $ tail -n 20 filename |
|
3052 </pre> |
|
3053 <p id="ch9Using_Linux_Tools_c0"> |
|
3054 This example shows the last 15 bytes of all files starting with |
|
3055 <span class="emphasis"> |
|
3056 <em> |
|
3057 foo |
|
3058 </em> |
|
3059 </span> |
|
3060 : |
|
3061 </p> |
|
3062 <pre class="programlisting"> |
|
3063 $ tail -c 15 foo* |
|
3064 </pre> |
|
3065 <p id="ch9Using_Linux_Tools_c1"> |
|
3066 This example shows all lines of filename from the second line onwards: |
|
3067 </p> |
|
3068 <pre class="programlisting"> |
|
3069 $ tail -n +2 filename |
|
3070 </pre> |
|
3071 <p id="ch9Using_Linux_Tools_c2"> |
|
3072 Using an older syntax (still used in Sun Solaris as the -n option is not supported), the last 20 lines and the last 50 bytes of filename can be shown with the following command: |
|
3073 </p> |
|
3074 <pre class="programlisting"> |
|
3075 $ tail -20 filename |
|
3076 $ tail -50c filename |
|
3077 </pre> |
|
3078 <p id="ch9Using_Linux_Tools_c3"> |
|
3079 However this syntax is now obsolete and does not conform with the POSIX 1003.1-2001 standard. Even if still supported in current versions, when used with other options (like -f, see below), these switches could not work at all. |
|
3080 </p> |
|
3081 <div class="section" title="7.2.1.File monitoring"> |
|
3082 <div class="titlepage"> |
|
3083 <div> |
|
3084 <div> |
|
3085 <h4 class="title"> |
|
3086 <a name="id2627095"> |
|
3087 </a> |
|
3088 7.2.1.File monitoring |
|
3089 </h4> |
|
3090 </div> |
|
3091 </div> |
|
3092 </div> |
|
3093 <p id="ch9Using_Linux_Tools_c4"> |
|
3094 <span class="emphasis"> |
|
3095 <em> |
|
3096 tail |
|
3097 </em> |
|
3098 </span> |
|
3099 has a special command line option |
|
3100 <span class="emphasis"> |
|
3101 <em> |
|
3102 -f |
|
3103 </em> |
|
3104 </span> |
|
3105 (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages: |
|
3106 </p> |
|
3107 <pre class="programlisting"> |
|
3108 $ tail -f /var/adm/messages |
|
3109 </pre> |
|
3110 <p id="ch9Using_Linux_Tools_c5"> |
|
3111 To interrupt tail while it is monitoring, break-in with |
|
3112 <span class="emphasis"> |
|
3113 <em> |
|
3114 Ctrl+C |
|
3115 </em> |
|
3116 </span> |
|
3117 . This command can be run "in the background" with &, see job control. |
|
3118 </p> |
|
3119 <p id="ch9Using_Linux_Tools_c6"> |
|
3120 If you have a command's result to monitor, you can use the |
|
3121 <span class="emphasis"> |
|
3122 <em> |
|
3123 watch |
|
3124 </em> |
|
3125 </span> |
|
3126 command. |
|
3127 </p> |
|
3128 </div> |
|
3129 </div> |
|
3130 <div class="section" title="7.3.cut"> |
|
3131 <div class="titlepage"> |
|
3132 <div> |
|
3133 <div> |
|
3134 <h3 class="title"> |
|
3135 <a name="id2627144"> |
|
3136 </a> |
|
3137 7.3.cut |
|
3138 </h3> |
|
3139 </div> |
|
3140 </div> |
|
3141 </div> |
|
3142 <p id="ch9Using_Linux_Tools_c7"> |
|
3143 In computing, |
|
3144 <span class="emphasis"> |
|
3145 <em> |
|
3146 cut |
|
3147 </em> |
|
3148 </span> |
|
3149 is a Unix command line utility which is used to extract sections from each line of input usually from a file. |
|
3150 </p> |
|
3151 <p id="ch9Using_Linux_Tools_c8"> |
|
3152 Extraction of line segments can typically be done by |
|
3153 <span class="emphasis"> |
|
3154 <em> |
|
3155 bytes (-b), characters (-c) |
|
3156 </em> |
|
3157 </span> |
|
3158 , or |
|
3159 <span class="emphasis"> |
|
3160 <em> |
|
3161 fields (-f) |
|
3162 </em> |
|
3163 </span> |
|
3164 separated by a |
|
3165 <span class="emphasis"> |
|
3166 <em> |
|
3167 delimiter (-d the tab character by default) |
|
3168 </em> |
|
3169 </span> |
|
3170 . A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1. |
|
3171 </p> |
|
3172 <p id="ch9Using_Linux_Tools_c9"> |
|
3173 Assuming a file named file containing the lines: |
|
3174 </p> |
|
3175 <pre class="programlisting"> |
|
3176 foo:bar:baz:qux:quux |
726 one:two:three:four:five:six:seven |
3177 one:two:three:four:five:six:seven |
727 alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu</pre> |
3178 alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu |
728 <p><a name="ch9Using_Linux_Tools_ca"></a>To output the fourth through tenth characters of each line:</p> |
3179 </pre> |
729 <pre class="programlisting"> |
3180 <p id="ch9Using_Linux_Tools_ca"> |
730 $ cut -c 4-10 file</pre> |
3181 To output the fourth through tenth characters of each line: |
731 <p><a name="ch9Using_Linux_Tools_cb"></a>This gives the output:</p> |
3182 </p> |
732 <pre class="programlisting"> |
3183 <pre class="programlisting"> |
733 :bar:ba |
3184 $ cut -c 4-10 file |
|
3185 </pre> |
|
3186 <p id="ch9Using_Linux_Tools_cb"> |
|
3187 This gives the output: |
|
3188 </p> |
|
3189 <pre class="programlisting"> |
|
3190 :bar:ba |
734 :two:th |
3191 :two:th |
735 ha:beta</pre> |
3192 ha:beta |
736 <p><a name="ch9Using_Linux_Tools_cc"></a>To output the fifth field through the end of the line of each line using the colon character as the field delimiter:</p> |
3193 </pre> |
737 <pre class="programlisting"> |
3194 <p id="ch9Using_Linux_Tools_cc"> |
738 $ cut -d : -f 5- file</pre> |
3195 To output the fifth field through the end of the line of each line using the colon character as the field delimiter: |
739 <p><a name="ch9Using_Linux_Tools_cd"></a>This gives the output:</p> |
3196 </p> |
740 <pre class="programlisting"> |
3197 <pre class="programlisting"> |
741 quux |
3198 $ cut -d : -f 5- file |
|
3199 </pre> |
|
3200 <p id="ch9Using_Linux_Tools_cd"> |
|
3201 This gives the output: |
|
3202 </p> |
|
3203 <pre class="programlisting"> |
|
3204 quux |
742 five:six:seven |
3205 five:six:seven |
743 epsilon:zeta:eta:teta:iota:kappa:lambda:mu</pre> |
3206 epsilon:zeta:eta:teta:iota:kappa:lambda:mu |
744 </div> |
3207 </pre> |
745 <div class="section" title="7.4. paste"> |
3208 </div> |
746 <div class="titlepage"><div><div><h3 class="title"> |
3209 <div class="section" title="7.4.paste"> |
747 <a name="id2948562"></a>7.4. paste</h3></div></div></div> |
3210 <div class="titlepage"> |
748 <p><a name="ch9Using_Linux_Tools_ce"></a><span class="emphasis"><em>paste</em></span> is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to the utility <span class="emphasis"><em>cat</em></span> command which operates on the vertical plane of two or more files.</p> |
3211 <div> |
749 <p><a name="ch9Using_Linux_Tools_cf"></a>To paste several columns of data together into the file <span class="emphasis"><em>www</em></span> from files <span class="emphasis"><em>who</em></span>, <span class="emphasis"><em>where</em></span>, and <span class="emphasis"><em>when</em></span>:</p> |
3212 <div> |
750 <pre class="programlisting"> |
3213 <h3 class="title"> |
751 $ paste who where when > www</pre> |
3214 <a name="id2627231"> |
752 <p><a name="ch9Using_Linux_Tools_d0"></a>If the files contain:</p> |
3215 </a> |
753 <p><a name="ch9Using_Linux_Tools_d1"></a>who</p> |
3216 7.4.paste |
754 <p><a name="ch9Using_Linux_Tools_d2"></a>where</p> |
3217 </h3> |
755 <p><a name="ch9Using_Linux_Tools_d3"></a>when</p> |
3218 </div> |
756 <p><a name="ch9Using_Linux_Tools_d4"></a>Batman</p> |
3219 </div> |
757 <p><a name="ch9Using_Linux_Tools_d5"></a>GothamCity</p> |
3220 </div> |
758 <p><a name="ch9Using_Linux_Tools_d6"></a>January 3</p> |
3221 <p id="ch9Using_Linux_Tools_ce"> |
759 <p><a name="ch9Using_Linux_Tools_d7"></a>Trillian</p> |
3222 <span class="emphasis"> |
760 <p><a name="ch9Using_Linux_Tools_d8"></a>Andromeda</p> |
3223 <em> |
761 <p><a name="ch9Using_Linux_Tools_d9"></a>February 4</p> |
3224 paste |
762 <p><a name="ch9Using_Linux_Tools_da"></a>Jeeves</p> |
3225 </em> |
763 <p><a name="ch9Using_Linux_Tools_db"></a>London</p> |
3226 </span> |
764 <p><a name="ch9Using_Linux_Tools_dc"></a>March 19</p> |
3227 is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to the utility |
765 <p><a name="ch9Using_Linux_Tools_dd"></a>This creates the file named <span class="emphasis"><em>www</em></span> containing:</p> |
3228 <span class="emphasis"> |
766 <pre class="programlisting"> |
3229 <em> |
767 Batman GothamCity January 3 |
3230 cat |
|
3231 </em> |
|
3232 </span> |
|
3233 command which operates on the vertical plane of two or more files. |
|
3234 </p> |
|
3235 <p id="ch9Using_Linux_Tools_cf"> |
|
3236 To paste several columns of data together into the file |
|
3237 <span class="emphasis"> |
|
3238 <em> |
|
3239 www |
|
3240 </em> |
|
3241 </span> |
|
3242 from files |
|
3243 <span class="emphasis"> |
|
3244 <em> |
|
3245 who |
|
3246 </em> |
|
3247 </span> |
|
3248 , |
|
3249 <span class="emphasis"> |
|
3250 <em> |
|
3251 where |
|
3252 </em> |
|
3253 </span> |
|
3254 , and |
|
3255 <span class="emphasis"> |
|
3256 <em> |
|
3257 when |
|
3258 </em> |
|
3259 </span> |
|
3260 : |
|
3261 </p> |
|
3262 <pre class="programlisting"> |
|
3263 $ paste who where when > www |
|
3264 </pre> |
|
3265 <p id="ch9Using_Linux_Tools_d0"> |
|
3266 If the files contain: |
|
3267 </p> |
|
3268 <p id="ch9Using_Linux_Tools_d1"> |
|
3269 who |
|
3270 </p> |
|
3271 <p id="ch9Using_Linux_Tools_d2"> |
|
3272 where |
|
3273 </p> |
|
3274 <p id="ch9Using_Linux_Tools_d3"> |
|
3275 when |
|
3276 </p> |
|
3277 <p id="ch9Using_Linux_Tools_d4"> |
|
3278 Batman |
|
3279 </p> |
|
3280 <p id="ch9Using_Linux_Tools_d5"> |
|
3281 GothamCity |
|
3282 </p> |
|
3283 <p id="ch9Using_Linux_Tools_d6"> |
|
3284 January 3 |
|
3285 </p> |
|
3286 <p id="ch9Using_Linux_Tools_d7"> |
|
3287 Trillian |
|
3288 </p> |
|
3289 <p id="ch9Using_Linux_Tools_d8"> |
|
3290 Andromeda |
|
3291 </p> |
|
3292 <p id="ch9Using_Linux_Tools_d9"> |
|
3293 February 4 |
|
3294 </p> |
|
3295 <p id="ch9Using_Linux_Tools_da"> |
|
3296 Jeeves |
|
3297 </p> |
|
3298 <p id="ch9Using_Linux_Tools_db"> |
|
3299 London |
|
3300 </p> |
|
3301 <p id="ch9Using_Linux_Tools_dc"> |
|
3302 March 19 |
|
3303 </p> |
|
3304 <p id="ch9Using_Linux_Tools_dd"> |
|
3305 This creates the file named |
|
3306 <span class="emphasis"> |
|
3307 <em> |
|
3308 www |
|
3309 </em> |
|
3310 </span> |
|
3311 containing: |
|
3312 </p> |
|
3313 <pre class="programlisting"> |
|
3314 Batman GothamCity January 3 |
768 Trillian Andromeda February 4 |
3315 Trillian Andromeda February 4 |
769 Jeeves London March 19</pre> |
3316 Jeeves London March 19 |
770 </div> |
3317 </pre> |
771 </div> |
3318 </div> |
772 <div class="section" title="8. Shell Meta Characters"> |
3319 </div> |
773 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
3320 <div class="section" title="8.Shell Meta Characters"> |
774 <a name="id2948682"></a>8. Shell Meta Characters</h2></div></div></div> |
3321 <div class="titlepage"> |
775 <p><a name="ch9Using_Linux_Tools_de"></a>Unix recognizes certain special characters, called "meta characters," as command directives. The shell meta characters are recognized anywhere they appear in the command line, even if they are not surrounded by blank space. For that reason, it is safest to only use the characters A-Z, a-z, 0-9, and the period, dash, and underscore characters when naming files and directories on Unix. If your file or directory has a shell meta character in the name, you will find it difficult to use the name in a shell command.</p> |
3322 <div> |
776 <p><a name="ch9Using_Linux_Tools_df"></a>The shell meta characters include:</p> |
3323 <div> |
777 <p><a name="ch9Using_Linux_Tools_e0"></a>/ < > ! $ % ^ & * | { } [ ] " ' ` ~ ;</p> |
3324 <h2 class="title" style="clear: both"> |
778 <p><a name="ch9Using_Linux_Tools_e1"></a>Different shells may differ in the meta characters recognized.</p> |
3325 <a name="id2627351"> |
779 <p><a name="ch9Using_Linux_Tools_e2"></a>As an example,</p> |
3326 </a> |
780 <pre class="programlisting"> |
3327 8.Shell Meta Characters |
781 $ ls file.*</pre> |
3328 </h2> |
782 <p><a name="ch9Using_Linux_Tools_e3"></a>run on a directory containing the files file, file.c, file.lst, and myfile would list the files file.c and file.lst. However,:</p> |
3329 </div> |
783 <pre class="programlisting"> |
3330 </div> |
784 $ ls file.?</pre> |
3331 </div> |
785 <p><a name="ch9Using_Linux_Tools_e4"></a>run on the same directory would only list file.c because the ? only matches one character, no more, no less. This can save you a great deal of typing time. For example, if there is a file called california_cornish_hens_with_wild_rice and no other files whose names begin with 'c', you could view the file without typing the whole name by typing this:</p> |
3332 <p id="ch9Using_Linux_Tools_de"> |
786 <pre class="programlisting"> |
3333 Unix recognizes certain special characters, called "meta characters," as command directives. The shell meta characters are recognized anywhere they appear in the command line, even if they are not surrounded by blank space. For that reason, it is safest to only use the characters A-Z, a-z, 0-9, and the period, dash, and underscore characters when naming files and directories on Unix. If your file or directory has a shell meta character in the name, you will find it difficult to use the name in a shell command. |
787 $ more c*</pre> |
3334 </p> |
788 <p><a name="ch9Using_Linux_Tools_e5"></a>because the c* matches that long file name.</p> |
3335 <p id="ch9Using_Linux_Tools_df"> |
789 <p><a name="ch9Using_Linux_Tools_e6"></a>Filenames containing metacharacters can pose many problems and should never be intentionally created. If you do find that you've created a file with metacharacters, and you would like to remove it, you have three options. You may use wildcards to match metacharacter, use the to directly enter the filename, or put the command in double quotes (except in the case of double quotes within the file name, these must be captured with one of the first two methods). For example, deleting a file named "``*`|more"` can be accomplished with:</p> |
3336 The shell meta characters include: |
790 <pre class="programlisting"> |
3337 </p> |
791 $ rm ??more</pre> |
3338 <p id="ch9Using_Linux_Tools_e0"> |
792 <p><a name="ch9Using_Linux_Tools_e7"></a>or:</p> |
3339 / < > ! $ % ^ & * | { } [ ] " ' ` ~ ; |
793 <pre class="programlisting"> |
3340 </p> |
794 $ rm $ backslash$*$ backslash$|more</pre> |
3341 <p id="ch9Using_Linux_Tools_e1"> |
795 <p><a name="ch9Using_Linux_Tools_e8"></a>or:</p> |
3342 Different shells may differ in the meta characters recognized. |
796 <pre class="programlisting"> |
3343 </p> |
797 $ rm ''*|more''</pre> |
3344 <p id="ch9Using_Linux_Tools_e2"> |
798 </div> |
3345 As an example, |
799 <div class="section" title="9. Looking At Files"> |
3346 </p> |
800 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
3347 <pre class="programlisting"> |
801 <a name="id2948789"></a>9. Looking At Files</h2></div></div></div> |
3348 $ ls file.* |
802 <div class="section" title="9.1. cat"> |
3349 </pre> |
803 <div class="titlepage"><div><div><h3 class="title"> |
3350 <p id="ch9Using_Linux_Tools_e3"> |
804 <a name="id2948797"></a>9.1. cat</h3></div></div></div> |
3351 run on a directory containing the files file, file.c, file.lst, and myfile would list the files file.c and file.lst. However,: |
805 <p><a name="ch9Using_Linux_Tools_e9"></a>The <span class="emphasis"><em>cat</em></span> command is a standard Unix program used to concatenate and display files. The name is from "catenate", a synonym of <span class="emphasis"><em>concatenate</em></span>.</p> |
3352 </p> |
806 <p><a name="ch9Using_Linux_Tools_ea"></a>The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read.</p> |
3353 <pre class="programlisting"> |
807 <p><a name="ch9Using_Linux_Tools_eb"></a>If the filename is specified as -, then <span class="emphasis"><em>cat</em></span> will read from standard input at that point in the sequence. If no files are specified, <span class="emphasis"><em>cat</em></span> will read from standard input entered.</p> |
3354 $ ls file.? |
808 <div class="section" title="9.1.1. Jargon File Definition"> |
3355 </pre> |
809 <div class="titlepage"><div><div><h4 class="title"> |
3356 <p id="ch9Using_Linux_Tools_e4"> |
810 <a name="id2948841"></a>9.1.1. Jargon File Definition</h4></div></div></div> |
3357 run on the same directory would only list file.c because the ? only matches one character, no more, no less. This can save you a great deal of typing time. For example, if there is a file called california_cornish_hens_with_wild_rice and no other files whose names begin with 'c', you could view the file without typing the whole name by typing this: |
811 <p><a name="ch9Using_Linux_Tools_ec"></a>The Jargon File version 4.4.7 lists this as the definition of <span class="emphasis"><em>cat</em></span>:</p> |
3358 </p> |
812 <pre class="programlisting"> |
3359 <pre class="programlisting"> |
813 1. To spew an entire file to the screen or some other output sink without |
3360 $ more c* |
|
3361 </pre> |
|
3362 <p id="ch9Using_Linux_Tools_e5"> |
|
3363 because the c* matches that long file name. |
|
3364 </p> |
|
3365 <p id="ch9Using_Linux_Tools_e6"> |
|
3366 Filenames containing metacharacters can pose many problems and should never be intentionally created. If you do find that you've created a file with metacharacters, and you would like to remove it, you have three options. You may use wildcards to match metacharacter, use the to directly enter the filename, or put the command in double quotes (except in the case of double quotes within the file name, these must be captured with one of the first two methods). For example, deleting a file named "``*`|more"` can be accomplished with: |
|
3367 </p> |
|
3368 <pre class="programlisting"> |
|
3369 $ rm ??more |
|
3370 </pre> |
|
3371 <p id="ch9Using_Linux_Tools_e7"> |
|
3372 or: |
|
3373 </p> |
|
3374 <pre class="programlisting"> |
|
3375 $ rm $ backslash$*$ backslash$|more |
|
3376 </pre> |
|
3377 <p id="ch9Using_Linux_Tools_e8"> |
|
3378 or: |
|
3379 </p> |
|
3380 <pre class="programlisting"> |
|
3381 $ rm ''*|more'' |
|
3382 </pre> |
|
3383 </div> |
|
3384 <div class="section" title="9.Looking At Files"> |
|
3385 <div class="titlepage"> |
|
3386 <div> |
|
3387 <div> |
|
3388 <h2 class="title" style="clear: both"> |
|
3389 <a name="id2627457"> |
|
3390 </a> |
|
3391 9.Looking At Files |
|
3392 </h2> |
|
3393 </div> |
|
3394 </div> |
|
3395 </div> |
|
3396 <div class="section" title="9.1.cat"> |
|
3397 <div class="titlepage"> |
|
3398 <div> |
|
3399 <div> |
|
3400 <h3 class="title"> |
|
3401 <a name="id2627466"> |
|
3402 </a> |
|
3403 9.1.cat |
|
3404 </h3> |
|
3405 </div> |
|
3406 </div> |
|
3407 </div> |
|
3408 <p id="ch9Using_Linux_Tools_e9"> |
|
3409 The |
|
3410 <span class="emphasis"> |
|
3411 <em> |
|
3412 cat |
|
3413 </em> |
|
3414 </span> |
|
3415 command is a standard Unix program used to concatenate and display files. The name is from "catenate", a synonym of |
|
3416 <span class="emphasis"> |
|
3417 <em> |
|
3418 concatenate |
|
3419 </em> |
|
3420 </span> |
|
3421 . |
|
3422 </p> |
|
3423 <p id="ch9Using_Linux_Tools_ea"> |
|
3424 The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read. |
|
3425 </p> |
|
3426 <p id="ch9Using_Linux_Tools_eb"> |
|
3427 If the filename is specified as -, then |
|
3428 <span class="emphasis"> |
|
3429 <em> |
|
3430 cat |
|
3431 </em> |
|
3432 </span> |
|
3433 will read from standard input at that point in the sequence. If no files are specified, |
|
3434 <span class="emphasis"> |
|
3435 <em> |
|
3436 cat |
|
3437 </em> |
|
3438 </span> |
|
3439 will read from standard input entered. |
|
3440 </p> |
|
3441 <div class="section" title="9.1.1.Jargon File Definition"> |
|
3442 <div class="titlepage"> |
|
3443 <div> |
|
3444 <div> |
|
3445 <h4 class="title"> |
|
3446 <a name="id2627510"> |
|
3447 </a> |
|
3448 9.1.1.Jargon File Definition |
|
3449 </h4> |
|
3450 </div> |
|
3451 </div> |
|
3452 </div> |
|
3453 <p id="ch9Using_Linux_Tools_ec"> |
|
3454 The Jargon File version 4.4.7 lists this as the definition of |
|
3455 <span class="emphasis"> |
|
3456 <em> |
|
3457 cat |
|
3458 </em> |
|
3459 </span> |
|
3460 : |
|
3461 </p> |
|
3462 <pre class="programlisting"> |
|
3463 1. To spew an entire file to the screen or some other output sink without |
814 pause (syn. blast). |
3464 pause (syn. blast). |
815 |
3465 |
816 2. By extension, to dump large amounts of data at an unprepared target or |
3466 2. By extension, to dump large amounts of data at an unprepared target or |
817 with no intention of browsing it carefully. Usage: considered silly. |
3467 with no intention of browsing it carefully. Usage: considered silly. |
818 Rare outside Unix sites. See also dd, BLT. |
3468 Rare outside Unix sites. See also dd, BLT. |
827 bad user-interface design, because of its woefully unobvious name. |
3477 bad user-interface design, because of its woefully unobvious name. |
828 It is far more often used to blast a single file to standard output |
3478 It is far more often used to blast a single file to standard output |
829 than to concatenate two or more files. The name cat for the former |
3479 than to concatenate two or more files. The name cat for the former |
830 operation is just as unintuitive as, say, LISP's cdr. |
3480 operation is just as unintuitive as, say, LISP's cdr. |
831 |
3481 |
832 Of such oppositions are holy wars made...</pre> |
3482 Of such oppositions are holy wars made... |
833 </div> |
3483 </pre> |
834 <div class="section" title="9.1.2. Useless Use of 'cat'"> |
3484 </div> |
835 <div class="titlepage"><div><div><h4 class="title"> |
3485 <div class="section" title="9.1.2.Useless Use of 'cat'"> |
836 <a name="id2948880"></a>9.1.2. Useless Use of 'cat'</h4></div></div></div> |
3486 <div class="titlepage"> |
837 <p><a name="ch9Using_Linux_Tools_ed"></a>UUOC (from comp.unix.shell on Usenet) stands for “Useless Use of cat”. As it is observed on <span class="emphasis"><em>comp.unix.shell</em></span>, “The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process.”</p> |
3487 <div> |
838 <p><a name="ch9Using_Linux_Tools_ee"></a>Nevertheless one sees people doing:</p> |
3488 <div> |
839 <pre class="programlisting"> |
3489 <h4 class="title"> |
840 $ cat file | some_command and its args ...</pre> |
3490 <a name="id2627549"> |
841 <p><a name="ch9Using_Linux_Tools_ef"></a>instead of the equivalent and cheaper:</p> |
3491 </a> |
842 <pre class="programlisting"> |
3492 9.1.2.Useless Use of 'cat' |
843 <file some_command and its args ...</pre> |
3493 </h4> |
844 <p><a name="ch9Using_Linux_Tools_f0"></a>or (equivalently and more classically):</p> |
3494 </div> |
845 <pre class="programlisting"> |
3495 </div> |
846 some_command and its args ... <file</pre> |
3496 </div> |
847 <p><a name="ch9Using_Linux_Tools_f1"></a>Since 1995, occasional awards for UUOC have been given out. The activity of fixing instances of UUOC is sometimes called 'demoggification'.</p> |
3497 <p id="ch9Using_Linux_Tools_ed"> |
848 <p><a name="ch9Using_Linux_Tools_f2"></a>Amongst many, it is still considered safer to use <span class="emphasis"><em>cat</em></span> for such cases given that the < and > keys are next to each other in many popular keyboard mappings. While the risk might be low, the impact of using > instead of < can be high and prohibitive.</p> |
3498 UUOC (from comp.unix.shell on Usenet) stands for Useless Use of cat. As it is observed on |
849 </div> |
3499 <span class="emphasis"> |
850 <div class="section" title="9.1.3. zcat"> |
3500 <em> |
851 <div class="titlepage"><div><div><h4 class="title"> |
3501 comp.unix.shell |
852 <a name="id2948951"></a>9.1.3. zcat</h4></div></div></div> |
3502 </em> |
853 <p><a name="ch9Using_Linux_Tools_f3"></a><span class="emphasis"><em>zcat</em></span> is a Unix program similar to <span class="emphasis"><em>cat</em></span>, that decompresses individual files and concatenates them to standard output. Traditionally <span class="emphasis"><em>zcat</em></span> operated on files compressed by compress but today it is usually able to operate on <span class="emphasis"><em>gzip</em></span> or even <span class="emphasis"><em>bzip2</em></span> archives. On such systems, it is equivalent to <span class="emphasis"><em>gunzip -c</em></span></p> |
3503 </span> |
854 </div> |
3504 , The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process. |
855 </div> |
3505 </p> |
856 <div class="section" title="9.2. more"> |
3506 <p id="ch9Using_Linux_Tools_ee"> |
857 <div class="titlepage"><div><div><h3 class="title"> |
3507 Nevertheless one sees people doing: |
858 <a name="id2948985"></a>9.2. more</h3></div></div></div> |
3508 </p> |
859 <p><a name="ch9Using_Linux_Tools_f4"></a>In computing, <span class="emphasis"><em>more</em></span> is a command to view (but not modify) the contents of a text file one screen at a time (terminal pager). It is available on Unix and Unix-like systems, DOS, OS/2 and Microsoft Windows. Programs of this sort are called pagers.</p> |
3509 <pre class="programlisting"> |
860 <div class="section" title="9.2.1. Usage"> |
3510 $ cat file | some_command and its args ... |
861 <div class="titlepage"><div><div><h4 class="title"> |
3511 </pre> |
862 <a name="id2949004"></a>9.2.1. Usage</h4></div></div></div> |
3512 <p id="ch9Using_Linux_Tools_ef"> |
863 <p><a name="ch9Using_Linux_Tools_f5"></a>The command-syntax is:</p> |
3513 instead of the equivalent and cheaper: |
864 <pre class="programlisting"> |
3514 </p> |
865 $ more [options] [file_name]</pre> |
3515 <pre class="programlisting"> |
866 <p><a name="ch9Using_Linux_Tools_f6"></a>If no file name is provided, <span class="emphasis"><em>more</em></span> looks for input from stdin.</p> |
3516 <file some_command and its args ... |
867 <p><a name="ch9Using_Linux_Tools_f7"></a>Once <span class="emphasis"><em>more</em></span> has obtained input, it displays as much as can fit on the current screen and waits for user input to advance, with the exception that a form feed (^L) will also cause <span class="emphasis"><em>more</em></span> to wait at that line, regardless of the amount of text on the screen. In the lower-left corner of the screen is displayed the text "--More--" and a percentage, representing the percent of the file that <span class="emphasis"><em>more</em></span> has paged through. (This percentage includes the text displayed on the current screen.) When <span class="emphasis"><em>more</em></span> reaches the end of a file (100%) it exits. The most common methods of navigating through a file are <span class="emphasis"><em>Enter</em></span>, which advances the output by one line, and <span class="emphasis"><em>Space</em></span>, which advances the output by one screen.</p> |
3517 </pre> |
868 <p><a name="ch9Using_Linux_Tools_f8"></a>There are also other commands that can be used while navigating through the document; consult <span class="emphasis"><em>more</em></span>'s <span class="emphasis"><em>man</em></span> page for more details.</p> |
3518 <p id="ch9Using_Linux_Tools_f0"> |
869 <p><a name="ch9Using_Linux_Tools_f9"></a><span class="emphasis"><em>Options</em></span> are typically entered before the file name, but can also be entered in the environment variable <span class="emphasis"><em>$MORE</em></span>. Options entered in the actual command line will override those entered in the <span class="emphasis"><em>$MORE</em></span> environment variable. Available options may vary between Unix systems.</p> |
3519 or (equivalently and more classically): |
870 </div> |
3520 </p> |
871 </div> |
3521 <pre class="programlisting"> |
872 <div class="section" title="9.3. less"> |
3522 some_command and its args ... <file |
873 <div class="titlepage"><div><div><h3 class="title"> |
3523 </pre> |
874 <a name="id2949096"></a>9.3. less</h3></div></div></div> |
3524 <p id="ch9Using_Linux_Tools_f1"> |
875 <p><a name="ch9Using_Linux_Tools_fa"></a><span class="emphasis"><em>less</em></span> is a terminal pager program on Unix, Windows and Unix-like systems used to view (but not change) the contents of a text file one screen at a time. It is similar to <span class="emphasis"><em>more</em></span>, but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, <span class="emphasis"><em>less</em></span> does not need to read the entire file before starting, resulting in faster load times with large files.</p> |
3525 Since 1995, occasional awards for UUOC have been given out. The activity of fixing instances of UUOC is sometimes called 'demoggification'. |
876 <div class="section" title="9.3.1. Usage"> |
3526 </p> |
877 <div class="titlepage"><div><div><h4 class="title"> |
3527 <p id="ch9Using_Linux_Tools_f2"> |
878 <a name="id2949125"></a>9.3.1. Usage</h4></div></div></div> |
3528 Amongst many, it is still considered safer to use |
879 <p><a name="ch9Using_Linux_Tools_fb"></a><span class="emphasis"><em>less</em></span> can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While <span class="emphasis"><em>less</em></span> is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both <span class="emphasis"><em>more</em></span> and <span class="emphasis"><em>vi</em></span>. It is also possible to search for character patterns in the file.</p> |
3529 <span class="emphasis"> |
880 <p><a name="ch9Using_Linux_Tools_fc"></a>By default, <span class="emphasis"><em>less</em></span> displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat.</p> |
3530 <em> |
881 <p><a name="ch9Using_Linux_Tools_fd"></a>The command-syntax is:</p> |
3531 cat |
882 <pre class="programlisting"> |
3532 </em> |
883 $ less [options] file_name</pre> |
3533 </span> |
884 </div> |
3534 for such cases given that the < and > keys are next to each other in many popular keyboard mappings. While the risk might be low, the impact of using > instead of < can be high and prohibitive. |
885 <div class="section" title="9.3.2. Frequently Used Options"> |
3535 </p> |
886 <div class="titlepage"><div><div><h4 class="title"> |
3536 </div> |
887 <a name="id2949180"></a>9.3.2. Frequently Used Options</h4></div></div></div> |
3537 <div class="section" title="9.1.3.zcat"> |
888 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
3538 <div class="titlepage"> |
889 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_fe"></a>-g: Highlights just the current match of any searched string.</p></li> |
3539 <div> |
890 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_ff"></a>-I: Case-insensitive searches.</p></li> |
3540 <div> |
891 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_100"></a>-M: Shows more detailed prompt, including file position.</p></li> |
3541 <h4 class="title"> |
892 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_101"></a>-N: Shows line numbers (useful for source code viewing).</p></li> |
3542 <a name="id2627620"> |
893 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_102"></a>-S: Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling.</p></li> |
3543 </a> |
894 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_103"></a>-?: Shows help.</p></li> |
3544 9.1.3.zcat |
895 </ul></div> |
3545 </h4> |
896 </div> |
3546 </div> |
897 <div class="section" title="9.3.3. Frequently Used Commands"> |
3547 </div> |
898 <div class="titlepage"><div><div><h4 class="title"> |
3548 </div> |
899 <a name="id2949232"></a>9.3.3. Frequently Used Commands</h4></div></div></div> |
3549 <p id="ch9Using_Linux_Tools_f3"> |
900 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
3550 <span class="emphasis"> |
901 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_104"></a>[Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.</p></li> |
3551 <em> |
902 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_105"></a>[Space bar]: Next page.</p></li> |
3552 zcat |
903 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_106"></a>b: Previous page.</p></li> |
3553 </em> |
904 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_107"></a>ng: Jump to line number n. Default is the start of the file.</p></li> |
3554 </span> |
905 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_108"></a>nG: Jump to line number n. Default is the end of the file.</p></li> |
3555 is a Unix program similar to |
906 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_109"></a>/pattern: Search for pattern. Regular expressions can be used.</p></li> |
3556 <span class="emphasis"> |
907 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10a"></a>'^ or g: Go to start of file.</p></li> |
3557 <em> |
908 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10b"></a>'$ or G: Go to end of file.</p></li> |
3558 cat |
909 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10c"></a>s: Save current content (got from another program like grep) in a file.</p></li> |
3559 </em> |
910 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10d"></a>=: File information.</p></li> |
3560 </span> |
911 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10e"></a>h: Help.</p></li> |
3561 , that decompresses individual files and concatenates them to standard output. Traditionally |
912 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_10f"></a>q: Quit.</p></li> |
3562 <span class="emphasis"> |
913 </ul></div> |
3563 <em> |
914 </div> |
3564 zcat |
915 <div class="section" title="9.3.4. Examples"> |
3565 </em> |
916 <div class="titlepage"><div><div><h4 class="title"> |
3566 </span> |
917 <a name="id2949322"></a>9.3.4. Examples</h4></div></div></div> |
3567 operated on files compressed by compress but today it is usually able to operate on |
918 <pre class="programlisting"> |
3568 <span class="emphasis"> |
919 $ less -M readme.txt #Read "readme.txt." |
3569 <em> |
|
3570 gzip |
|
3571 </em> |
|
3572 </span> |
|
3573 or even |
|
3574 <span class="emphasis"> |
|
3575 <em> |
|
3576 bzip2 |
|
3577 </em> |
|
3578 </span> |
|
3579 archives. On such systems, it is equivalent to |
|
3580 <span class="emphasis"> |
|
3581 <em> |
|
3582 gunzip -c |
|
3583 </em> |
|
3584 </span> |
|
3585 </p> |
|
3586 </div> |
|
3587 </div> |
|
3588 <div class="section" title="9.2.more"> |
|
3589 <div class="titlepage"> |
|
3590 <div> |
|
3591 <div> |
|
3592 <h3 class="title"> |
|
3593 <a name="id2627654"> |
|
3594 </a> |
|
3595 9.2.more |
|
3596 </h3> |
|
3597 </div> |
|
3598 </div> |
|
3599 </div> |
|
3600 <p id="ch9Using_Linux_Tools_f4"> |
|
3601 In computing, |
|
3602 <span class="emphasis"> |
|
3603 <em> |
|
3604 more |
|
3605 </em> |
|
3606 </span> |
|
3607 is a command to view (but not modify) the contents of a text file one screen at a time (terminal pager). It is available on Unix and Unix-like systems, DOS, OS/2 and Microsoft Windows. Programs of this sort are called pagers. |
|
3608 </p> |
|
3609 <div class="section" title="9.2.1.Usage"> |
|
3610 <div class="titlepage"> |
|
3611 <div> |
|
3612 <div> |
|
3613 <h4 class="title"> |
|
3614 <a name="id2627673"> |
|
3615 </a> |
|
3616 9.2.1.Usage |
|
3617 </h4> |
|
3618 </div> |
|
3619 </div> |
|
3620 </div> |
|
3621 <p id="ch9Using_Linux_Tools_f5"> |
|
3622 The command-syntax is: |
|
3623 </p> |
|
3624 <pre class="programlisting"> |
|
3625 $ more [options] [file_name] |
|
3626 </pre> |
|
3627 <p id="ch9Using_Linux_Tools_f6"> |
|
3628 If no file name is provided, |
|
3629 <span class="emphasis"> |
|
3630 <em> |
|
3631 more |
|
3632 </em> |
|
3633 </span> |
|
3634 looks for input from stdin. |
|
3635 </p> |
|
3636 <p id="ch9Using_Linux_Tools_f7"> |
|
3637 Once |
|
3638 <span class="emphasis"> |
|
3639 <em> |
|
3640 more |
|
3641 </em> |
|
3642 </span> |
|
3643 has obtained input, it displays as much as can fit on the current screen and waits for user input to advance, with the exception that a form feed (^L) will also cause |
|
3644 <span class="emphasis"> |
|
3645 <em> |
|
3646 more |
|
3647 </em> |
|
3648 </span> |
|
3649 to wait at that line, regardless of the amount of text on the screen. In the lower-left corner of the screen is displayed the text "--More--" and a percentage, representing the percent of the file that |
|
3650 <span class="emphasis"> |
|
3651 <em> |
|
3652 more |
|
3653 </em> |
|
3654 </span> |
|
3655 has paged through. (This percentage includes the text displayed on the current screen.) When |
|
3656 <span class="emphasis"> |
|
3657 <em> |
|
3658 more |
|
3659 </em> |
|
3660 </span> |
|
3661 reaches the end of a file (100%) it exits. The most common methods of navigating through a file are |
|
3662 <span class="emphasis"> |
|
3663 <em> |
|
3664 Enter |
|
3665 </em> |
|
3666 </span> |
|
3667 , which advances the output by one line, and |
|
3668 <span class="emphasis"> |
|
3669 <em> |
|
3670 Space |
|
3671 </em> |
|
3672 </span> |
|
3673 , which advances the output by one screen. |
|
3674 </p> |
|
3675 <p id="ch9Using_Linux_Tools_f8"> |
|
3676 There are also other commands that can be used while navigating through the document; consult |
|
3677 <span class="emphasis"> |
|
3678 <em> |
|
3679 more |
|
3680 </em> |
|
3681 </span> |
|
3682 's |
|
3683 <span class="emphasis"> |
|
3684 <em> |
|
3685 man |
|
3686 </em> |
|
3687 </span> |
|
3688 page for more details. |
|
3689 </p> |
|
3690 <p id="ch9Using_Linux_Tools_f9"> |
|
3691 <span class="emphasis"> |
|
3692 <em> |
|
3693 Options |
|
3694 </em> |
|
3695 </span> |
|
3696 are typically entered before the file name, but can also be entered in the environment variable |
|
3697 <span class="emphasis"> |
|
3698 <em> |
|
3699 $MORE |
|
3700 </em> |
|
3701 </span> |
|
3702 . Options entered in the actual command line will override those entered in the |
|
3703 <span class="emphasis"> |
|
3704 <em> |
|
3705 $MORE |
|
3706 </em> |
|
3707 </span> |
|
3708 environment variable. Available options may vary between Unix systems. |
|
3709 </p> |
|
3710 </div> |
|
3711 </div> |
|
3712 <div class="section" title="9.3.less"> |
|
3713 <div class="titlepage"> |
|
3714 <div> |
|
3715 <div> |
|
3716 <h3 class="title"> |
|
3717 <a name="id2627765"> |
|
3718 </a> |
|
3719 9.3.less |
|
3720 </h3> |
|
3721 </div> |
|
3722 </div> |
|
3723 </div> |
|
3724 <p id="ch9Using_Linux_Tools_fa"> |
|
3725 <span class="emphasis"> |
|
3726 <em> |
|
3727 less |
|
3728 </em> |
|
3729 </span> |
|
3730 is a terminal pager program on Unix, Windows and Unix-like systems used to view (but not change) the contents of a text file one screen at a time. It is similar to |
|
3731 <span class="emphasis"> |
|
3732 <em> |
|
3733 more |
|
3734 </em> |
|
3735 </span> |
|
3736 , but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, |
|
3737 <span class="emphasis"> |
|
3738 <em> |
|
3739 less |
|
3740 </em> |
|
3741 </span> |
|
3742 does not need to read the entire file before starting, resulting in faster load times with large files. |
|
3743 </p> |
|
3744 <div class="section" title="9.3.1.Usage"> |
|
3745 <div class="titlepage"> |
|
3746 <div> |
|
3747 <div> |
|
3748 <h4 class="title"> |
|
3749 <a name="id2627794"> |
|
3750 </a> |
|
3751 9.3.1.Usage |
|
3752 </h4> |
|
3753 </div> |
|
3754 </div> |
|
3755 </div> |
|
3756 <p id="ch9Using_Linux_Tools_fb"> |
|
3757 <span class="emphasis"> |
|
3758 <em> |
|
3759 less |
|
3760 </em> |
|
3761 </span> |
|
3762 can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While |
|
3763 <span class="emphasis"> |
|
3764 <em> |
|
3765 less |
|
3766 </em> |
|
3767 </span> |
|
3768 is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both |
|
3769 <span class="emphasis"> |
|
3770 <em> |
|
3771 more |
|
3772 </em> |
|
3773 </span> |
|
3774 and |
|
3775 <span class="emphasis"> |
|
3776 <em> |
|
3777 vi |
|
3778 </em> |
|
3779 </span> |
|
3780 . It is also possible to search for character patterns in the file. |
|
3781 </p> |
|
3782 <p id="ch9Using_Linux_Tools_fc"> |
|
3783 By default, |
|
3784 <span class="emphasis"> |
|
3785 <em> |
|
3786 less |
|
3787 </em> |
|
3788 </span> |
|
3789 displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat. |
|
3790 </p> |
|
3791 <p id="ch9Using_Linux_Tools_fd"> |
|
3792 The command-syntax is: |
|
3793 </p> |
|
3794 <pre class="programlisting"> |
|
3795 $ less [options] file_name |
|
3796 </pre> |
|
3797 </div> |
|
3798 <div class="section" title="9.3.2.Frequently Used Options"> |
|
3799 <div class="titlepage"> |
|
3800 <div> |
|
3801 <div> |
|
3802 <h4 class="title"> |
|
3803 <a name="id2627848"> |
|
3804 </a> |
|
3805 9.3.2.Frequently Used Options |
|
3806 </h4> |
|
3807 </div> |
|
3808 </div> |
|
3809 </div> |
|
3810 <div class="itemizedlist"> |
|
3811 <ul class="itemizedlist" type="*"> |
|
3812 <li class="listitem" style="list-style-type: *"> |
|
3813 <p id="ch9Using_Linux_Tools_fe"> |
|
3814 -g: Highlights just the current match of any searched string. |
|
3815 </p> |
|
3816 </li> |
|
3817 <li class="listitem" style="list-style-type: *"> |
|
3818 <p id="ch9Using_Linux_Tools_ff"> |
|
3819 -I: Case-insensitive searches. |
|
3820 </p> |
|
3821 </li> |
|
3822 <li class="listitem" style="list-style-type: *"> |
|
3823 <p id="ch9Using_Linux_Tools_100"> |
|
3824 -M: Shows more detailed prompt, including file position. |
|
3825 </p> |
|
3826 </li> |
|
3827 <li class="listitem" style="list-style-type: *"> |
|
3828 <p id="ch9Using_Linux_Tools_101"> |
|
3829 -N: Shows line numbers (useful for source code viewing). |
|
3830 </p> |
|
3831 </li> |
|
3832 <li class="listitem" style="list-style-type: *"> |
|
3833 <p id="ch9Using_Linux_Tools_102"> |
|
3834 -S: Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling. |
|
3835 </p> |
|
3836 </li> |
|
3837 <li class="listitem" style="list-style-type: *"> |
|
3838 <p id="ch9Using_Linux_Tools_103"> |
|
3839 -?: Shows help. |
|
3840 </p> |
|
3841 </li> |
|
3842 </ul> |
|
3843 </div> |
|
3844 </div> |
|
3845 <div class="section" title="9.3.3.Frequently Used Commands"> |
|
3846 <div class="titlepage"> |
|
3847 <div> |
|
3848 <div> |
|
3849 <h4 class="title"> |
|
3850 <a name="id2627901"> |
|
3851 </a> |
|
3852 9.3.3.Frequently Used Commands |
|
3853 </h4> |
|
3854 </div> |
|
3855 </div> |
|
3856 </div> |
|
3857 <div class="itemizedlist"> |
|
3858 <ul class="itemizedlist" type="*"> |
|
3859 <li class="listitem" style="list-style-type: *"> |
|
3860 <p id="ch9Using_Linux_Tools_104"> |
|
3861 [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation. |
|
3862 </p> |
|
3863 </li> |
|
3864 <li class="listitem" style="list-style-type: *"> |
|
3865 <p id="ch9Using_Linux_Tools_105"> |
|
3866 [Space bar]: Next page. |
|
3867 </p> |
|
3868 </li> |
|
3869 <li class="listitem" style="list-style-type: *"> |
|
3870 <p id="ch9Using_Linux_Tools_106"> |
|
3871 b: Previous page. |
|
3872 </p> |
|
3873 </li> |
|
3874 <li class="listitem" style="list-style-type: *"> |
|
3875 <p id="ch9Using_Linux_Tools_107"> |
|
3876 ng: Jump to line number n. Default is the start of the file. |
|
3877 </p> |
|
3878 </li> |
|
3879 <li class="listitem" style="list-style-type: *"> |
|
3880 <p id="ch9Using_Linux_Tools_108"> |
|
3881 nG: Jump to line number n. Default is the end of the file. |
|
3882 </p> |
|
3883 </li> |
|
3884 <li class="listitem" style="list-style-type: *"> |
|
3885 <p id="ch9Using_Linux_Tools_109"> |
|
3886 /pattern: Search for pattern. Regular expressions can be used. |
|
3887 </p> |
|
3888 </li> |
|
3889 <li class="listitem" style="list-style-type: *"> |
|
3890 <p id="ch9Using_Linux_Tools_10a"> |
|
3891 '^ or g: Go to start of file. |
|
3892 </p> |
|
3893 </li> |
|
3894 <li class="listitem" style="list-style-type: *"> |
|
3895 <p id="ch9Using_Linux_Tools_10b"> |
|
3896 '$ or G: Go to end of file. |
|
3897 </p> |
|
3898 </li> |
|
3899 <li class="listitem" style="list-style-type: *"> |
|
3900 <p id="ch9Using_Linux_Tools_10c"> |
|
3901 s: Save current content (got from another program like grep) in a file. |
|
3902 </p> |
|
3903 </li> |
|
3904 <li class="listitem" style="list-style-type: *"> |
|
3905 <p id="ch9Using_Linux_Tools_10d"> |
|
3906 =: File information. |
|
3907 </p> |
|
3908 </li> |
|
3909 <li class="listitem" style="list-style-type: *"> |
|
3910 <p id="ch9Using_Linux_Tools_10e"> |
|
3911 h: Help. |
|
3912 </p> |
|
3913 </li> |
|
3914 <li class="listitem" style="list-style-type: *"> |
|
3915 <p id="ch9Using_Linux_Tools_10f"> |
|
3916 q: Quit. |
|
3917 </p> |
|
3918 </li> |
|
3919 </ul> |
|
3920 </div> |
|
3921 </div> |
|
3922 <div class="section" title="9.3.4.Examples"> |
|
3923 <div class="titlepage"> |
|
3924 <div> |
|
3925 <div> |
|
3926 <h4 class="title"> |
|
3927 <a name="id2627990"> |
|
3928 </a> |
|
3929 9.3.4.Examples |
|
3930 </h4> |
|
3931 </div> |
|
3932 </div> |
|
3933 </div> |
|
3934 <pre class="programlisting"> |
|
3935 $ less -M readme.txt #Read "readme.txt." |
920 $ less +F /var/log/mail.log #Follow mode for log |
3936 $ less +F /var/log/mail.log #Follow mode for log |
921 $ file * | less #Easier file analysis. |
3937 $ file * | less #Easier file analysis. |
922 $ grep -i void *.c | less -I -p void #Case insensitive search for "void" in all .c files</pre> |
3938 $ grep -i void *.c | less -I -p void #Case insensitive search for "void" in all .c files |
923 </div> |
3939 </pre> |
924 </div> |
3940 </div> |
925 </div> |
3941 </div> |
926 <div class="section" title="10. Directory Structure"> |
3942 </div> |
927 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
3943 <div class="section" title="10.Directory Structure"> |
928 <a name="id2949337"></a>10. Directory Structure</h2></div></div></div> |
3944 <div class="titlepage"> |
929 <p><a name="ch9Using_Linux_Tools_110"></a>In the File Hierarchy Standard (FHS) all files and directories appear under the root directory "/", even if they are stored on different physical devices. Note however that some of these directories may or may not be present on a Unix system depending on whether certain subsystems, such as the X Window System, are installed.</p> |
3945 <div> |
930 <p><a name="ch9Using_Linux_Tools_111"></a>The majority of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other than Linux.</p> |
3946 <div> |
931 <p><a name="ch9Using_Linux_Tools_112"></a>Directory</p> |
3947 <h2 class="title" style="clear: both"> |
932 <p><a name="ch9Using_Linux_Tools_113"></a>Description</p> |
3948 <a name="id2628006"> |
933 <p><a name="ch9Using_Linux_Tools_114"></a>/</p> |
3949 </a> |
934 <p><a name="ch9Using_Linux_Tools_115"></a>Primary hierarchy root and root directory of |
3950 10.Directory Structure |
935 the entire file system hierarchy.</p> |
3951 </h2> |
936 <p><a name="ch9Using_Linux_Tools_116"></a>/bin/</p> |
3952 </div> |
937 <p><a name="ch9Using_Linux_Tools_117"></a>Essential command binaries that need to be |
3953 </div> |
|
3954 </div> |
|
3955 <p id="ch9Using_Linux_Tools_110"> |
|
3956 In the File Hierarchy Standard (FHS) all files and directories appear under the root directory "/", even if they are stored on different physical devices. Note however that some of these directories may or may not be present on a Unix system depending on whether certain subsystems, such as the X Window System, are installed. |
|
3957 </p> |
|
3958 <p id="ch9Using_Linux_Tools_111"> |
|
3959 The majority of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other than Linux. |
|
3960 </p> |
|
3961 <p id="ch9Using_Linux_Tools_112"> |
|
3962 Directory |
|
3963 </p> |
|
3964 <p id="ch9Using_Linux_Tools_113"> |
|
3965 Description |
|
3966 </p> |
|
3967 <p id="ch9Using_Linux_Tools_114"> |
|
3968 / |
|
3969 </p> |
|
3970 <p id="ch9Using_Linux_Tools_115"> |
|
3971 Primary hierarchy root and root directory of |
|
3972 the entire file system hierarchy. |
|
3973 </p> |
|
3974 <p id="ch9Using_Linux_Tools_116"> |
|
3975 /bin/ |
|
3976 </p> |
|
3977 <p id="ch9Using_Linux_Tools_117"> |
|
3978 Essential command binaries that need to be |
938 available in single user mode; for all users, |
3979 available in single user mode; for all users, |
939 e.g., <span class="emphasis"><em>cat</em></span>, <span class="emphasis"><em>ls</em></span>, <span class="emphasis"><em>cp</em></span>.</p> |
3980 e.g., |
940 <p><a name="ch9Using_Linux_Tools_118"></a>/boot/</p> |
3981 <span class="emphasis"> |
941 <p><a name="ch9Using_Linux_Tools_119"></a>Boot loader files, e.g., <span class="emphasis"><em>kernels</em></span>, <span class="emphasis"><em>initrd</em></span>; |
3982 <em> |
942 often a separate partition.</p> |
3983 cat |
943 <p><a name="ch9Using_Linux_Tools_11a"></a>/dev/</p> |
3984 </em> |
944 <p><a name="ch9Using_Linux_Tools_11b"></a>Essential devices, e.g., /dev/null</p> |
3985 </span> |
945 <p><a name="ch9Using_Linux_Tools_11c"></a>/etc/</p> |
3986 , |
946 <p><a name="ch9Using_Linux_Tools_11d"></a>Host-specific system-wide configuration files |
3987 <span class="emphasis"> |
947 (the name comes from <span class="emphasis"><em>et cetera</em></span>)</p> |
3988 <em> |
948 <p><a name="ch9Using_Linux_Tools_11e"></a>/home/</p> |
3989 ls |
949 <p><a name="ch9Using_Linux_Tools_11f"></a>User's home directories, containing saved |
3990 </em> |
|
3991 </span> |
|
3992 , |
|
3993 <span class="emphasis"> |
|
3994 <em> |
|
3995 cp |
|
3996 </em> |
|
3997 </span> |
|
3998 . |
|
3999 </p> |
|
4000 <p id="ch9Using_Linux_Tools_118"> |
|
4001 /boot/ |
|
4002 </p> |
|
4003 <p id="ch9Using_Linux_Tools_119"> |
|
4004 Boot loader files, e.g., |
|
4005 <span class="emphasis"> |
|
4006 <em> |
|
4007 kernels |
|
4008 </em> |
|
4009 </span> |
|
4010 , |
|
4011 <span class="emphasis"> |
|
4012 <em> |
|
4013 initrd |
|
4014 </em> |
|
4015 </span> |
|
4016 ; |
|
4017 often a separate partition. |
|
4018 </p> |
|
4019 <p id="ch9Using_Linux_Tools_11a"> |
|
4020 /dev/ |
|
4021 </p> |
|
4022 <p id="ch9Using_Linux_Tools_11b"> |
|
4023 Essential devices, e.g., /dev/null |
|
4024 </p> |
|
4025 <p id="ch9Using_Linux_Tools_11c"> |
|
4026 /etc/ |
|
4027 </p> |
|
4028 <p id="ch9Using_Linux_Tools_11d"> |
|
4029 Host-specific system-wide configuration files |
|
4030 (the name comes from |
|
4031 <span class="emphasis"> |
|
4032 <em> |
|
4033 et cetera |
|
4034 </em> |
|
4035 </span> |
|
4036 ) |
|
4037 </p> |
|
4038 <p id="ch9Using_Linux_Tools_11e"> |
|
4039 /home/ |
|
4040 </p> |
|
4041 <p id="ch9Using_Linux_Tools_11f"> |
|
4042 User's home directories, containing saved |
950 files, personal settings, etc.; often a |
4043 files, personal settings, etc.; often a |
951 separate partition.</p> |
4044 separate partition. |
952 <p><a name="ch9Using_Linux_Tools_120"></a>/lib/</p> |
4045 </p> |
953 <p><a name="ch9Using_Linux_Tools_121"></a>Libraries essential for the binaries in |
4046 <p id="ch9Using_Linux_Tools_120"> |
954 <span class="emphasis"><em>/bin/</em></span> and <span class="emphasis"><em>/sbin/</em></span></p> |
4047 /lib/ |
955 <p><a name="ch9Using_Linux_Tools_122"></a>/media/</p> |
4048 </p> |
956 <p><a name="ch9Using_Linux_Tools_123"></a>Mount points for removable media such as |
4049 <p id="ch9Using_Linux_Tools_121"> |
957 CD-ROMs, external hard disks, USB sticks, etc.</p> |
4050 Libraries essential for the binaries in |
958 <p><a name="ch9Using_Linux_Tools_124"></a>/mnt/</p> |
4051 <span class="emphasis"> |
959 <p><a name="ch9Using_Linux_Tools_125"></a>Temporarily mounted file systems</p> |
4052 <em> |
960 <p><a name="ch9Using_Linux_Tools_126"></a>/opt/</p> |
4053 /bin/ |
961 <p><a name="ch9Using_Linux_Tools_127"></a>Optional application software packages</p> |
4054 </em> |
962 <p><a name="ch9Using_Linux_Tools_128"></a>/proc/</p> |
4055 </span> |
963 <p><a name="ch9Using_Linux_Tools_129"></a>Virtual filesystem documenting kernel and |
4056 and |
|
4057 <span class="emphasis"> |
|
4058 <em> |
|
4059 /sbin/ |
|
4060 </em> |
|
4061 </span> |
|
4062 </p> |
|
4063 <p id="ch9Using_Linux_Tools_122"> |
|
4064 /media/ |
|
4065 </p> |
|
4066 <p id="ch9Using_Linux_Tools_123"> |
|
4067 Mount points for removable media such as |
|
4068 CD-ROMs, external hard disks, USB sticks, etc. |
|
4069 </p> |
|
4070 <p id="ch9Using_Linux_Tools_124"> |
|
4071 /mnt/ |
|
4072 </p> |
|
4073 <p id="ch9Using_Linux_Tools_125"> |
|
4074 Temporarily mounted file systems |
|
4075 </p> |
|
4076 <p id="ch9Using_Linux_Tools_126"> |
|
4077 /opt/ |
|
4078 </p> |
|
4079 <p id="ch9Using_Linux_Tools_127"> |
|
4080 Optional application software packages |
|
4081 </p> |
|
4082 <p id="ch9Using_Linux_Tools_128"> |
|
4083 /proc/ |
|
4084 </p> |
|
4085 <p id="ch9Using_Linux_Tools_129"> |
|
4086 Virtual filesystem documenting kernel and |
964 process status as text files; e.g., uptime, |
4087 process status as text files; e.g., uptime, |
965 network. In Linux, corresponds to a <span class="emphasis"><em>Procfs</em></span> |
4088 network. In Linux, corresponds to a |
966 mount.</p> |
4089 <span class="emphasis"> |
967 <p><a name="ch9Using_Linux_Tools_12a"></a>/root/</p> |
4090 <em> |
968 <p><a name="ch9Using_Linux_Tools_12b"></a>Home directory for the root user</p> |
4091 Procfs |
969 <p><a name="ch9Using_Linux_Tools_12c"></a>/sbin/</p> |
4092 </em> |
970 <p><a name="ch9Using_Linux_Tools_12d"></a>Essential system binaries; e.g., <span class="emphasis"><em>init</em></span>, |
4093 </span> |
971 <span class="emphasis"><em>route</em></span>, <span class="emphasis"><em>mount</em></span>.</p> |
4094 mount. |
972 <p><a name="ch9Using_Linux_Tools_12e"></a>/srv/</p> |
4095 </p> |
973 <p><a name="ch9Using_Linux_Tools_12f"></a>Site-specific data which is served by the |
4096 <p id="ch9Using_Linux_Tools_12a"> |
974 system.</p> |
4097 /root/ |
975 <p><a name="ch9Using_Linux_Tools_130"></a>/tmp/</p> |
4098 </p> |
976 <p><a name="ch9Using_Linux_Tools_131"></a>Temporary files. Often not preserved between |
4099 <p id="ch9Using_Linux_Tools_12b"> |
977 system reboots.</p> |
4100 Home directory for the root user |
978 <p><a name="ch9Using_Linux_Tools_132"></a>/usr/</p> |
4101 </p> |
979 <p><a name="ch9Using_Linux_Tools_133"></a>Secondary hierarchy for read-only user data; |
4102 <p id="ch9Using_Linux_Tools_12c"> |
|
4103 /sbin/ |
|
4104 </p> |
|
4105 <p id="ch9Using_Linux_Tools_12d"> |
|
4106 Essential system binaries; e.g., |
|
4107 <span class="emphasis"> |
|
4108 <em> |
|
4109 init |
|
4110 </em> |
|
4111 </span> |
|
4112 , |
|
4113 <span class="emphasis"> |
|
4114 <em> |
|
4115 route |
|
4116 </em> |
|
4117 </span> |
|
4118 , |
|
4119 <span class="emphasis"> |
|
4120 <em> |
|
4121 mount |
|
4122 </em> |
|
4123 </span> |
|
4124 . |
|
4125 </p> |
|
4126 <p id="ch9Using_Linux_Tools_12e"> |
|
4127 /srv/ |
|
4128 </p> |
|
4129 <p id="ch9Using_Linux_Tools_12f"> |
|
4130 Site-specific data which is served by the |
|
4131 system. |
|
4132 </p> |
|
4133 <p id="ch9Using_Linux_Tools_130"> |
|
4134 /tmp/ |
|
4135 </p> |
|
4136 <p id="ch9Using_Linux_Tools_131"> |
|
4137 Temporary files. Often not preserved between |
|
4138 system reboots. |
|
4139 </p> |
|
4140 <p id="ch9Using_Linux_Tools_132"> |
|
4141 /usr/ |
|
4142 </p> |
|
4143 <p id="ch9Using_Linux_Tools_133"> |
|
4144 Secondary hierarchy for read-only user data; |
980 contains the majority of (multi-)user |
4145 contains the majority of (multi-)user |
981 utilities and applications.</p> |
4146 utilities and applications. |
982 <p><a name="ch9Using_Linux_Tools_134"></a>/var/</p> |
4147 </p> |
983 <p><a name="ch9Using_Linux_Tools_135"></a>Variable files - files whose content is |
4148 <p id="ch9Using_Linux_Tools_134"> |
|
4149 /var/ |
|
4150 </p> |
|
4151 <p id="ch9Using_Linux_Tools_135"> |
|
4152 Variable files - files whose content is |
984 expected to continually change during normal |
4153 expected to continually change during normal |
985 operation of the system - such as logs, spool |
4154 operation of the system - such as logs, spool |
986 files, and temporary e-mail files. |
4155 files, and temporary e-mail files. |
987 Sometimes a separate partition.</p> |
4156 Sometimes a separate partition. |
988 <div class="section" title="10.1. man hier"> |
4157 </p> |
989 <div class="titlepage"><div><div><h3 class="title"> |
4158 <div class="section" title="10.1.man hier"> |
990 <a name="id2949602"></a>10.1. man hier</h3></div></div></div> |
4159 <div class="titlepage"> |
991 <p><a name="ch9Using_Linux_Tools_136"></a>This is the manual page on the UNIX filesystem. The syntax for this is:</p> |
4160 <div> |
992 <pre class="programlisting"> |
4161 <div> |
993 $ man hier</pre> |
4162 <h3 class="title"> |
994 </div> |
4163 <a name="id2628270"> |
995 <div class="section" title="10.2. ls -l"> |
4164 </a> |
996 <div class="titlepage"><div><div><h3 class="title"> |
4165 10.1.man hier |
997 <a name="id2949618"></a>10.2. ls -l</h3></div></div></div> |
4166 </h3> |
998 <p><a name="ch9Using_Linux_Tools_137"></a>Shows you huge amounts of information (permissions, owners, size, and when last modified) for folders and files. The syntax is</p> |
4167 </div> |
999 <pre class="programlisting"> |
4168 </div> |
1000 $ ls -l</pre> |
4169 </div> |
1001 <p><a name="ch9Using_Linux_Tools_138"></a>This can be done after entering the required directory.</p> |
4170 <p id="ch9Using_Linux_Tools_136"> |
1002 </div> |
4171 This is the manual page on the UNIX filesystem. The syntax for this is: |
1003 </div> |
4172 </p> |
1004 <div class="section" title="11. Permissions and Ownership"> |
4173 <pre class="programlisting"> |
1005 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
4174 $ man hier |
1006 <a name="id2949641"></a>11. Permissions and Ownership</h2></div></div></div> |
4175 </pre> |
1007 <div class="section" title="11.1. chmod"> |
4176 </div> |
1008 <div class="titlepage"><div><div><h3 class="title"> |
4177 <div class="section" title="10.2.ls -l"> |
1009 <a name="id2949650"></a>11.1. chmod</h3></div></div></div> |
4178 <div class="titlepage"> |
1010 <p><a name="ch9Using_Linux_Tools_139"></a>The <span class="emphasis"><em>chmod</em></span> command (abbreviated from 'change mode') is a shell command and C language function in Unix and Unix-like environments. When executed, it can change file system modes of files and directories. The modes include permissions and special modes.A chmod command first appeared in AT&T Unix version 1, and is still used today on Unix-like machines.</p> |
4179 <div> |
1011 <div class="section" title="11.1.1. Usage"> |
4180 <div> |
1012 <div class="titlepage"><div><div><h4 class="title"> |
4181 <h3 class="title"> |
1013 <a name="id2949676"></a>11.1.1. Usage</h4></div></div></div> |
4182 <a name="id2628287"> |
1014 <p><a name="ch9Using_Linux_Tools_13a"></a>The <span class="emphasis"><em>chmod</em></span> command options are specified like this:</p> |
4183 </a> |
1015 <pre class="programlisting"> |
4184 10.2.ls -l |
1016 $ chmod [options] mode[,mode] file1 [file2 ...]</pre> |
4185 </h3> |
1017 <p><a name="ch9Using_Linux_Tools_13b"></a>To view what the permissions currently are, type:</p> |
4186 </div> |
1018 <pre class="programlisting"> |
4187 </div> |
1019 $ ls -l file</pre> |
4188 </div> |
1020 </div> |
4189 <p id="ch9Using_Linux_Tools_137"> |
1021 <div class="section" title="11.1.2. Command line options"> |
4190 Shows you huge amounts of information (permissions, owners, size, and when last modified) for folders and files. The syntax is |
1022 <div class="titlepage"><div><div><h4 class="title"> |
4191 </p> |
1023 <a name="id2949704"></a>11.1.2. Command line options</h4></div></div></div> |
4192 <pre class="programlisting"> |
1024 <p><a name="ch9Using_Linux_Tools_13c"></a>The <span class="emphasis"><em>chmod</em></span> command has a number of command line options that affect its behavior. The most common options are:</p> |
4193 $ ls -l |
1025 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
4194 </pre> |
1026 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_13d"></a>-R: Changes the modes of directories and files recursively</p></li> |
4195 <p id="ch9Using_Linux_Tools_138"> |
1027 <li class="listitem" style="list-style-type: *"><p><a name="ch9Using_Linux_Tools_13e"></a>-v: Verbose mode; lists all files as they are being processed</p></li> |
4196 This can be done after entering the required directory. |
1028 </ul></div> |
4197 </p> |
1029 <div class="section" title="11.1.2.1. Symbolic modes"> |
4198 </div> |
1030 <div class="titlepage"><div><div><h5 class="title"> |
4199 </div> |
1031 <a name="id2949740"></a>11.1.2.1. Symbolic modes</h5></div></div></div> |
4200 <div class="section" title="11.Permissions and Ownership"> |
1032 <p><a name="ch9Using_Linux_Tools_13f"></a>To the <span class="emphasis"><em>chmod</em></span> utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:</p> |
4201 <div class="titlepage"> |
1033 <pre class="programlisting"> |
4202 <div> |
1034 $ chmod [references][operator][modes] file1 ...</pre> |
4203 <div> |
1035 <p><a name="ch9Using_Linux_Tools_140"></a>The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all” but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters:</p> |
4204 <h2 class="title" style="clear: both"> |
1036 <p><a name="ch9Using_Linux_Tools_141"></a>Reference</p> |
4205 <a name="id2628310"> |
1037 <p><a name="ch9Using_Linux_Tools_142"></a>Class</p> |
4206 </a> |
1038 <p><a name="ch9Using_Linux_Tools_143"></a>Description</p> |
4207 11.Permissions and Ownership |
1039 <p><a name="ch9Using_Linux_Tools_144"></a>u</p> |
4208 </h2> |
1040 <p><a name="ch9Using_Linux_Tools_145"></a>user</p> |
4209 </div> |
1041 <p><a name="ch9Using_Linux_Tools_146"></a>the owner of the file</p> |
4210 </div> |
1042 <p><a name="ch9Using_Linux_Tools_147"></a>g</p> |
4211 </div> |
1043 <p><a name="ch9Using_Linux_Tools_148"></a>group</p> |
4212 <div class="section" title="11.1.chmod"> |
1044 <p><a name="ch9Using_Linux_Tools_149"></a>users who are members of the file's group</p> |
4213 <div class="titlepage"> |
1045 <p><a name="ch9Using_Linux_Tools_14a"></a>o</p> |
4214 <div> |
1046 <p><a name="ch9Using_Linux_Tools_14b"></a>others</p> |
4215 <div> |
1047 <p><a name="ch9Using_Linux_Tools_14c"></a>users who are not hte owner of the file or |
4216 <h3 class="title"> |
1048 members of the group</p> |
4217 <a name="id2628319"> |
1049 <p><a name="ch9Using_Linux_Tools_14d"></a>a</p> |
4218 </a> |
1050 <p><a name="ch9Using_Linux_Tools_14e"></a>all</p> |
4219 11.1.chmod |
1051 <p><a name="ch9Using_Linux_Tools_14f"></a>all three of the above; is the same as <span class="emphasis"><em>ugo</em></span></p> |
4220 </h3> |
1052 <p><a name="ch9Using_Linux_Tools_150"></a>The <span class="emphasis"><em>chmod</em></span> program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:</p> |
4221 </div> |
1053 <p><a name="ch9Using_Linux_Tools_151"></a>Operator</p> |
4222 </div> |
1054 <p><a name="ch9Using_Linux_Tools_152"></a>Description</p> |
4223 </div> |
1055 <div class="itemizedlist"> |
4224 <p id="ch9Using_Linux_Tools_139"> |
1056 <span style="color: red"><list_item></list_item></span><ul class="itemizedlist" type="*"></ul> |
4225 The |
1057 </div> |
4226 <span class="emphasis"> |
1058 <p><a name="ch9Using_Linux_Tools_153"></a>adds the specified modes to the specified classes</p> |
4227 <em> |
1059 <div class="itemizedlist"> |
4228 chmod |
1060 <span style="color: red"><list_item></list_item></span><ul class="itemizedlist" type="*"></ul> |
4229 </em> |
1061 </div> |
4230 </span> |
1062 <p><a name="ch9Using_Linux_Tools_154"></a>removes the specified modes from the specified |
4231 command (abbreviated from 'change mode') is a shell command and C language function in Unix and Unix-like environments. When executed, it can change file system modes of files and directories. The modes include permissions and special modes.A chmod command first appeared in AT&T Unix version 1, and is still used today on Unix-like machines. |
1063 classes</p> |
4232 </p> |
1064 <p><a name="ch9Using_Linux_Tools_155"></a>=</p> |
4233 <div class="section" title="11.1.1.Usage"> |
1065 <p><a name="ch9Using_Linux_Tools_156"></a>the modes specified are to be made the exact modes |
4234 <div class="titlepage"> |
1066 for the specified classes</p> |
4235 <div> |
1067 <p><a name="ch9Using_Linux_Tools_157"></a>The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions:</p> |
4236 <div> |
1068 <p><a name="ch9Using_Linux_Tools_158"></a>Mode</p> |
4237 <h4 class="title"> |
1069 <p><a name="ch9Using_Linux_Tools_159"></a>Name</p> |
4238 <a name="id2628344"> |
1070 <p><a name="ch9Using_Linux_Tools_15a"></a>Description</p> |
4239 </a> |
1071 <p><a name="ch9Using_Linux_Tools_15b"></a>r</p> |
4240 11.1.1.Usage |
1072 <p><a name="ch9Using_Linux_Tools_15c"></a>read</p> |
4241 </h4> |
1073 <p><a name="ch9Using_Linux_Tools_15d"></a>read a file or list a directory's contents</p> |
4242 </div> |
1074 <p><a name="ch9Using_Linux_Tools_15e"></a>w</p> |
4243 </div> |
1075 <p><a name="ch9Using_Linux_Tools_15f"></a>write</p> |
4244 </div> |
1076 <p><a name="ch9Using_Linux_Tools_160"></a>write to a file or directory</p> |
4245 <p id="ch9Using_Linux_Tools_13a"> |
1077 <p><a name="ch9Using_Linux_Tools_161"></a>x</p> |
4246 The |
1078 <p><a name="ch9Using_Linux_Tools_162"></a>execute</p> |
4247 <span class="emphasis"> |
1079 <p><a name="ch9Using_Linux_Tools_163"></a>execute a file or recurse a directory tree</p> |
4248 <em> |
1080 <p><a name="ch9Using_Linux_Tools_164"></a>X</p> |
4249 chmod |
1081 <p><a name="ch9Using_Linux_Tools_165"></a>special |
4250 </em> |
1082 execute</p> |
4251 </span> |
1083 <p><a name="ch9Using_Linux_Tools_166"></a>which is not a permission in itself but rather |
4252 command options are specified like this: |
|
4253 </p> |
|
4254 <pre class="programlisting"> |
|
4255 $ chmod [options] mode[,mode] file1 [file2 ...] |
|
4256 </pre> |
|
4257 <p id="ch9Using_Linux_Tools_13b"> |
|
4258 To view what the permissions currently are, type: |
|
4259 </p> |
|
4260 <pre class="programlisting"> |
|
4261 $ ls -l file |
|
4262 </pre> |
|
4263 </div> |
|
4264 <div class="section" title="11.1.2.Command line options"> |
|
4265 <div class="titlepage"> |
|
4266 <div> |
|
4267 <div> |
|
4268 <h4 class="title"> |
|
4269 <a name="id2628373"> |
|
4270 </a> |
|
4271 11.1.2.Command line options |
|
4272 </h4> |
|
4273 </div> |
|
4274 </div> |
|
4275 </div> |
|
4276 <p id="ch9Using_Linux_Tools_13c"> |
|
4277 The |
|
4278 <span class="emphasis"> |
|
4279 <em> |
|
4280 chmod |
|
4281 </em> |
|
4282 </span> |
|
4283 command has a number of command line options that affect its behavior. The most common options are: |
|
4284 </p> |
|
4285 <div class="itemizedlist"> |
|
4286 <ul class="itemizedlist" type="*"> |
|
4287 <li class="listitem" style="list-style-type: *"> |
|
4288 <p id="ch9Using_Linux_Tools_13d"> |
|
4289 -R: Changes the modes of directories and files recursively |
|
4290 </p> |
|
4291 </li> |
|
4292 <li class="listitem" style="list-style-type: *"> |
|
4293 <p id="ch9Using_Linux_Tools_13e"> |
|
4294 -v: Verbose mode; lists all files as they are being processed |
|
4295 </p> |
|
4296 </li> |
|
4297 </ul> |
|
4298 </div> |
|
4299 <div class="section" title="11.1.2.1.Symbolic modes"> |
|
4300 <div class="titlepage"> |
|
4301 <div> |
|
4302 <div> |
|
4303 <h5 class="title"> |
|
4304 <a name="id2628408"> |
|
4305 </a> |
|
4306 11.1.2.1.Symbolic modes |
|
4307 </h5> |
|
4308 </div> |
|
4309 </div> |
|
4310 </div> |
|
4311 <p id="ch9Using_Linux_Tools_13f"> |
|
4312 To the |
|
4313 <span class="emphasis"> |
|
4314 <em> |
|
4315 chmod |
|
4316 </em> |
|
4317 </span> |
|
4318 utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text: |
|
4319 </p> |
|
4320 <pre class="programlisting"> |
|
4321 $ chmod [references][operator][modes] file1 ... |
|
4322 </pre> |
|
4323 <p id="ch9Using_Linux_Tools_140"> |
|
4324 The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to all but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters: |
|
4325 </p> |
|
4326 <p id="ch9Using_Linux_Tools_141"> |
|
4327 Reference |
|
4328 </p> |
|
4329 <p id="ch9Using_Linux_Tools_142"> |
|
4330 Class |
|
4331 </p> |
|
4332 <p id="ch9Using_Linux_Tools_143"> |
|
4333 Description |
|
4334 </p> |
|
4335 <p id="ch9Using_Linux_Tools_144"> |
|
4336 u |
|
4337 </p> |
|
4338 <p id="ch9Using_Linux_Tools_145"> |
|
4339 user |
|
4340 </p> |
|
4341 <p id="ch9Using_Linux_Tools_146"> |
|
4342 the owner of the file |
|
4343 </p> |
|
4344 <p id="ch9Using_Linux_Tools_147"> |
|
4345 g |
|
4346 </p> |
|
4347 <p id="ch9Using_Linux_Tools_148"> |
|
4348 group |
|
4349 </p> |
|
4350 <p id="ch9Using_Linux_Tools_149"> |
|
4351 users who are members of the file's group |
|
4352 </p> |
|
4353 <p id="ch9Using_Linux_Tools_14a"> |
|
4354 o |
|
4355 </p> |
|
4356 <p id="ch9Using_Linux_Tools_14b"> |
|
4357 others |
|
4358 </p> |
|
4359 <p id="ch9Using_Linux_Tools_14c"> |
|
4360 users who are not hte owner of the file or |
|
4361 members of the group |
|
4362 </p> |
|
4363 <p id="ch9Using_Linux_Tools_14d"> |
|
4364 a |
|
4365 </p> |
|
4366 <p id="ch9Using_Linux_Tools_14e"> |
|
4367 all |
|
4368 </p> |
|
4369 <p id="ch9Using_Linux_Tools_14f"> |
|
4370 all three of the above; is the same as |
|
4371 <span class="emphasis"> |
|
4372 <em> |
|
4373 ugo |
|
4374 </em> |
|
4375 </span> |
|
4376 </p> |
|
4377 <p id="ch9Using_Linux_Tools_150"> |
|
4378 The |
|
4379 <span class="emphasis"> |
|
4380 <em> |
|
4381 chmod |
|
4382 </em> |
|
4383 </span> |
|
4384 program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted: |
|
4385 </p> |
|
4386 <p id="ch9Using_Linux_Tools_151"> |
|
4387 Operator |
|
4388 </p> |
|
4389 <p id="ch9Using_Linux_Tools_152"> |
|
4390 Description |
|
4391 </p> |
|
4392 <div class="itemizedlist"> |
|
4393 <span style="color: red"> |
|
4394 <list_item></list_item> |
|
4395 </span> |
|
4396 <ul class="itemizedlist" type="*"> |
|
4397 </ul> |
|
4398 </div> |
|
4399 <p id="ch9Using_Linux_Tools_153"> |
|
4400 adds the specified modes to the specified classes |
|
4401 </p> |
|
4402 <div class="itemizedlist"> |
|
4403 <span style="color: red"> |
|
4404 <list_item></list_item> |
|
4405 </span> |
|
4406 <ul class="itemizedlist" type="*"> |
|
4407 </ul> |
|
4408 </div> |
|
4409 <p id="ch9Using_Linux_Tools_154"> |
|
4410 removes the specified modes from the specified |
|
4411 classes |
|
4412 </p> |
|
4413 <p id="ch9Using_Linux_Tools_155"> |
|
4414 = |
|
4415 </p> |
|
4416 <p id="ch9Using_Linux_Tools_156"> |
|
4417 the modes specified are to be made the exact modes |
|
4418 for the specified classes |
|
4419 </p> |
|
4420 <p id="ch9Using_Linux_Tools_157"> |
|
4421 The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions: |
|
4422 </p> |
|
4423 <p id="ch9Using_Linux_Tools_158"> |
|
4424 Mode |
|
4425 </p> |
|
4426 <p id="ch9Using_Linux_Tools_159"> |
|
4427 Name |
|
4428 </p> |
|
4429 <p id="ch9Using_Linux_Tools_15a"> |
|
4430 Description |
|
4431 </p> |
|
4432 <p id="ch9Using_Linux_Tools_15b"> |
|
4433 r |
|
4434 </p> |
|
4435 <p id="ch9Using_Linux_Tools_15c"> |
|
4436 read |
|
4437 </p> |
|
4438 <p id="ch9Using_Linux_Tools_15d"> |
|
4439 read a file or list a directory's contents |
|
4440 </p> |
|
4441 <p id="ch9Using_Linux_Tools_15e"> |
|
4442 w |
|
4443 </p> |
|
4444 <p id="ch9Using_Linux_Tools_15f"> |
|
4445 write |
|
4446 </p> |
|
4447 <p id="ch9Using_Linux_Tools_160"> |
|
4448 write to a file or directory |
|
4449 </p> |
|
4450 <p id="ch9Using_Linux_Tools_161"> |
|
4451 x |
|
4452 </p> |
|
4453 <p id="ch9Using_Linux_Tools_162"> |
|
4454 execute |
|
4455 </p> |
|
4456 <p id="ch9Using_Linux_Tools_163"> |
|
4457 execute a file or recurse a directory tree |
|
4458 </p> |
|
4459 <p id="ch9Using_Linux_Tools_164"> |
|
4460 X |
|
4461 </p> |
|
4462 <p id="ch9Using_Linux_Tools_165"> |
|
4463 special |
|
4464 execute |
|
4465 </p> |
|
4466 <p id="ch9Using_Linux_Tools_166"> |
|
4467 which is not a permission in itself but rather |
1084 can be used instead of 'x'. It applies execute |
4468 can be used instead of 'x'. It applies execute |
1085 permissions to directories regardless of their |
4469 permissions to directories regardless of their |
1086 current permissions and applies execute |
4470 current permissions and applies execute |
1087 permissions to a file which already has at |
4471 permissions to a file which already has at |
1088 least 1 execute permission bit already set |
4472 least 1 execute permission bit already set |
1089 (either user, group or other). It is only |
4473 (either user, group or other). It is only |
1090 really useful when used with '+' and usually |
4474 really useful when used with '+' and usually |
1091 in combination with the <span class="emphasis"><em>-R</em></span> option for giving |
4475 in combination with the |
|
4476 <span class="emphasis"> |
|
4477 <em> |
|
4478 -R |
|
4479 </em> |
|
4480 </span> |
|
4481 option for giving |
1092 group or other access to a big directory tree |
4482 group or other access to a big directory tree |
1093 without setting execute permission on normal |
4483 without setting execute permission on normal |
1094 files (such as text files), which would |
4484 files (such as text files), which would |
1095 normally happen if one just used 'chmod -R |
4485 normally happen if one just used 'chmod -R |
1096 a+rx .', whereas with 'X' one can do 'chmod -R |
4486 a+rx .', whereas with 'X' one can do 'chmod -R |
1097 a+rX .' instead.</p> |
4487 a+rX .' instead. |
1098 <p><a name="ch9Using_Linux_Tools_167"></a>s</p> |
4488 </p> |
1099 <p><a name="ch9Using_Linux_Tools_168"></a>setuid/gid</p> |
4489 <p id="ch9Using_Linux_Tools_167"> |
1100 <p><a name="ch9Using_Linux_Tools_169"></a>are Unix access rights flags that allow users |
4490 s |
|
4491 </p> |
|
4492 <p id="ch9Using_Linux_Tools_168"> |
|
4493 setuid/gid |
|
4494 </p> |
|
4495 <p id="ch9Using_Linux_Tools_169"> |
|
4496 are Unix access rights flags that allow users |
1101 to run an executable with the permissions of |
4497 to run an executable with the permissions of |
1102 the executable's owner or group.They are often |
4498 the executable's owner or group.They are often |
1103 used to allow users on a computer system to run |
4499 used to allow users on a computer system to run |
1104 programs with temporarily elevated privileges |
4500 programs with temporarily elevated privileges |
1105 in order to perform a specific task. While the |
4501 in order to perform a specific task. While the |
1106 assumed user id or group id privileges provided |
4502 assumed user id or group id privileges provided |
1107 are not always elevated, at a minimum they are |
4503 are not always elevated, at a minimum they are |
1108 specific.They are needed for tasks that require |
4504 specific.They are needed for tasks that require |
1109 higher privileges than those which a common |
4505 higher privileges than those which a common |
1110 user has, such as changing his or her login |
4506 user has, such as changing his or her login |
1111 password.</p> |
4507 password. |
1112 <p><a name="ch9Using_Linux_Tools_16a"></a>t</p> |
4508 </p> |
1113 <p><a name="ch9Using_Linux_Tools_16b"></a>sticky</p> |
4509 <p id="ch9Using_Linux_Tools_16a"> |
1114 <p><a name="ch9Using_Linux_Tools_16c"></a>The most common use of the sticky bit today is |
4510 t |
|
4511 </p> |
|
4512 <p id="ch9Using_Linux_Tools_16b"> |
|
4513 sticky |
|
4514 </p> |
|
4515 <p id="ch9Using_Linux_Tools_16c"> |
|
4516 The most common use of the sticky bit today is |
1115 on directories, where, when set, items inside |
4517 on directories, where, when set, items inside |
1116 the directory can be renamed or deleted only by |
4518 the directory can be renamed or deleted only by |
1117 the item's owner, the directory's owner, or the |
4519 the item's owner, the directory's owner, or the |
1118 superuser; without the sticky bit set, any user |
4520 superuser; without the sticky bit set, any user |
1119 with write and execute permissions for the |
4521 with write and execute permissions for the |
1120 directory can rename or delete contained files, |
4522 directory can rename or delete contained files, |
1121 regardless of owner.</p> |
4523 regardless of owner. |
1122 <p><a name="ch9Using_Linux_Tools_16d"></a>The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas.</p> |
4524 </p> |
1123 </div> |
4525 <p id="ch9Using_Linux_Tools_16d"> |
1124 <div class="section" title="11.1.2.2. Symbolic examples"> |
4526 The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas. |
1125 <div class="titlepage"><div><div><h5 class="title"> |
4527 </p> |
1126 <a name="id2945605"></a>11.1.2.2. Symbolic examples</h5></div></div></div> |
4528 </div> |
1127 <p><a name="ch9Using_Linux_Tools_16e"></a>Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory:</p> |
4529 <div class="section" title="11.1.2.2.Symbolic examples"> |
1128 <pre class="programlisting"> |
4530 <div class="titlepage"> |
1129 $ chmod ug+rw mydir |
4531 <div> |
|
4532 <div> |
|
4533 <h5 class="title"> |
|
4534 <a name="id2624274"> |
|
4535 </a> |
|
4536 11.1.2.2.Symbolic examples |
|
4537 </h5> |
|
4538 </div> |
|
4539 </div> |
|
4540 </div> |
|
4541 <p id="ch9Using_Linux_Tools_16e"> |
|
4542 Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory: |
|
4543 </p> |
|
4544 <pre class="programlisting"> |
|
4545 $ chmod ug+rw mydir |
1130 $ ls -ld mydir |
4546 $ ls -ld mydir |
1131 drw-rw---- 2 starwars yoda 96 Dec 8 12:53 mydir</pre> |
4547 drw-rw---- 2 starwars yoda 96 Dec 8 12:53 mydir |
1132 <p><a name="ch9Using_Linux_Tools_16f"></a>For a file, remove <span class="emphasis"><em>write</em></span> permissions for all classes:</p> |
4548 </pre> |
1133 <pre class="programlisting"> |
4549 <p id="ch9Using_Linux_Tools_16f"> |
1134 $ chmod a-w myfile |
4550 For a file, remove |
|
4551 <span class="emphasis"> |
|
4552 <em> |
|
4553 write |
|
4554 </em> |
|
4555 </span> |
|
4556 permissions for all classes: |
|
4557 </p> |
|
4558 <pre class="programlisting"> |
|
4559 $ chmod a-w myfile |
1135 $ ls -l myfile |
4560 $ ls -l myfile |
1136 -r-xr-xr-x 2 starwars yoda 96 Dec 8 12:53 myfile</pre> |
4561 -r-xr-xr-x 2 starwars yoda 96 Dec 8 12:53 myfile |
1137 <p><a name="ch9Using_Linux_Tools_170"></a>Set the permissions for the <span class="emphasis"><em>u*ser and the *g*roup to read and execute only (no write permission) on *mydir</em></span>.</p> |
4562 </pre> |
1138 <pre class="programlisting"> |
4563 <p id="ch9Using_Linux_Tools_170"> |
1139 $ chmod ug=rx mydir |
4564 Set the permissions for the |
|
4565 <span class="emphasis"> |
|
4566 <em> |
|
4567 u*ser and the *g*roup to read and execute only (no write permission) on *mydir |
|
4568 </em> |
|
4569 </span> |
|
4570 . |
|
4571 </p> |
|
4572 <pre class="programlisting"> |
|
4573 $ chmod ug=rx mydir |
1140 $ ls -ld mydir |
4574 $ ls -ld mydir |
1141 dr-xr-x--- 2 starwars yoda 96 Dec 8 12:53 mydir</pre> |
4575 dr-xr-x--- 2 starwars yoda 96 Dec 8 12:53 mydir |
1142 </div> |
4576 </pre> |
1143 <div class="section" title="11.1.2.3. Octal numbers"> |
4577 </div> |
1144 <div class="titlepage"><div><div><h5 class="title"> |
4578 <div class="section" title="11.1.2.3.Octal numbers"> |
1145 <a name="id2945650"></a>11.1.2.3. Octal numbers</h5></div></div></div> |
4579 <div class="titlepage"> |
1146 <p><a name="ch9Using_Linux_Tools_171"></a>The <span class="emphasis"><em>chmod</em></span> command also accepts three and four-digit octal numbers representing modes. Using a three-digit octal number to set the modes of a file named myfile :</p> |
4580 <div> |
1147 <pre class="programlisting"> |
4581 <div> |
1148 $ chmod 664 myfile |
4582 <h5 class="title"> |
|
4583 <a name="id2624319"> |
|
4584 </a> |
|
4585 11.1.2.3.Octal numbers |
|
4586 </h5> |
|
4587 </div> |
|
4588 </div> |
|
4589 </div> |
|
4590 <p id="ch9Using_Linux_Tools_171"> |
|
4591 The |
|
4592 <span class="emphasis"> |
|
4593 <em> |
|
4594 chmod |
|
4595 </em> |
|
4596 </span> |
|
4597 command also accepts three and four-digit octal numbers representing modes. Using a three-digit octal number to set the modes of a file named myfile : |
|
4598 </p> |
|
4599 <pre class="programlisting"> |
|
4600 $ chmod 664 myfile |
1149 $ ls -l myfile |
4601 $ ls -l myfile |
1150 -rw-rw-r-- 1 57 Jul 3 10:13 myfile</pre> |
4602 -rw-rw-r-- 1 57 Jul 3 10:13 myfile |
1151 <p><a name="ch9Using_Linux_Tools_172"></a>Since the <span class="emphasis"><em>setuid</em></span>, <span class="emphasis"><em>setgid</em></span> and <span class="emphasis"><em>sticky</em></span> bits are not set, this is equivalent to:</p> |
4603 </pre> |
1152 <pre class="programlisting"> |
4604 <p id="ch9Using_Linux_Tools_172"> |
1153 $ chmod 0664 myfile</pre> |
4605 Since the |
1154 </div> |
4606 <span class="emphasis"> |
1155 <div class="section" title="11.1.2.4. Special modes"> |
4607 <em> |
1156 <div class="titlepage"><div><div><h5 class="title"> |
4608 setuid |
1157 <a name="id2950277"></a>11.1.2.4. Special modes</h5></div></div></div> |
4609 </em> |
1158 <p><a name="ch9Using_Linux_Tools_173"></a>The <span class="emphasis"><em>chmod</em></span> command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use <span class="strong"><strong>s</strong></span> to represent the <span class="emphasis"><em>setuid</em></span> and <span class="emphasis"><em>setgid</em></span> modes, and <span class="strong"><strong>t</strong></span> to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.</p> |
4610 </span> |
1159 <p><a name="ch9Using_Linux_Tools_174"></a>Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used.</p> |
4611 , |
1160 </div> |
4612 <span class="emphasis"> |
1161 </div> |
4613 <em> |
1162 </div> |
4614 setgid |
1163 </div> |
4615 </em> |
1164 <div class="section" title="12. Redirection and Piping"> |
4616 </span> |
1165 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
4617 and |
1166 <a name="id2945091"></a>12. Redirection and Piping</h2></div></div></div> |
4618 <span class="emphasis"> |
1167 <p><a name="ch9Using_Linux_Tools_175"></a>In computing, <span class="emphasis"><em>redirection</em></span> is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.</p> |
4619 <em> |
1168 <p><a name="ch9Using_Linux_Tools_176"></a>Programs do redirection with the <span class="emphasis"><em>dup2(2)</em></span> system call, or its less-flexible but higher-level stdio analogues, <span class="emphasis"><em>freopen(3)</em></span> and <span class="emphasis"><em>popen(3)</em></span>.</p> |
4620 sticky |
1169 <div class="section" title="12.1. Redirecting standard input and standard output"> |
4621 </em> |
1170 <div class="titlepage"><div><div><h3 class="title"> |
4622 </span> |
1171 <a name="id2950576"></a>12.1. Redirecting standard input and standard output</h3></div></div></div> |
4623 bits are not set, this is equivalent to: |
1172 <p><a name="ch9Using_Linux_Tools_177"></a>Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows:</p> |
4624 </p> |
1173 <pre class="programlisting"> |
4625 <pre class="programlisting"> |
1174 $ command1 > file1</pre> |
4626 $ chmod 0664 myfile |
1175 <p><a name="ch9Using_Linux_Tools_178"></a>executes <span class="emphasis"><em>command1</em></span>, placing the output in file1. Note that this will truncate any existing data in <span class="emphasis"><em>file1</em></span>. To append output to the end of the file, use the >> operator.:</p> |
4627 </pre> |
1176 <pre class="programlisting"> |
4628 </div> |
1177 $ command1 < file1</pre> |
4629 <div class="section" title="11.1.2.4.Special modes"> |
1178 <p><a name="ch9Using_Linux_Tools_179"></a>executes <span class="emphasis"><em>command1</em></span>, using <span class="emphasis"><em>file1</em></span> as the source of input (as opposed to the keyboard).:</p> |
4630 <div class="titlepage"> |
1179 <pre class="programlisting"> |
4631 <div> |
1180 $ command1 < infile > outfile</pre> |
4632 <div> |
1181 <p><a name="ch9Using_Linux_Tools_17a"></a>combines the two capabilities: <span class="emphasis"><em>command1</em></span> reads from <span class="emphasis"><em>infile</em></span> and writes to <span class="emphasis"><em>outfile</em></span></p> |
4633 <h5 class="title"> |
1182 </div> |
4634 <a name="id2628946"> |
1183 <div class="section" title="12.2. Piping"> |
4635 </a> |
1184 <div class="titlepage"><div><div><h3 class="title"> |
4636 11.1.2.4.Special modes |
1185 <a name="id2950645"></a>12.2. Piping</h3></div></div></div> |
4637 </h5> |
1186 <p><a name="ch9Using_Linux_Tools_17b"></a>Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file: |
4638 </div> |
1187 A pipeline of three programs run on a text terminal:</p> |
4639 </div> |
1188 <pre class="programlisting"> |
4640 </div> |
1189 $ command1 | command2</pre> |
4641 <p id="ch9Using_Linux_Tools_173"> |
1190 <p><a name="ch9Using_Linux_Tools_17c"></a>executes <span class="emphasis"><em>command1</em></span>, using its output as the input for <span class="emphasis"><em>command2</em></span> (commonly called piping, since the "|" character is known as a "pipe").</p> |
4642 The |
1191 <p><a name="ch9Using_Linux_Tools_17d"></a>This is equivalent to using two redirects and a temporary file:</p> |
4643 <span class="emphasis"> |
1192 <pre class="programlisting"> |
4644 <em> |
1193 $ command1 > tempfile |
4645 chmod |
|
4646 </em> |
|
4647 </span> |
|
4648 command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use |
|
4649 <span class="strong"> |
|
4650 <strong> |
|
4651 s |
|
4652 </strong> |
|
4653 </span> |
|
4654 to represent the |
|
4655 <span class="emphasis"> |
|
4656 <em> |
|
4657 setuid |
|
4658 </em> |
|
4659 </span> |
|
4660 and |
|
4661 <span class="emphasis"> |
|
4662 <em> |
|
4663 setgid |
|
4664 </em> |
|
4665 </span> |
|
4666 modes, and |
|
4667 <span class="strong"> |
|
4668 <strong> |
|
4669 t |
|
4670 </strong> |
|
4671 </span> |
|
4672 to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified. |
|
4673 </p> |
|
4674 <p id="ch9Using_Linux_Tools_174"> |
|
4675 Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used. |
|
4676 </p> |
|
4677 </div> |
|
4678 </div> |
|
4679 </div> |
|
4680 </div> |
|
4681 <div class="section" title="12.Redirection and Piping"> |
|
4682 <div class="titlepage"> |
|
4683 <div> |
|
4684 <div> |
|
4685 <h2 class="title" style="clear: both"> |
|
4686 <a name="id2623759"> |
|
4687 </a> |
|
4688 12.Redirection and Piping |
|
4689 </h2> |
|
4690 </div> |
|
4691 </div> |
|
4692 </div> |
|
4693 <p id="ch9Using_Linux_Tools_175"> |
|
4694 In computing, |
|
4695 <span class="emphasis"> |
|
4696 <em> |
|
4697 redirection |
|
4698 </em> |
|
4699 </span> |
|
4700 is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations. |
|
4701 </p> |
|
4702 <p id="ch9Using_Linux_Tools_176"> |
|
4703 Programs do redirection with the |
|
4704 <span class="emphasis"> |
|
4705 <em> |
|
4706 dup2(2) |
|
4707 </em> |
|
4708 </span> |
|
4709 system call, or its less-flexible but higher-level stdio analogues, |
|
4710 <span class="emphasis"> |
|
4711 <em> |
|
4712 freopen(3) |
|
4713 </em> |
|
4714 </span> |
|
4715 and |
|
4716 <span class="emphasis"> |
|
4717 <em> |
|
4718 popen(3) |
|
4719 </em> |
|
4720 </span> |
|
4721 . |
|
4722 </p> |
|
4723 <div class="section" title="12.1.Redirecting standard input and standard output"> |
|
4724 <div class="titlepage"> |
|
4725 <div> |
|
4726 <div> |
|
4727 <h3 class="title"> |
|
4728 <a name="id2629245"> |
|
4729 </a> |
|
4730 12.1.Redirecting standard input and standard output |
|
4731 </h3> |
|
4732 </div> |
|
4733 </div> |
|
4734 </div> |
|
4735 <p id="ch9Using_Linux_Tools_177"> |
|
4736 Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows: |
|
4737 </p> |
|
4738 <pre class="programlisting"> |
|
4739 $ command1 > file1 |
|
4740 </pre> |
|
4741 <p id="ch9Using_Linux_Tools_178"> |
|
4742 executes |
|
4743 <span class="emphasis"> |
|
4744 <em> |
|
4745 command1 |
|
4746 </em> |
|
4747 </span> |
|
4748 , placing the output in file1. Note that this will truncate any existing data in |
|
4749 <span class="emphasis"> |
|
4750 <em> |
|
4751 file1 |
|
4752 </em> |
|
4753 </span> |
|
4754 . To append output to the end of the file, use the >> operator.: |
|
4755 </p> |
|
4756 <pre class="programlisting"> |
|
4757 $ command1 < file1 |
|
4758 </pre> |
|
4759 <p id="ch9Using_Linux_Tools_179"> |
|
4760 executes |
|
4761 <span class="emphasis"> |
|
4762 <em> |
|
4763 command1 |
|
4764 </em> |
|
4765 </span> |
|
4766 , using |
|
4767 <span class="emphasis"> |
|
4768 <em> |
|
4769 file1 |
|
4770 </em> |
|
4771 </span> |
|
4772 as the source of input (as opposed to the keyboard).: |
|
4773 </p> |
|
4774 <pre class="programlisting"> |
|
4775 $ command1 < infile > outfile |
|
4776 </pre> |
|
4777 <p id="ch9Using_Linux_Tools_17a"> |
|
4778 combines the two capabilities: |
|
4779 <span class="emphasis"> |
|
4780 <em> |
|
4781 command1 |
|
4782 </em> |
|
4783 </span> |
|
4784 reads from |
|
4785 <span class="emphasis"> |
|
4786 <em> |
|
4787 infile |
|
4788 </em> |
|
4789 </span> |
|
4790 and writes to |
|
4791 <span class="emphasis"> |
|
4792 <em> |
|
4793 outfile |
|
4794 </em> |
|
4795 </span> |
|
4796 </p> |
|
4797 </div> |
|
4798 <div class="section" title="12.2.Piping"> |
|
4799 <div class="titlepage"> |
|
4800 <div> |
|
4801 <div> |
|
4802 <h3 class="title"> |
|
4803 <a name="id2629314"> |
|
4804 </a> |
|
4805 12.2.Piping |
|
4806 </h3> |
|
4807 </div> |
|
4808 </div> |
|
4809 </div> |
|
4810 <p id="ch9Using_Linux_Tools_17b"> |
|
4811 Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file: |
|
4812 A pipeline of three programs run on a text terminal: |
|
4813 </p> |
|
4814 <pre class="programlisting"> |
|
4815 $ command1 | command2 |
|
4816 </pre> |
|
4817 <p id="ch9Using_Linux_Tools_17c"> |
|
4818 executes |
|
4819 <span class="emphasis"> |
|
4820 <em> |
|
4821 command1 |
|
4822 </em> |
|
4823 </span> |
|
4824 , using its output as the input for |
|
4825 <span class="emphasis"> |
|
4826 <em> |
|
4827 command2 |
|
4828 </em> |
|
4829 </span> |
|
4830 (commonly called piping, since the "|" character is known as a "pipe"). |
|
4831 </p> |
|
4832 <p id="ch9Using_Linux_Tools_17d"> |
|
4833 This is equivalent to using two redirects and a temporary file: |
|
4834 </p> |
|
4835 <pre class="programlisting"> |
|
4836 $ command1 > tempfile |
1194 $ command2 < tempfile |
4837 $ command2 < tempfile |
1195 $ rm tempfile</pre> |
4838 $ rm tempfile |
1196 <p><a name="ch9Using_Linux_Tools_17e"></a>A good example for command piping is combining <span class="emphasis"><em>echo</em></span> with another command to achieve something interactive in a non-interactive shell, e.g.:</p> |
4839 </pre> |
1197 <pre class="programlisting"> |
4840 <p id="ch9Using_Linux_Tools_17e"> |
1198 $ echo -e "user npass" | ftp localhost</pre> |
4841 A good example for command piping is combining |
1199 <p><a name="ch9Using_Linux_Tools_17f"></a>This runs the ftp client with input user, press return, then pass.</p> |
4842 <span class="emphasis"> |
1200 </div> |
4843 <em> |
1201 <div class="section" title="12.3. Redirecting to and from the standard file handles"> |
4844 echo |
1202 <div class="titlepage"><div><div><h3 class="title"> |
4845 </em> |
1203 <a name="id2950707"></a>12.3. Redirecting to and from the standard file handles</h3></div></div></div> |
4846 </span> |
1204 <p><a name="ch9Using_Linux_Tools_180"></a>In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are:</p> |
4847 with another command to achieve something interactive in a non-interactive shell, e.g.: |
1205 <p><a name="ch9Using_Linux_Tools_181"></a>Handle</p> |
4848 </p> |
1206 <p><a name="ch9Using_Linux_Tools_182"></a>Name</p> |
4849 <pre class="programlisting"> |
1207 <p><a name="ch9Using_Linux_Tools_183"></a>Description</p> |
4850 $ echo -e "user npass" | ftp localhost |
1208 <p><a name="ch9Using_Linux_Tools_184"></a>0</p> |
4851 </pre> |
1209 <p><a name="ch9Using_Linux_Tools_185"></a>stdin</p> |
4852 <p id="ch9Using_Linux_Tools_17f"> |
1210 <p><a name="ch9Using_Linux_Tools_186"></a>Standard input</p> |
4853 This runs the ftp client with input user, press return, then pass. |
1211 <p><a name="ch9Using_Linux_Tools_187"></a>1</p> |
4854 </p> |
1212 <p><a name="ch9Using_Linux_Tools_188"></a>stdout</p> |
4855 </div> |
1213 <p><a name="ch9Using_Linux_Tools_189"></a>Standard output</p> |
4856 <div class="section" title="12.3.Redirecting to and from the standard file handles"> |
1214 <p><a name="ch9Using_Linux_Tools_18a"></a>2</p> |
4857 <div class="titlepage"> |
1215 <p><a name="ch9Using_Linux_Tools_18b"></a>stderr</p> |
4858 <div> |
1216 <p><a name="ch9Using_Linux_Tools_18c"></a>Standard error</p> |
4859 <div> |
1217 <p><a name="ch9Using_Linux_Tools_18d"></a>For example:</p> |
4860 <h3 class="title"> |
1218 <pre class="programlisting"> |
4861 <a name="id2629376"> |
1219 $ command1 2> file1</pre> |
4862 </a> |
1220 <p><a name="ch9Using_Linux_Tools_18e"></a>executes <span class="emphasis"><em>command1</em></span>, directing the standard error stream to <span class="emphasis"><em>file1</em></span>.</p> |
4863 12.3.Redirecting to and from the standard file handles |
1221 <p><a name="ch9Using_Linux_Tools_18f"></a>In shells derived from <span class="emphasis"><em>csh</em></span> (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result.</p> |
4864 </h3> |
1222 <p><a name="ch9Using_Linux_Tools_190"></a>Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example:</p> |
4865 </div> |
1223 <pre class="programlisting"> |
4866 </div> |
1224 $ find / -name .profile > results 2>&1</pre> |
4867 </div> |
1225 <p><a name="ch9Using_Linux_Tools_191"></a>will try to find all files named <span class="emphasis"><em>.profile</em></span>. Executed without redirection, it will output hits to <span class="emphasis"><em>stdout</em></span> and errors (e.g. for lack of privilege to traverse protected directories) to <span class="emphasis"><em>stderr</em></span>. If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge <span class="emphasis"><em>stderr</em></span> (handle 2) into <span class="emphasis"><em>stdout</em></span> (handle 1) using 2>&1 .</p> |
4868 <p id="ch9Using_Linux_Tools_180"> |
1226 <p><a name="ch9Using_Linux_Tools_192"></a>It's possible use 2>&1 before ">" but it doesn't work. In fact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged.</p> |
4869 In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are: |
1227 <p><a name="ch9Using_Linux_Tools_193"></a>If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus:</p> |
4870 </p> |
1228 <pre class="programlisting"> |
4871 <p id="ch9Using_Linux_Tools_181"> |
1229 $ find / -name .profile 2>&1 | less</pre> |
4872 Handle |
1230 <p><a name="ch9Using_Linux_Tools_194"></a>A simplified form of the command:</p> |
4873 </p> |
1231 <pre class="programlisting"> |
4874 <p id="ch9Using_Linux_Tools_182"> |
1232 $ command > file 2>&1</pre> |
4875 Name |
1233 <p><a name="ch9Using_Linux_Tools_195"></a>is:</p> |
4876 </p> |
1234 <pre class="programlisting"> |
4877 <p id="ch9Using_Linux_Tools_183"> |
1235 $ command &>file</pre> |
4878 Description |
1236 <p><a name="ch9Using_Linux_Tools_196"></a>or:</p> |
4879 </p> |
1237 <pre class="programlisting"> |
4880 <p id="ch9Using_Linux_Tools_184"> |
1238 $command >&file</pre> |
4881 0 |
1239 </div> |
4882 </p> |
1240 <div class="section" title="12.4. Chained pipelines"> |
4883 <p id="ch9Using_Linux_Tools_185"> |
1241 <div class="titlepage"><div><div><h3 class="title"> |
4884 stdin |
1242 <a name="id2950901"></a>12.4. Chained pipelines</h3></div></div></div> |
4885 </p> |
1243 <p><a name="ch9Using_Linux_Tools_197"></a>The redirection and piping tokens can be chained together to create complex commands. For example:</p> |
4886 <p id="ch9Using_Linux_Tools_186"> |
1244 <pre class="programlisting"> |
4887 Standard input |
1245 $ ls | grep ' .sh' | sort > shlist</pre> |
4888 </p> |
1246 <p><a name="ch9Using_Linux_Tools_198"></a>lists the contents of the current directory, where this output is filtered to only contain lines which contain <span class="emphasis"><em>.sh</em></span>, sort this resultant output lexicographically, and place the final output in <span class="emphasis"><em>shlist</em></span>. This type of construction is used very commonly in shell scripts and batch files.</p> |
4889 <p id="ch9Using_Linux_Tools_187"> |
1247 </div> |
4890 1 |
1248 <div class="section" title="12.5. Redirect to multiple outputs"> |
4891 </p> |
1249 <div class="titlepage"><div><div><h3 class="title"> |
4892 <p id="ch9Using_Linux_Tools_188"> |
1250 <a name="id2950934"></a>12.5. Redirect to multiple outputs</h3></div></div></div> |
4893 stdout |
1251 <p><a name="ch9Using_Linux_Tools_199"></a>The standard command <span class="emphasis"><em>tee</em></span> can redirect output from a command to several destinations.</p> |
4894 </p> |
1252 <pre class="programlisting"> |
4895 <p id="ch9Using_Linux_Tools_189"> |
1253 $ ls -lrt | tee xyz</pre> |
4896 Standard output |
1254 <p><a name="ch9Using_Linux_Tools_19a"></a>This directs the file list output to both standard output as well as to the file <span class="emphasis"><em>xyz</em></span>.</p> |
4897 </p> |
1255 </div> |
4898 <p id="ch9Using_Linux_Tools_18a"> |
1256 </div> |
4899 2 |
1257 <div class="section" title="13. More Text Processing"> |
4900 </p> |
1258 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
4901 <p id="ch9Using_Linux_Tools_18b"> |
1259 <a name="id2950964"></a>13. More Text Processing</h2></div></div></div> |
4902 stderr |
1260 <div class="section" title="13.1. grep"> |
4903 </p> |
1261 <div class="titlepage"><div><div><h3 class="title"> |
4904 <p id="ch9Using_Linux_Tools_18c"> |
1262 <a name="id2950973"></a>13.1. grep</h3></div></div></div> |
4905 Standard error |
1263 <p><a name="ch9Using_Linux_Tools_19b"></a><span class="emphasis"><em>grep</em></span> is a command line text search utility originally written for Unix. The name is taken from the first letters in <span class="emphasis"><em>global / regular expression / print</em></span>, a series of instructions for the <span class="emphasis"><em>ed</em></span> text editor. The <span class="emphasis"><em>grep</em></span> command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.</p> |
4906 </p> |
1264 <div class="section" title="13.1.1. Usage"> |
4907 <p id="ch9Using_Linux_Tools_18d"> |
1265 <div class="titlepage"><div><div><h4 class="title"> |
4908 For example: |
1266 <a name="id2951004"></a>13.1.1. Usage</h4></div></div></div> |
4909 </p> |
1267 <p><a name="ch9Using_Linux_Tools_19c"></a>This is an example of a common <span class="emphasis"><em>grep</em></span> usage:</p> |
4910 <pre class="programlisting"> |
1268 <pre class="programlisting"> |
4911 $ command1 2> file1 |
1269 $ grep apple fruitlist.txt</pre> |
4912 </pre> |
1270 <p><a name="ch9Using_Linux_Tools_19d"></a>In this case, <span class="emphasis"><em>grep</em></span> prints all lines containing 'apple' from the file <span class="emphasis"><em>fruitlist.txt</em></span>, regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The <span class="emphasis"><em>grep</em></span> command is case sensitive by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'.</p> |
4913 <p id="ch9Using_Linux_Tools_18e"> |
1271 <p><a name="ch9Using_Linux_Tools_19e"></a>Like most Unix commands, <span class="emphasis"><em>grep</em></span> accepts command line arguments to change this and many other behaviors. For example:</p> |
4914 executes |
1272 <pre class="programlisting"> |
4915 <span class="emphasis"> |
1273 $ grep -i apple fruitlist.txt</pre> |
4916 <em> |
1274 <p><a name="ch9Using_Linux_Tools_19f"></a>This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells <span class="emphasis"><em>grep</em></span> to be case insensitive, or to ignore case.</p> |
4917 command1 |
1275 <p><a name="ch9Using_Linux_Tools_1a0"></a>To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match):</p> |
4918 </em> |
1276 <pre class="programlisting"> |
4919 </span> |
1277 $ grep -w apple fruitlist.txt</pre> |
4920 , directing the standard error stream to |
1278 <p><a name="ch9Using_Linux_Tools_1a1"></a>Regular expressions can be used to match more complicated queries.</p> |
4921 <span class="emphasis"> |
1279 <div class="section" title="13.1.1.1. Variations"> |
4922 <em> |
1280 <div class="titlepage"><div><div><h5 class="title"> |
4923 file1 |
1281 <a name="id2951083"></a>13.1.1.1. Variations</h5></div></div></div> |
4924 </em> |
1282 <p><a name="ch9Using_Linux_Tools_1a2"></a>There are countless implementations and derivatives of <span class="emphasis"><em>grep</em></span> available for many operating systems. Early variants of <span class="emphasis"><em>grep</em></span> included <span class="emphasis"><em>egrep</em></span> and <span class="emphasis"><em>fgrep</em></span>. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern <span class="emphasis"><em>grep</em></span> implementations as command-line switches (and standardized as -E and -F in POSIX). In such combined implementations, <span class="emphasis"><em>grep</em></span> may also behave differently depending on the name by which it is invoked, allowing <span class="emphasis"><em>fgrep</em></span>, <span class="emphasis"><em>egrep</em></span>, and <span class="emphasis"><em>grep</em></span> to be links to the same program.</p> |
4925 </span> |
1283 <p><a name="ch9Using_Linux_Tools_1a3"></a><span class="emphasis"><em>pcregrep</em></span> is an implementation of <span class="emphasis"><em>grep</em></span> that uses Perl regular expression syntax.</p> |
4926 . |
1284 <p><a name="ch9Using_Linux_Tools_1a4"></a>Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The <span class="emphasis"><em>pgrep</em></span> utility, for instance, displays the processes whose names match a given regular expression.</p> |
4927 </p> |
1285 </div> |
4928 <p id="ch9Using_Linux_Tools_18f"> |
1286 </div> |
4929 In shells derived from |
1287 </div> |
4930 <span class="emphasis"> |
1288 <div class="section" title="13.2. tr"> |
4931 <em> |
1289 <div class="titlepage"><div><div><h3 class="title"> |
4932 csh |
1290 <a name="id2951162"></a>13.2. tr</h3></div></div></div> |
4933 </em> |
1291 <p><a name="ch9Using_Linux_Tools_1a5"></a><span class="emphasis"><em>tr</em></span> (abbreviated from <span class="emphasis"><em>translate</em></span> or <span class="emphasis"><em>transliterate</em></span>) is a command in Unix-like operating systems.</p> |
4934 </span> |
1292 <p><a name="ch9Using_Linux_Tools_1a6"></a>When executed, the program reads from the standard input and writes to the standard output. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set. For example,</p> |
4935 (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result. |
1293 <pre class="programlisting"> |
4936 </p> |
1294 $ tr 'abcd' 'jkmn'</pre> |
4937 <p id="ch9Using_Linux_Tools_190"> |
1295 <p><a name="ch9Using_Linux_Tools_1a7"></a>maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'.</p> |
4938 Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example: |
1296 <p><a name="ch9Using_Linux_Tools_1a8"></a>Sets of characters may be abbreviated by using character ranges. The previous example could be written:</p> |
4939 </p> |
1297 <pre class="programlisting"> |
4940 <pre class="programlisting"> |
1298 $ tr 'a-d' 'jkmn'</pre> |
4941 $ find / -name .profile > results 2>&1 |
1299 <p><a name="ch9Using_Linux_Tools_1a9"></a>In POSIX compliant versions of <span class="emphasis"><em>tr</em></span> the set represented by a character range depends on the locale's collating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced with POSIX character sets such as [:alpha:].</p> |
4942 </pre> |
1300 <p><a name="ch9Using_Linux_Tools_1aa"></a>The <span class="emphasis"><em>-c</em></span> flag complements the first set of characters.</p> |
4943 <p id="ch9Using_Linux_Tools_191"> |
1301 <pre class="programlisting"> |
4944 will try to find all files named |
1302 $ tr -cd '[:alnum:]'</pre> |
4945 <span class="emphasis"> |
1303 <p><a name="ch9Using_Linux_Tools_1ab"></a>therefore removes all non-alphanumeric characters.</p> |
4946 <em> |
1304 <p><a name="ch9Using_Linux_Tools_1ac"></a>The <span class="emphasis"><em>-s</em></span> flag causes tr to compress sequences of identical adjacent characters in its output to a single token. For example,</p> |
4947 .profile |
1305 <pre class="programlisting"> |
4948 </em> |
1306 $ tr -s ' n' ' n'</pre> |
4949 </span> |
1307 <p><a name="ch9Using_Linux_Tools_1ad"></a>replaces sequences of one or more newline characters with a single newline.</p> |
4950 . Executed without redirection, it will output hits to |
1308 <p><a name="ch9Using_Linux_Tools_1ae"></a>The <span class="emphasis"><em>-d</em></span> flag causes tr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters, thereby converting a file in DOS/Windows format to one in Unix format.</p> |
4951 <span class="emphasis"> |
1309 <pre class="programlisting"> |
4952 <em> |
1310 $ tr -d ' r'</pre> |
4953 stdout |
1311 <p><a name="ch9Using_Linux_Tools_1af"></a>Most versions of <span class="emphasis"><em>tr</em></span>, including GNU <span class="emphasis"><em>tr</em></span> and classic Unix <span class="emphasis"><em>tr</em></span>, operate on single byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support.</p> |
4954 </em> |
1312 <p><a name="ch9Using_Linux_Tools_1b0"></a>Ruby and Perl also have an internal <span class="emphasis"><em>tr</em></span> operator, which operates analogously. Tcl's <span class="emphasis"><em>string map</em></span> command is more general in that it maps strings to strings while <span class="emphasis"><em>tr</em></span> maps characters to characters.</p> |
4955 </span> |
1313 </div> |
4956 and errors (e.g. for lack of privilege to traverse protected directories) to |
1314 </div> |
4957 <span class="emphasis"> |
1315 <div class="section" title="14. Elementary Regex"> |
4958 <em> |
1316 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
4959 stderr |
1317 <a name="id2951309"></a>14. Elementary Regex</h2></div></div></div> |
4960 </em> |
1318 <p><a name="ch9Using_Linux_Tools_1b1"></a>In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. A regular expression (often shortened to regex or regexp) is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.</p> |
4961 </span> |
1319 <p><a name="ch9Using_Linux_Tools_1b2"></a>Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, Perl, Ruby and Tcl have a powerful regular expression engine built directly into their syntax. Several utilities provided by Unix distributions—including the editor <span class="emphasis"><em>ed</em></span> and the filter <span class="emphasis"><em>grep</em></span> — were the first to popularize the concept of regular expressions.</p> |
4962 . If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge |
1320 <p><a name="ch9Using_Linux_Tools_1b3"></a>Traditional Unix regular expression syntax followed common conventions but often differed from tool to tool. The IEEE POSIX <span class="emphasis"><em>Basic Regular Expressions</em></span> (BRE) standard (released alongside an alternative flavor called Extended Regular Expressions or ERE) was designed mostly for backward compatibility with the traditional (Simple Regular Expression) syntax but provided a common standard which has since been adopted as the default syntax of many Unix regular expression tools, though there is often some variation or additional features. Many such tools also provide support for ERE syntax with command line arguments.</p> |
4963 <span class="emphasis"> |
1321 <p><a name="ch9Using_Linux_Tools_1b4"></a>In the BRE syntax, most characters are treated as literals — they match only themselves (i.e., a matches "a"). The exceptions, listed below, are called metacharacters or metasequences.</p> |
4964 <em> |
1322 <p><a name="ch9Using_Linux_Tools_1b5"></a>Metacharacter</p> |
4965 stderr |
1323 <p><a name="ch9Using_Linux_Tools_1b6"></a>Description</p> |
4966 </em> |
1324 <p><a name="ch9Using_Linux_Tools_1b7"></a>.</p> |
4967 </span> |
1325 <p><a name="ch9Using_Linux_Tools_1b8"></a>Matches any single character (many applications exclude |
4968 (handle 2) into |
|
4969 <span class="emphasis"> |
|
4970 <em> |
|
4971 stdout |
|
4972 </em> |
|
4973 </span> |
|
4974 (handle 1) using 2>&1 . |
|
4975 </p> |
|
4976 <p id="ch9Using_Linux_Tools_192"> |
|
4977 It's possible use 2>&1 before ">" but it doesn't work. In fact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged. |
|
4978 </p> |
|
4979 <p id="ch9Using_Linux_Tools_193"> |
|
4980 If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus: |
|
4981 </p> |
|
4982 <pre class="programlisting"> |
|
4983 $ find / -name .profile 2>&1 | less |
|
4984 </pre> |
|
4985 <p id="ch9Using_Linux_Tools_194"> |
|
4986 A simplified form of the command: |
|
4987 </p> |
|
4988 <pre class="programlisting"> |
|
4989 $ command > file 2>&1 |
|
4990 </pre> |
|
4991 <p id="ch9Using_Linux_Tools_195"> |
|
4992 is: |
|
4993 </p> |
|
4994 <pre class="programlisting"> |
|
4995 $ command &>file |
|
4996 </pre> |
|
4997 <p id="ch9Using_Linux_Tools_196"> |
|
4998 or: |
|
4999 </p> |
|
5000 <pre class="programlisting"> |
|
5001 $command >&file |
|
5002 </pre> |
|
5003 </div> |
|
5004 <div class="section" title="12.4.Chained pipelines"> |
|
5005 <div class="titlepage"> |
|
5006 <div> |
|
5007 <div> |
|
5008 <h3 class="title"> |
|
5009 <a name="id2629569"> |
|
5010 </a> |
|
5011 12.4.Chained pipelines |
|
5012 </h3> |
|
5013 </div> |
|
5014 </div> |
|
5015 </div> |
|
5016 <p id="ch9Using_Linux_Tools_197"> |
|
5017 The redirection and piping tokens can be chained together to create complex commands. For example: |
|
5018 </p> |
|
5019 <pre class="programlisting"> |
|
5020 $ ls | grep ' .sh' | sort > shlist |
|
5021 </pre> |
|
5022 <p id="ch9Using_Linux_Tools_198"> |
|
5023 lists the contents of the current directory, where this output is filtered to only contain lines which contain |
|
5024 <span class="emphasis"> |
|
5025 <em> |
|
5026 .sh |
|
5027 </em> |
|
5028 </span> |
|
5029 , sort this resultant output lexicographically, and place the final output in |
|
5030 <span class="emphasis"> |
|
5031 <em> |
|
5032 shlist |
|
5033 </em> |
|
5034 </span> |
|
5035 . This type of construction is used very commonly in shell scripts and batch files. |
|
5036 </p> |
|
5037 </div> |
|
5038 <div class="section" title="12.5.Redirect to multiple outputs"> |
|
5039 <div class="titlepage"> |
|
5040 <div> |
|
5041 <div> |
|
5042 <h3 class="title"> |
|
5043 <a name="id2629602"> |
|
5044 </a> |
|
5045 12.5.Redirect to multiple outputs |
|
5046 </h3> |
|
5047 </div> |
|
5048 </div> |
|
5049 </div> |
|
5050 <p id="ch9Using_Linux_Tools_199"> |
|
5051 The standard command |
|
5052 <span class="emphasis"> |
|
5053 <em> |
|
5054 tee |
|
5055 </em> |
|
5056 </span> |
|
5057 can redirect output from a command to several destinations. |
|
5058 </p> |
|
5059 <pre class="programlisting"> |
|
5060 $ ls -lrt | tee xyz |
|
5061 </pre> |
|
5062 <p id="ch9Using_Linux_Tools_19a"> |
|
5063 This directs the file list output to both standard output as well as to the file |
|
5064 <span class="emphasis"> |
|
5065 <em> |
|
5066 xyz |
|
5067 </em> |
|
5068 </span> |
|
5069 . |
|
5070 </p> |
|
5071 </div> |
|
5072 </div> |
|
5073 <div class="section" title="13.More Text Processing"> |
|
5074 <div class="titlepage"> |
|
5075 <div> |
|
5076 <div> |
|
5077 <h2 class="title" style="clear: both"> |
|
5078 <a name="id2629633"> |
|
5079 </a> |
|
5080 13.More Text Processing |
|
5081 </h2> |
|
5082 </div> |
|
5083 </div> |
|
5084 </div> |
|
5085 <div class="section" title="13.1.grep"> |
|
5086 <div class="titlepage"> |
|
5087 <div> |
|
5088 <div> |
|
5089 <h3 class="title"> |
|
5090 <a name="id2629642"> |
|
5091 </a> |
|
5092 13.1.grep |
|
5093 </h3> |
|
5094 </div> |
|
5095 </div> |
|
5096 </div> |
|
5097 <p id="ch9Using_Linux_Tools_19b"> |
|
5098 <span class="emphasis"> |
|
5099 <em> |
|
5100 grep |
|
5101 </em> |
|
5102 </span> |
|
5103 is a command line text search utility originally written for Unix. The name is taken from the first letters in |
|
5104 <span class="emphasis"> |
|
5105 <em> |
|
5106 global / regular expression / print |
|
5107 </em> |
|
5108 </span> |
|
5109 , a series of instructions for the |
|
5110 <span class="emphasis"> |
|
5111 <em> |
|
5112 ed |
|
5113 </em> |
|
5114 </span> |
|
5115 text editor. The |
|
5116 <span class="emphasis"> |
|
5117 <em> |
|
5118 grep |
|
5119 </em> |
|
5120 </span> |
|
5121 command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output. |
|
5122 </p> |
|
5123 <div class="section" title="13.1.1.Usage"> |
|
5124 <div class="titlepage"> |
|
5125 <div> |
|
5126 <div> |
|
5127 <h4 class="title"> |
|
5128 <a name="id2629673"> |
|
5129 </a> |
|
5130 13.1.1.Usage |
|
5131 </h4> |
|
5132 </div> |
|
5133 </div> |
|
5134 </div> |
|
5135 <p id="ch9Using_Linux_Tools_19c"> |
|
5136 This is an example of a common |
|
5137 <span class="emphasis"> |
|
5138 <em> |
|
5139 grep |
|
5140 </em> |
|
5141 </span> |
|
5142 usage: |
|
5143 </p> |
|
5144 <pre class="programlisting"> |
|
5145 $ grep apple fruitlist.txt |
|
5146 </pre> |
|
5147 <p id="ch9Using_Linux_Tools_19d"> |
|
5148 In this case, |
|
5149 <span class="emphasis"> |
|
5150 <em> |
|
5151 grep |
|
5152 </em> |
|
5153 </span> |
|
5154 prints all lines containing 'apple' from the file |
|
5155 <span class="emphasis"> |
|
5156 <em> |
|
5157 fruitlist.txt |
|
5158 </em> |
|
5159 </span> |
|
5160 , regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The |
|
5161 <span class="emphasis"> |
|
5162 <em> |
|
5163 grep |
|
5164 </em> |
|
5165 </span> |
|
5166 command is case sensitive by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'. |
|
5167 </p> |
|
5168 <p id="ch9Using_Linux_Tools_19e"> |
|
5169 Like most Unix commands, |
|
5170 <span class="emphasis"> |
|
5171 <em> |
|
5172 grep |
|
5173 </em> |
|
5174 </span> |
|
5175 accepts command line arguments to change this and many other behaviors. For example: |
|
5176 </p> |
|
5177 <pre class="programlisting"> |
|
5178 $ grep -i apple fruitlist.txt |
|
5179 </pre> |
|
5180 <p id="ch9Using_Linux_Tools_19f"> |
|
5181 This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells |
|
5182 <span class="emphasis"> |
|
5183 <em> |
|
5184 grep |
|
5185 </em> |
|
5186 </span> |
|
5187 to be case insensitive, or to ignore case. |
|
5188 </p> |
|
5189 <p id="ch9Using_Linux_Tools_1a0"> |
|
5190 To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match): |
|
5191 </p> |
|
5192 <pre class="programlisting"> |
|
5193 $ grep -w apple fruitlist.txt |
|
5194 </pre> |
|
5195 <p id="ch9Using_Linux_Tools_1a1"> |
|
5196 Regular expressions can be used to match more complicated queries. |
|
5197 </p> |
|
5198 <div class="section" title="13.1.1.1.Variations"> |
|
5199 <div class="titlepage"> |
|
5200 <div> |
|
5201 <div> |
|
5202 <h5 class="title"> |
|
5203 <a name="id2629751"> |
|
5204 </a> |
|
5205 13.1.1.1.Variations |
|
5206 </h5> |
|
5207 </div> |
|
5208 </div> |
|
5209 </div> |
|
5210 <p id="ch9Using_Linux_Tools_1a2"> |
|
5211 There are countless implementations and derivatives of |
|
5212 <span class="emphasis"> |
|
5213 <em> |
|
5214 grep |
|
5215 </em> |
|
5216 </span> |
|
5217 available for many operating systems. Early variants of |
|
5218 <span class="emphasis"> |
|
5219 <em> |
|
5220 grep |
|
5221 </em> |
|
5222 </span> |
|
5223 included |
|
5224 <span class="emphasis"> |
|
5225 <em> |
|
5226 egrep |
|
5227 </em> |
|
5228 </span> |
|
5229 and |
|
5230 <span class="emphasis"> |
|
5231 <em> |
|
5232 fgrep |
|
5233 </em> |
|
5234 </span> |
|
5235 . The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern |
|
5236 <span class="emphasis"> |
|
5237 <em> |
|
5238 grep |
|
5239 </em> |
|
5240 </span> |
|
5241 implementations as command-line switches (and standardized as -E and -F in POSIX). In such combined implementations, |
|
5242 <span class="emphasis"> |
|
5243 <em> |
|
5244 grep |
|
5245 </em> |
|
5246 </span> |
|
5247 may also behave differently depending on the name by which it is invoked, allowing |
|
5248 <span class="emphasis"> |
|
5249 <em> |
|
5250 fgrep |
|
5251 </em> |
|
5252 </span> |
|
5253 , |
|
5254 <span class="emphasis"> |
|
5255 <em> |
|
5256 egrep |
|
5257 </em> |
|
5258 </span> |
|
5259 , and |
|
5260 <span class="emphasis"> |
|
5261 <em> |
|
5262 grep |
|
5263 </em> |
|
5264 </span> |
|
5265 to be links to the same program. |
|
5266 </p> |
|
5267 <p id="ch9Using_Linux_Tools_1a3"> |
|
5268 <span class="emphasis"> |
|
5269 <em> |
|
5270 pcregrep |
|
5271 </em> |
|
5272 </span> |
|
5273 is an implementation of |
|
5274 <span class="emphasis"> |
|
5275 <em> |
|
5276 grep |
|
5277 </em> |
|
5278 </span> |
|
5279 that uses Perl regular expression syntax. |
|
5280 </p> |
|
5281 <p id="ch9Using_Linux_Tools_1a4"> |
|
5282 Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The |
|
5283 <span class="emphasis"> |
|
5284 <em> |
|
5285 pgrep |
|
5286 </em> |
|
5287 </span> |
|
5288 utility, for instance, displays the processes whose names match a given regular expression. |
|
5289 </p> |
|
5290 </div> |
|
5291 </div> |
|
5292 </div> |
|
5293 <div class="section" title="13.2.tr"> |
|
5294 <div class="titlepage"> |
|
5295 <div> |
|
5296 <div> |
|
5297 <h3 class="title"> |
|
5298 <a name="id2629831"> |
|
5299 </a> |
|
5300 13.2.tr |
|
5301 </h3> |
|
5302 </div> |
|
5303 </div> |
|
5304 </div> |
|
5305 <p id="ch9Using_Linux_Tools_1a5"> |
|
5306 <span class="emphasis"> |
|
5307 <em> |
|
5308 tr |
|
5309 </em> |
|
5310 </span> |
|
5311 (abbreviated from |
|
5312 <span class="emphasis"> |
|
5313 <em> |
|
5314 translate |
|
5315 </em> |
|
5316 </span> |
|
5317 or |
|
5318 <span class="emphasis"> |
|
5319 <em> |
|
5320 transliterate |
|
5321 </em> |
|
5322 </span> |
|
5323 ) is a command in Unix-like operating systems. |
|
5324 </p> |
|
5325 <p id="ch9Using_Linux_Tools_1a6"> |
|
5326 When executed, the program reads from the standard input and writes to the standard output. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set. For example, |
|
5327 </p> |
|
5328 <pre class="programlisting"> |
|
5329 $ tr 'abcd' 'jkmn' |
|
5330 </pre> |
|
5331 <p id="ch9Using_Linux_Tools_1a7"> |
|
5332 maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'. |
|
5333 </p> |
|
5334 <p id="ch9Using_Linux_Tools_1a8"> |
|
5335 Sets of characters may be abbreviated by using character ranges. The previous example could be written: |
|
5336 </p> |
|
5337 <pre class="programlisting"> |
|
5338 $ tr 'a-d' 'jkmn' |
|
5339 </pre> |
|
5340 <p id="ch9Using_Linux_Tools_1a9"> |
|
5341 In POSIX compliant versions of |
|
5342 <span class="emphasis"> |
|
5343 <em> |
|
5344 tr |
|
5345 </em> |
|
5346 </span> |
|
5347 the set represented by a character range depends on the locale's collating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced with POSIX character sets such as [:alpha:]. |
|
5348 </p> |
|
5349 <p id="ch9Using_Linux_Tools_1aa"> |
|
5350 The |
|
5351 <span class="emphasis"> |
|
5352 <em> |
|
5353 -c |
|
5354 </em> |
|
5355 </span> |
|
5356 flag complements the first set of characters. |
|
5357 </p> |
|
5358 <pre class="programlisting"> |
|
5359 $ tr -cd '[:alnum:]' |
|
5360 </pre> |
|
5361 <p id="ch9Using_Linux_Tools_1ab"> |
|
5362 therefore removes all non-alphanumeric characters. |
|
5363 </p> |
|
5364 <p id="ch9Using_Linux_Tools_1ac"> |
|
5365 The |
|
5366 <span class="emphasis"> |
|
5367 <em> |
|
5368 -s |
|
5369 </em> |
|
5370 </span> |
|
5371 flag causes tr to compress sequences of identical adjacent characters in its output to a single token. For example, |
|
5372 </p> |
|
5373 <pre class="programlisting"> |
|
5374 $ tr -s ' n' ' n' |
|
5375 </pre> |
|
5376 <p id="ch9Using_Linux_Tools_1ad"> |
|
5377 replaces sequences of one or more newline characters with a single newline. |
|
5378 </p> |
|
5379 <p id="ch9Using_Linux_Tools_1ae"> |
|
5380 The |
|
5381 <span class="emphasis"> |
|
5382 <em> |
|
5383 -d |
|
5384 </em> |
|
5385 </span> |
|
5386 flag causes tr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters, thereby converting a file in DOS/Windows format to one in Unix format. |
|
5387 </p> |
|
5388 <pre class="programlisting"> |
|
5389 $ tr -d ' r' |
|
5390 </pre> |
|
5391 <p id="ch9Using_Linux_Tools_1af"> |
|
5392 Most versions of |
|
5393 <span class="emphasis"> |
|
5394 <em> |
|
5395 tr |
|
5396 </em> |
|
5397 </span> |
|
5398 , including GNU |
|
5399 <span class="emphasis"> |
|
5400 <em> |
|
5401 tr |
|
5402 </em> |
|
5403 </span> |
|
5404 and classic Unix |
|
5405 <span class="emphasis"> |
|
5406 <em> |
|
5407 tr |
|
5408 </em> |
|
5409 </span> |
|
5410 , operate on single byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support. |
|
5411 </p> |
|
5412 <p id="ch9Using_Linux_Tools_1b0"> |
|
5413 Ruby and Perl also have an internal |
|
5414 <span class="emphasis"> |
|
5415 <em> |
|
5416 tr |
|
5417 </em> |
|
5418 </span> |
|
5419 operator, which operates analogously. Tcl's |
|
5420 <span class="emphasis"> |
|
5421 <em> |
|
5422 string map |
|
5423 </em> |
|
5424 </span> |
|
5425 command is more general in that it maps strings to strings while |
|
5426 <span class="emphasis"> |
|
5427 <em> |
|
5428 tr |
|
5429 </em> |
|
5430 </span> |
|
5431 maps characters to characters. |
|
5432 </p> |
|
5433 </div> |
|
5434 </div> |
|
5435 <div class="section" title="14.Elementary Regex"> |
|
5436 <div class="titlepage"> |
|
5437 <div> |
|
5438 <div> |
|
5439 <h2 class="title" style="clear: both"> |
|
5440 <a name="id2629978"> |
|
5441 </a> |
|
5442 14.Elementary Regex |
|
5443 </h2> |
|
5444 </div> |
|
5445 </div> |
|
5446 </div> |
|
5447 <p id="ch9Using_Linux_Tools_1b1"> |
|
5448 In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. A regular expression (often shortened to regex or regexp) is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. |
|
5449 </p> |
|
5450 <p id="ch9Using_Linux_Tools_1b2"> |
|
5451 Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, Perl, Ruby and Tcl have a powerful regular expression engine built directly into their syntax. Several utilities provided by Unix distributionsincluding the editor |
|
5452 <span class="emphasis"> |
|
5453 <em> |
|
5454 ed |
|
5455 </em> |
|
5456 </span> |
|
5457 and the filter |
|
5458 <span class="emphasis"> |
|
5459 <em> |
|
5460 grep |
|
5461 </em> |
|
5462 </span> |
|
5463 were the first to popularize the concept of regular expressions. |
|
5464 </p> |
|
5465 <p id="ch9Using_Linux_Tools_1b3"> |
|
5466 Traditional Unix regular expression syntax followed common conventions but often differed from tool to tool. The IEEE POSIX |
|
5467 <span class="emphasis"> |
|
5468 <em> |
|
5469 Basic Regular Expressions |
|
5470 </em> |
|
5471 </span> |
|
5472 (BRE) standard (released alongside an alternative flavor called Extended Regular Expressions or ERE) was designed mostly for backward compatibility with the traditional (Simple Regular Expression) syntax but provided a common standard which has since been adopted as the default syntax of many Unix regular expression tools, though there is often some variation or additional features. Many such tools also provide support for ERE syntax with command line arguments. |
|
5473 </p> |
|
5474 <p id="ch9Using_Linux_Tools_1b4"> |
|
5475 In the BRE syntax, most characters are treated as literals they match only themselves (i.e., a matches "a"). The exceptions, listed below, are called metacharacters or metasequences. |
|
5476 </p> |
|
5477 <p id="ch9Using_Linux_Tools_1b5"> |
|
5478 Metacharacter |
|
5479 </p> |
|
5480 <p id="ch9Using_Linux_Tools_1b6"> |
|
5481 Description |
|
5482 </p> |
|
5483 <p id="ch9Using_Linux_Tools_1b7"> |
|
5484 . |
|
5485 </p> |
|
5486 <p id="ch9Using_Linux_Tools_1b8"> |
|
5487 Matches any single character (many applications exclude |
1326 newlines, and exactly which characters are considered |
5488 newlines, and exactly which characters are considered |
1327 newlines is flavor, character encoding, and platform |
5489 newlines is flavor, character encoding, and platform |
1328 specific, but it is safe to assume that the line feed |
5490 specific, but it is safe to assume that the line feed |
1329 character is included). Within POSIX bracket expressions, |
5491 character is included). Within POSIX bracket expressions, |
1330 the dot character matches a literal dot. For example, a.c |
5492 the dot character matches a literal dot. For example, a.c |
1331 matches abc, etc., but [a.c] matches only a, ., or |
5493 matches abc, etc., but [a.c] matches only a, ., or |
1332 c.</p> |
5494 c. |
1333 <p><a name="ch9Using_Linux_Tools_1b9"></a>[ ]</p> |
5495 </p> |
1334 <p><a name="ch9Using_Linux_Tools_1ba"></a>A bracket expression. Matches a single character that is |
5496 <p id="ch9Using_Linux_Tools_1b9"> |
|
5497 [ ] |
|
5498 </p> |
|
5499 <p id="ch9Using_Linux_Tools_1ba"> |
|
5500 A bracket expression. Matches a single character that is |
1335 contained within the brackets. For example, [abc] matches |
5501 contained within the brackets. For example, [abc] matches |
1336 a, b, or c. [a-z] specifies a range which matches any |
5502 a, b, or c. [a-z] specifies a range which matches any |
1337 lowercase letter from a to z. These forms can be mixed: |
5503 lowercase letter from a to z. These forms can be mixed: |
1338 [abcx-z] matches a, b, c, x, y, or z, as does |
5504 [abcx-z] matches a, b, c, x, y, or z, as does |
1339 [a-cx-z]. The - character is treated as a literal character |
5505 [a-cx-z]. The - character is treated as a literal character |
1340 if it is the last or the first character within the |
5506 if it is the last or the first character within the |
1341 brackets, or if it is escaped with a backslash: [abc-], |
5507 brackets, or if it is escaped with a backslash: [abc-], |
1342 [-abc], or [a-bc].</p> |
5508 [-abc], or [a-bc]. |
1343 <p><a name="ch9Using_Linux_Tools_1bb"></a>[^ ]</p> |
5509 </p> |
1344 <p><a name="ch9Using_Linux_Tools_1bc"></a>Matches a single character that is not contained within the |
5510 <p id="ch9Using_Linux_Tools_1bb"> |
|
5511 [^ ] |
|
5512 </p> |
|
5513 <p id="ch9Using_Linux_Tools_1bc"> |
|
5514 Matches a single character that is not contained within the |
1345 brackets. For example, [^abc] matches any character other |
5515 brackets. For example, [^abc] matches any character other |
1346 than a, b, or c. [^a-z] matches any single character |
5516 than a, b, or c. [^a-z] matches any single character |
1347 that is not a lowercase letter from a to z. As above, |
5517 that is not a lowercase letter from a to z. As above, |
1348 literal characters and ranges can be mixed.</p> |
5518 literal characters and ranges can be mixed. |
1349 <p><a name="ch9Using_Linux_Tools_1bd"></a>^</p> |
5519 </p> |
1350 <p><a name="ch9Using_Linux_Tools_1be"></a>Matches the starting position within the string. In |
5520 <p id="ch9Using_Linux_Tools_1bd"> |
|
5521 ^ |
|
5522 </p> |
|
5523 <p id="ch9Using_Linux_Tools_1be"> |
|
5524 Matches the starting position within the string. In |
1351 line-based tools, it matches the starting position of any |
5525 line-based tools, it matches the starting position of any |
1352 line.</p> |
5526 line. |
1353 <p><a name="ch9Using_Linux_Tools_1bf"></a>$</p> |
5527 </p> |
1354 <p><a name="ch9Using_Linux_Tools_1c0"></a>Matches the ending position of the string or the position |
5528 <p id="ch9Using_Linux_Tools_1bf"> |
|
5529 $ |
|
5530 </p> |
|
5531 <p id="ch9Using_Linux_Tools_1c0"> |
|
5532 Matches the ending position of the string or the position |
1355 just before a string-ending newline. In line-based tools, |
5533 just before a string-ending newline. In line-based tools, |
1356 it matches the ending position of any line.</p> |
5534 it matches the ending position of any line. |
1357 <p><a name="ch9Using_Linux_Tools_1c1"></a>*</p> |
5535 </p> |
1358 <p><a name="ch9Using_Linux_Tools_1c2"></a>Matches the preceding element zero or more times. For |
5536 <p id="ch9Using_Linux_Tools_1c1"> |
|
5537 * |
|
5538 </p> |
|
5539 <p id="ch9Using_Linux_Tools_1c2"> |
|
5540 Matches the preceding element zero or more times. For |
1359 example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* |
5541 example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]* |
1360 matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. |
5542 matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on. |
1361 (ab)* matches "", "ab", "abab", "ababab", and so on.</p> |
5543 (ab)* matches "", "ab", "abab", "ababab", and so on. |
1362 <p><a name="ch9Using_Linux_Tools_1c3"></a>?</p> |
5544 </p> |
1363 <p><a name="ch9Using_Linux_Tools_1c4"></a>Matches the preceding element zero or one time. For |
5545 <p id="ch9Using_Linux_Tools_1c3"> |
1364 example, ba? matches "b" or "ba".</p> |
5546 ? |
1365 <p><a name="ch9Using_Linux_Tools_1c5"></a>+</p> |
5547 </p> |
1366 <p><a name="ch9Using_Linux_Tools_1c6"></a>Matches the preceding element one or more times. For |
5548 <p id="ch9Using_Linux_Tools_1c4"> |
1367 example, ba+ matches "ba", "baa", "baaa", and so on.</p> |
5549 Matches the preceding element zero or one time. For |
1368 <p><a name="ch9Using_Linux_Tools_1c7"></a>|</p> |
5550 example, ba? matches "b" or "ba". |
1369 <p><a name="ch9Using_Linux_Tools_1c8"></a>The choice (aka alternation or set union) operator matches |
5551 </p> |
|
5552 <p id="ch9Using_Linux_Tools_1c5"> |
|
5553 + |
|
5554 </p> |
|
5555 <p id="ch9Using_Linux_Tools_1c6"> |
|
5556 Matches the preceding element one or more times. For |
|
5557 example, ba+ matches "ba", "baa", "baaa", and so on. |
|
5558 </p> |
|
5559 <p id="ch9Using_Linux_Tools_1c7"> |
|
5560 | |
|
5561 </p> |
|
5562 <p id="ch9Using_Linux_Tools_1c8"> |
|
5563 The choice (aka alternation or set union) operator matches |
1370 either the expression before or the expression after the |
5564 either the expression before or the expression after the |
1371 operator. For example, abc|def matches "abc" or "def".</p> |
5565 operator. For example, abc|def matches "abc" or "def". |
1372 <div class="section" title="14.1. Lazy quantification"> |
5566 </p> |
1373 <div class="titlepage"><div><div><h3 class="title"> |
5567 <div class="section" title="14.1.Lazy quantification"> |
1374 <a name="id2951511"></a>14.1. Lazy quantification</h3></div></div></div> |
5568 <div class="titlepage"> |
1375 <p><a name="ch9Using_Linux_Tools_1c9"></a>The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex. For example, someone new to regexes wishing to find the first instance of an item between < and > symbols in this example:</p> |
5569 <div> |
1376 <pre class="programlisting"> |
5570 <div> |
1377 Another whale explosion occurred on <January 26>, <2004>.</pre> |
5571 <h3 class="title"> |
1378 <p><a name="ch9Using_Linux_Tools_1ca"></a>...would likely come up with the pattern <.*>, or similar. However, this pattern will actually return "<January 26>, <2004>" instead of the "<January 26>" which might be expected, because the * quantifier is greedy — it will consume as many characters as possible from the input, and "January 26>, <2004" has more characters than "January 26".</p> |
5572 <a name="id2630180"> |
1379 <p><a name="ch9Using_Linux_Tools_1cb"></a>Though this problem can be avoided in a number of ways (e.g., by specifying the text that is not to be matched: <[^>]*>), modern regular expression tools allow a quantifier to be specified as <span class="emphasis"><em>lazy</em></span> (also known as non-greedy, reluctant, minimal, or ungreedy) by putting a question mark after the quantifier (e.g., <.*?>), or by using a modifier which reverses the greediness of quantifiers (though changing the meaning of the standard quantifiers can be confusing). By using a lazy quantifier, the expression tries the minimal match first. Though in the previous example lazy matching is used to select one of many matching results, in some cases it can also be used to improve performance when greedy matching would require more backtracking.</p> |
5573 </a> |
1380 </div> |
5574 14.1.Lazy quantification |
1381 </div> |
5575 </h3> |
1382 <div class="section" title="15. One Liners"> |
5576 </div> |
1383 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
5577 </div> |
1384 <a name="id2951586"></a>15. One Liners</h2></div></div></div> |
5578 </div> |
1385 <p><a name="ch9Using_Linux_Tools_1cc"></a>A <span class="emphasis"><em>one-liner</em></span> is textual input to the command-line of an operating system shell that performs some function in just one line of input.</p> |
5579 <p id="ch9Using_Linux_Tools_1c9"> |
1386 <p><a name="ch9Using_Linux_Tools_1cd"></a>The one liner can be</p> |
5580 The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex. For example, someone new to regexes wishing to find the first instance of an item between < and > symbols in this example: |
1387 <div class="orderedlist"><ol class="orderedlist" type="1"> |
5581 </p> |
1388 <li class="listitem"><p><a name="ch9Using_Linux_Tools_1ce"></a>An expression written in the language of the shell.</p></li> |
5582 <pre class="programlisting"> |
1389 <li class="listitem"><p><a name="ch9Using_Linux_Tools_1cf"></a>The invocation of an interpreter together with program source for the interpreter to run.</p></li> |
5583 Another whale explosion occurred on <January 26>, <2004>. |
1390 <li class="listitem"><p><a name="ch9Using_Linux_Tools_1d0"></a>The invocation of a compiler together with source to compile and |
5584 </pre> |
1391 instructions for executing the compiled program.</p></li> |
5585 <p id="ch9Using_Linux_Tools_1ca"> |
1392 </ol></div> |
5586 ...would likely come up with the pattern <.*>, or similar. However, this pattern will actually return "<January 26>, <2004>" instead of the "<January 26>" which might be expected, because the * quantifier is greedy it will consume as many characters as possible from the input, and "January 26>, <2004" has more characters than "January 26". |
1393 <p><a name="ch9Using_Linux_Tools_1d1"></a>Certain dynamic scripting languages such as AWK, sed, and perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as these Unix shells or the Windows PowerShell, allow for the construction of powerful one-liners.</p> |
5587 </p> |
1394 <p><a name="ch9Using_Linux_Tools_1d2"></a>The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.</p> |
5588 <p id="ch9Using_Linux_Tools_1cb"> |
1395 <p><a name="ch9Using_Linux_Tools_1d3"></a>The word <span class="emphasis"><em>One-liner</em></span> has two references in the index of the book <span class="emphasis"><em>The AWK Programming Language</em></span> (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the One-liner paradigm with their daily work on early Unix machines:</p> |
5589 Though this problem can be avoided in a number of ways (e.g., by specifying the text that is not to be matched: <[^>]*>), modern regular expression tools allow a quantifier to be specified as |
1396 <pre class="programlisting"> |
5590 <span class="emphasis"> |
1397 “The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs [...] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style [...] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners.”</pre> |
5591 <em> |
1398 <p><a name="ch9Using_Linux_Tools_1d4"></a>Notice that this original definition of a One-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a One-liner. But this strict understanding of a One-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language.</p> |
5592 lazy |
1399 <p><a name="ch9Using_Linux_Tools_1d5"></a>The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter.</p> |
5593 </em> |
1400 <p><a name="ch9Using_Linux_Tools_1d6"></a>Here are the first few of them:</p> |
5594 </span> |
1401 <div class="orderedlist"><ol class="orderedlist" type="1"> |
5595 (also known as non-greedy, reluctant, minimal, or ungreedy) by putting a question mark after the quantifier (e.g., <.*?>), or by using a modifier which reverses the greediness of quantifiers (though changing the meaning of the standard quantifiers can be confusing). By using a lazy quantifier, the expression tries the minimal match first. Though in the previous example lazy matching is used to select one of many matching results, in some cases it can also be used to improve performance when greedy matching would require more backtracking. |
1402 <li class="listitem"> |
5596 </p> |
1403 <p><a name="ch9Using_Linux_Tools_1d7"></a>Print the total number of input lines:</p> |
5597 </div> |
1404 <p><a name="ch9Using_Linux_Tools_1d8"></a>END { print NR }</p> |
5598 </div> |
1405 </li> |
5599 <div class="section" title="15.One Liners"> |
1406 <li class="listitem"> |
5600 <div class="titlepage"> |
1407 <p><a name="ch9Using_Linux_Tools_1d9"></a>Print the tenth input line:</p> |
5601 <div> |
1408 <p><a name="ch9Using_Linux_Tools_1da"></a>NR == 10</p> |
5602 <div> |
1409 </li> |
5603 <h2 class="title" style="clear: both"> |
1410 <li class="listitem"> |
5604 <a name="id2630255"> |
1411 <p><a name="ch9Using_Linux_Tools_1db"></a>Print the last field of every input line:</p> |
5605 </a> |
1412 <p><a name="ch9Using_Linux_Tools_1dc"></a>{ print $NF }</p> |
5606 15.One Liners |
1413 </li> |
5607 </h2> |
1414 </ol></div> |
5608 </div> |
1415 <p><a name="ch9Using_Linux_Tools_1dd"></a>One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.</p> |
5609 </div> |
1416 <p><a name="ch9Using_Linux_Tools_1de"></a>The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).:</p> |
5610 </div> |
1417 <pre class="programlisting"> |
5611 <p id="ch9Using_Linux_Tools_1cc"> |
1418 main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return |
5612 A |
1419 *t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}</pre> |
5613 <span class="emphasis"> |
1420 <p><a name="ch9Using_Linux_Tools_1df"></a>This one-liner program is a <span class="emphasis"><em>glob pattern matcher</em></span>. It understands the glob characters '*' meaning 'zero or more characters' and '?' meaning exactly one character, just like most Unix shells.</p> |
5614 <em> |
1421 <p><a name="ch9Using_Linux_Tools_1e0"></a>Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples:</p> |
5615 one-liner |
1422 <pre class="programlisting"> |
5616 </em> |
1423 $ prog foo 'f??'; echo $? |
5617 </span> |
|
5618 is textual input to the command-line of an operating system shell that performs some function in just one line of input. |
|
5619 </p> |
|
5620 <p id="ch9Using_Linux_Tools_1cd"> |
|
5621 The one liner can be |
|
5622 </p> |
|
5623 <div class="orderedlist"> |
|
5624 <ol class="orderedlist" type="1"> |
|
5625 <li class="listitem"> |
|
5626 <p id="ch9Using_Linux_Tools_1ce"> |
|
5627 An expression written in the language of the shell. |
|
5628 </p> |
|
5629 </li> |
|
5630 <li class="listitem"> |
|
5631 <p id="ch9Using_Linux_Tools_1cf"> |
|
5632 The invocation of an interpreter together with program source for the interpreter to run. |
|
5633 </p> |
|
5634 </li> |
|
5635 <li class="listitem"> |
|
5636 <p id="ch9Using_Linux_Tools_1d0"> |
|
5637 The invocation of a compiler together with source to compile and |
|
5638 instructions for executing the compiled program. |
|
5639 </p> |
|
5640 </li> |
|
5641 </ol> |
|
5642 </div> |
|
5643 <p id="ch9Using_Linux_Tools_1d1"> |
|
5644 Certain dynamic scripting languages such as AWK, sed, and perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as these Unix shells or the Windows PowerShell, allow for the construction of powerful one-liners. |
|
5645 </p> |
|
5646 <p id="ch9Using_Linux_Tools_1d2"> |
|
5647 The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line. |
|
5648 </p> |
|
5649 <p id="ch9Using_Linux_Tools_1d3"> |
|
5650 The word |
|
5651 <span class="emphasis"> |
|
5652 <em> |
|
5653 One-liner |
|
5654 </em> |
|
5655 </span> |
|
5656 has two references in the index of the book |
|
5657 <span class="emphasis"> |
|
5658 <em> |
|
5659 The AWK Programming Language |
|
5660 </em> |
|
5661 </span> |
|
5662 (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the One-liner paradigm with their daily work on early Unix machines: |
|
5663 </p> |
|
5664 <pre class="programlisting"> |
|
5665 The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs [...] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style [...] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners. |
|
5666 </pre> |
|
5667 <p id="ch9Using_Linux_Tools_1d4"> |
|
5668 Notice that this original definition of a One-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a One-liner. But this strict understanding of a One-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language. |
|
5669 </p> |
|
5670 <p id="ch9Using_Linux_Tools_1d5"> |
|
5671 The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter. |
|
5672 </p> |
|
5673 <p id="ch9Using_Linux_Tools_1d6"> |
|
5674 Here are the first few of them: |
|
5675 </p> |
|
5676 <div class="orderedlist"> |
|
5677 <ol class="orderedlist" type="1"> |
|
5678 <li class="listitem"> |
|
5679 <p id="ch9Using_Linux_Tools_1d7"> |
|
5680 Print the total number of input lines: |
|
5681 </p> |
|
5682 <p id="ch9Using_Linux_Tools_1d8"> |
|
5683 END { print NR } |
|
5684 </p> |
|
5685 </li> |
|
5686 <li class="listitem"> |
|
5687 <p id="ch9Using_Linux_Tools_1d9"> |
|
5688 Print the tenth input line: |
|
5689 </p> |
|
5690 <p id="ch9Using_Linux_Tools_1da"> |
|
5691 NR == 10 |
|
5692 </p> |
|
5693 </li> |
|
5694 <li class="listitem"> |
|
5695 <p id="ch9Using_Linux_Tools_1db"> |
|
5696 Print the last field of every input line: |
|
5697 </p> |
|
5698 <p id="ch9Using_Linux_Tools_1dc"> |
|
5699 { print $NF } |
|
5700 </p> |
|
5701 </li> |
|
5702 </ol> |
|
5703 </div> |
|
5704 <p id="ch9Using_Linux_Tools_1dd"> |
|
5705 One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner. |
|
5706 </p> |
|
5707 <p id="ch9Using_Linux_Tools_1de"> |
|
5708 The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).: |
|
5709 </p> |
|
5710 <pre class="programlisting"> |
|
5711 main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return |
|
5712 *t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);} |
|
5713 </pre> |
|
5714 <p id="ch9Using_Linux_Tools_1df"> |
|
5715 This one-liner program is a |
|
5716 <span class="emphasis"> |
|
5717 <em> |
|
5718 glob pattern matcher |
|
5719 </em> |
|
5720 </span> |
|
5721 . It understands the glob characters '*' meaning 'zero or more characters' and '?' meaning exactly one character, just like most Unix shells. |
|
5722 </p> |
|
5723 <p id="ch9Using_Linux_Tools_1e0"> |
|
5724 Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples: |
|
5725 </p> |
|
5726 <pre class="programlisting"> |
|
5727 $ prog foo 'f??'; echo $? |
1424 |
5728 |
1425 $ prog 'best short program' '??st*o**p?*'; echo $?</pre> |
5729 $ prog 'best short program' '??st*o**p?*'; echo $? |
1426 <p><a name="ch9Using_Linux_Tools_1e1"></a>Here is a one line shell script to show directories:</p> |
5730 </pre> |
1427 <pre class="programlisting"> |
5731 <p id="ch9Using_Linux_Tools_1e1"> |
1428 $ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^ /]* //--/g' -e 's/^/ /' -e 's/-/|/'</pre> |
5732 Here is a one line shell script to show directories: |
1429 </div> |
5733 </p> |
1430 </div> |
5734 <pre class="programlisting"> |
1431 </div></body> |
5735 $ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^ /]* //--/g' -e 's/^/ /' -e 's/-/|/' |
|
5736 </pre> |
|
5737 </div> |
|
5738 </div> |
|
5739 </div> |
|
5740 </body> |
1432 </html> |
5741 </html> |