|
1 <html> |
|
2 <head> |
|
3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|
4 <title>List and Tuples </title> |
|
5 <link rel="shortcut icon" type="image/png" href="/review/support/figs/favicon.png"> |
|
6 <script type="text/javascript" src="/review/support/jquery-min.js"></script> |
|
7 <script type="text/javascript" src="/review/support/form.js"></script> |
|
8 <script type="text/javascript" src="/review/support/hsbook.js"></script> |
|
9 <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> |
|
10 </head> |
|
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" id="ch3list_tuples"> |
|
12 <div class="titlepage"></div> |
|
13 <div class="toc"> |
|
14 <p><b>Table of Contents</b></p> |
|
15 <dl> |
|
16 <dt><span class="article"><a href="#id2689198"></a></span></dt> |
|
17 <dd><dl> |
|
18 <dt><span class="section"><a href="#id2499323">1. Introducing Linux</a></span></dt> |
|
19 <dd><dl> |
|
20 <dt><span class="section"><a href="#id2687018">1.1. Historical Background</a></span></dt> |
|
21 <dt><span class="section"><a href="#id2740259">1.2. Design and Implications</a></span></dt> |
|
22 <dt><span class="section"><a href="#id2740531">1.3. Reasons for Using Linux</a></span></dt> |
|
23 </dl></dd> |
|
24 <dt><span class="section"><a href="#id2740663">2. Getting Started</a></span></dt> |
|
25 <dd><dl><dt><span class="section"><a href="#id2740671">2.1. Logging in, activating the user interface and logging out</a></span></dt></dl></dd> |
|
26 <dt><span class="section"><a href="#id2740949">3. Basic Commands</a></span></dt> |
|
27 <dd><dl> |
|
28 <dt><span class="section"><a href="#id2740958">3.1. ls</a></span></dt> |
|
29 <dt><span class="section"><a href="#id2741120">3.2. date</a></span></dt> |
|
30 <dt><span class="section"><a href="#id2741180">3.3. cd</a></span></dt> |
|
31 <dt><span class="section"><a href="#id2741282">3.4. who</a></span></dt> |
|
32 <dt><span class="section"><a href="#id2741364">3.5. mkdir</a></span></dt> |
|
33 </dl></dd> |
|
34 <dt><span class="section"><a href="#id2741543">4. Getting Help</a></span></dt> |
|
35 <dd><dl> |
|
36 <dt><span class="section"><a href="#id2741551">4.1. apropos and whatis</a></span></dt> |
|
37 <dt><span class="section"><a href="#id2740051">4.2. man</a></span></dt> |
|
38 <dt><span class="section"><a href="#id2741979">4.3. info</a></span></dt> |
|
39 <dt><span class="section"><a href="#id2742070">4.4. --help</a></span></dt> |
|
40 </dl></dd> |
|
41 <dt><span class="section"><a href="#id2742122">5. Basic file handling</a></span></dt> |
|
42 <dd><dl> |
|
43 <dt><span class="section"><a href="#id2742131">5.1. cp</a></span></dt> |
|
44 <dt><span class="section"><a href="#id2742442">5.2. mv</a></span></dt> |
|
45 <dt><span class="section"><a href="#id2742574">5.3. rm</a></span></dt> |
|
46 </dl></dd> |
|
47 <dt><span class="section"><a href="#id2742744">6. Command Line Arguments</a></span></dt> |
|
48 <dt><span class="section"><a href="#id2742837">7. Basic Text Processing</a></span></dt> |
|
49 <dd><dl> |
|
50 <dt><span class="section"><a href="#id2742845">7.1. head</a></span></dt> |
|
51 <dt><span class="section"><a href="#id2742932">7.2. tail</a></span></dt> |
|
52 <dt><span class="section"><a href="#id2743058">7.3. cut</a></span></dt> |
|
53 <dt><span class="section"><a href="#id2743144">7.4. paste</a></span></dt> |
|
54 </dl></dd> |
|
55 <dt><span class="section"><a href="#id2743276">8. Shell Meta Characters</a></span></dt> |
|
56 <dt><span class="section"><a href="#id2743383">9. Looking At Files</a></span></dt> |
|
57 <dd><dl> |
|
58 <dt><span class="section"><a href="#id2743392">9.1. cat</a></span></dt> |
|
59 <dt><span class="section"><a href="#id2743578">9.2. more</a></span></dt> |
|
60 <dt><span class="section"><a href="#id2743688">9.3. less</a></span></dt> |
|
61 </dl></dd> |
|
62 <dt><span class="section"><a href="#id2743929">10. Directory Structure</a></span></dt> |
|
63 <dd><dl> |
|
64 <dt><span class="section"><a href="#id2744198">10.1. man hier</a></span></dt> |
|
65 <dt><span class="section"><a href="#id2744214">10.2. ls -l</a></span></dt> |
|
66 </dl></dd> |
|
67 <dt><span class="section"><a href="#id2744237">11. Permissions and Ownership</a></span></dt> |
|
68 <dd><dl><dt><span class="section"><a href="#id2744246">11.1. chmod</a></span></dt></dl></dd> |
|
69 <dt><span class="section"><a href="#id2745018">12. Redirection and Piping</a></span></dt> |
|
70 <dd><dl> |
|
71 <dt><span class="section"><a href="#id2745055">12.1. Redirecting standard input and standard output</a></span></dt> |
|
72 <dt><span class="section"><a href="#id2745124">12.2. Piping</a></span></dt> |
|
73 <dt><span class="section"><a href="#id2745186">12.3. Redirecting to and from the standard file handles</a></span></dt> |
|
74 <dt><span class="section"><a href="#id2745390">12.4. Chained pipelines</a></span></dt> |
|
75 <dt><span class="section"><a href="#id2745424">12.5. Redirect to multiple outputs</a></span></dt> |
|
76 </dl></dd> |
|
77 <dt><span class="section"><a href="#id2745454">13. More Text Processing</a></span></dt> |
|
78 <dd><dl> |
|
79 <dt><span class="section"><a href="#id2745463">13.1. grep</a></span></dt> |
|
80 <dt><span class="section"><a href="#id2745652">13.2. tr</a></span></dt> |
|
81 </dl></dd> |
|
82 <dt><span class="section"><a href="#id2745806">14. Elementary Regex</a></span></dt> |
|
83 <dd><dl><dt><span class="section"><a href="#id2746019">14.1. Lazy quantification</a></span></dt></dl></dd> |
|
84 <dt><span class="section"><a href="#id2746095">15. One Liners</a></span></dt> |
|
85 </dl></dd> |
|
86 </dl> |
|
87 </div> |
|
88 <div class="article"> |
|
89 <div class="titlepage"> |
|
90 <div><div><h2 class="title" id="id2689198"></a></h2></div></div> |
|
91 <hr> |
|
92 </div> |
|
93 <div class="toc"> |
|
94 <p><b>Table of Contents</b></p> |
|
95 <dl> |
|
96 <dt><span class="section"><a href="#id2499323">1. Introducing Linux</a></span></dt> |
|
97 <dd><dl> |
|
98 <dt><span class="section"><a href="#id2687018">1.1. Historical Background</a></span></dt> |
|
99 <dt><span class="section"><a href="#id2740259">1.2. Design and Implications</a></span></dt> |
|
100 <dt><span class="section"><a href="#id2740531">1.3. Reasons for Using Linux</a></span></dt> |
|
101 </dl></dd> |
|
102 <dt><span class="section"><a href="#id2740663">2. Getting Started</a></span></dt> |
|
103 <dd><dl><dt><span class="section"><a href="#id2740671">2.1. Logging in, activating the user interface and logging out</a></span></dt></dl></dd> |
|
104 <dt><span class="section"><a href="#id2740949">3. Basic Commands</a></span></dt> |
|
105 <dd><dl> |
|
106 <dt><span class="section"><a href="#id2740958">3.1. ls</a></span></dt> |
|
107 <dt><span class="section"><a href="#id2741120">3.2. date</a></span></dt> |
|
108 <dt><span class="section"><a href="#id2741180">3.3. cd</a></span></dt> |
|
109 <dt><span class="section"><a href="#id2741282">3.4. who</a></span></dt> |
|
110 <dt><span class="section"><a href="#id2741364">3.5. mkdir</a></span></dt> |
|
111 </dl></dd> |
|
112 <dt><span class="section"><a href="#id2741543">4. Getting Help</a></span></dt> |
|
113 <dd><dl> |
|
114 <dt><span class="section"><a href="#id2741551">4.1. apropos and whatis</a></span></dt> |
|
115 <dt><span class="section"><a href="#id2740051">4.2. man</a></span></dt> |
|
116 <dt><span class="section"><a href="#id2741979">4.3. info</a></span></dt> |
|
117 <dt><span class="section"><a href="#id2742070">4.4. --help</a></span></dt> |
|
118 </dl></dd> |
|
119 <dt><span class="section"><a href="#id2742122">5. Basic file handling</a></span></dt> |
|
120 <dd><dl> |
|
121 <dt><span class="section"><a href="#id2742131">5.1. cp</a></span></dt> |
|
122 <dt><span class="section"><a href="#id2742442">5.2. mv</a></span></dt> |
|
123 <dt><span class="section"><a href="#id2742574">5.3. rm</a></span></dt> |
|
124 </dl></dd> |
|
125 <dt><span class="section"><a href="#id2742744">6. Command Line Arguments</a></span></dt> |
|
126 <dt><span class="section"><a href="#id2742837">7. Basic Text Processing</a></span></dt> |
|
127 <dd><dl> |
|
128 <dt><span class="section"><a href="#id2742845">7.1. head</a></span></dt> |
|
129 <dt><span class="section"><a href="#id2742932">7.2. tail</a></span></dt> |
|
130 <dt><span class="section"><a href="#id2743058">7.3. cut</a></span></dt> |
|
131 <dt><span class="section"><a href="#id2743144">7.4. paste</a></span></dt> |
|
132 </dl></dd> |
|
133 <dt><span class="section"><a href="#id2743276">8. Shell Meta Characters</a></span></dt> |
|
134 <dt><span class="section"><a href="#id2743383">9. Looking At Files</a></span></dt> |
|
135 <dd><dl> |
|
136 <dt><span class="section"><a href="#id2743392">9.1. cat</a></span></dt> |
|
137 <dt><span class="section"><a href="#id2743578">9.2. more</a></span></dt> |
|
138 <dt><span class="section"><a href="#id2743688">9.3. less</a></span></dt> |
|
139 </dl></dd> |
|
140 <dt><span class="section"><a href="#id2743929">10. Directory Structure</a></span></dt> |
|
141 <dd><dl> |
|
142 <dt><span class="section"><a href="#id2744198">10.1. man hier</a></span></dt> |
|
143 <dt><span class="section"><a href="#id2744214">10.2. ls -l</a></span></dt> |
|
144 </dl></dd> |
|
145 <dt><span class="section"><a href="#id2744237">11. Permissions and Ownership</a></span></dt> |
|
146 <dd><dl><dt><span class="section"><a href="#id2744246">11.1. chmod</a></span></dt></dl></dd> |
|
147 <dt><span class="section"><a href="#id2745018">12. Redirection and Piping</a></span></dt> |
|
148 <dd><dl> |
|
149 <dt><span class="section"><a href="#id2745055">12.1. Redirecting standard input and standard output</a></span></dt> |
|
150 <dt><span class="section"><a href="#id2745124">12.2. Piping</a></span></dt> |
|
151 <dt><span class="section"><a href="#id2745186">12.3. Redirecting to and from the standard file handles</a></span></dt> |
|
152 <dt><span class="section"><a href="#id2745390">12.4. Chained pipelines</a></span></dt> |
|
153 <dt><span class="section"><a href="#id2745424">12.5. Redirect to multiple outputs</a></span></dt> |
|
154 </dl></dd> |
|
155 <dt><span class="section"><a href="#id2745454">13. More Text Processing</a></span></dt> |
|
156 <dd><dl> |
|
157 <dt><span class="section"><a href="#id2745463">13.1. grep</a></span></dt> |
|
158 <dt><span class="section"><a href="#id2745652">13.2. tr</a></span></dt> |
|
159 </dl></dd> |
|
160 <dt><span class="section"><a href="#id2745806">14. Elementary Regex</a></span></dt> |
|
161 <dd><dl><dt><span class="section"><a href="#id2746019">14.1. Lazy quantification</a></span></dt></dl></dd> |
|
162 <dt><span class="section"><a href="#id2746095">15. One Liners</a></span></dt> |
|
163 </dl> |
|
164 </div> |
|
165 <div class="section" title="1. Introducing Linux"> |
|
166 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
167 <a name="id2499323"></a>1. Introducing Linux</h2></div></div></div> |
|
168 <p id="ch3list_tuples_1"></a>(Attribution : A significant chunk of the content under this section is based on data from Wikipedia and the Linux Documentation Project)</p> |
|
169 <p id="ch3list_tuples_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> |
|
170 <p id="ch3list_tuples_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> |
|
171 <p id="ch3list_tuples_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> |
|
172 <div class="section" title="1.1. Historical Background"> |
|
173 <div class="titlepage"><div><div><h3 class="title"> |
|
174 <a name="id2687018"></a>1.1. Historical Background</h3></div></div></div> |
|
175 <div class="section" title="1.1.1. Events leading to the creation"> |
|
176 <div class="titlepage"><div><div><h4 class="title"> |
|
177 <a name="id2686984"></a>1.1.1. Events leading to the creation</h4></div></div></div> |
|
178 <div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
179 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
180 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
181 </ul></div> |
|
182 </div> |
|
183 <div class="section" title="1.1.2. The Creation of Linux"> |
|
184 <div class="titlepage"><div><div><h4 class="title"> |
|
185 <a name="id2740214"></a>1.1.2. The Creation of Linux</h4></div></div></div> |
|
186 <p id="ch3list_tuples_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> |
|
187 <p id="ch3list_tuples_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> |
|
188 </div> |
|
189 </div> |
|
190 <div class="section" title="1.2. Design and Implications"> |
|
191 <div class="titlepage"><div><div><h3 class="title"> |
|
192 <a name="id2740259"></a>1.2. Design and Implications</h3></div></div></div> |
|
193 <p id="ch3list_tuples_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> |
|
194 <div class="section" title="1.2.1. User Interface"> |
|
195 <div class="titlepage"><div><div><h4 class="title"> |
|
196 <a name="id2740283"></a>1.2.1. User Interface</h4></div></div></div> |
|
197 <p id="ch3list_tuples_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> |
|
198 <p id="ch3list_tuples_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> |
|
199 </div> |
|
200 <div class="section" title="1.2.2. Development"> |
|
201 <div class="titlepage"><div><div><h4 class="title"> |
|
202 <a name="id2740337"></a>1.2.2. Development</h4></div></div></div> |
|
203 <p id="ch3list_tuples_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> |
|
204 <p id="ch3list_tuples_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> |
|
205 <p id="ch3list_tuples_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> |
|
206 </div> |
|
207 <div class="section" title="1.2.3. Community"> |
|
208 <div class="titlepage"><div><div><h4 class="title"> |
|
209 <a name="id2740394"></a>1.2.3. Community</h4></div></div></div> |
|
210 <p id="ch3list_tuples_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> |
|
211 <p id="ch3list_tuples_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> |
|
212 <p id="ch3list_tuples_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> |
|
213 </div> |
|
214 <div class="section" title="1.2.4. Can I make a profit out of running a business involving Linux?"> |
|
215 <div class="titlepage"><div><div><h4 class="title"> |
|
216 <a name="id2740438"></a>1.2.4. Can I make a profit out of running a business involving Linux?</h4></div></div></div> |
|
217 <p id="ch3list_tuples_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> |
|
218 </div> |
|
219 <div class="section" title="1.2.5. Programming on Linux"> |
|
220 <div class="titlepage"><div><div><h4 class="title"> |
|
221 <a name="id2740484"></a>1.2.5. Programming on Linux</h4></div></div></div> |
|
222 <p id="ch3list_tuples_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> |
|
223 <p id="ch3list_tuples_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> |
|
224 <p id="ch3list_tuples_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> |
|
225 </div> |
|
226 </div> |
|
227 <div class="section" title="1.3. Reasons for Using Linux"> |
|
228 <div class="titlepage"><div><div><h3 class="title"> |
|
229 <a name="id2740531"></a>1.3. Reasons for Using Linux</h3></div></div></div> |
|
230 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_16"></a>Linux is free:</p></li></ul></div> |
|
231 <p id="ch3list_tuples_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. |
|
232 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> |
|
234 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_18"></a>Linux is portable to any hardware platform:</p></li></ul></div> |
|
235 <p id="ch3list_tuples_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> |
|
236 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_1a"></a>Linux was made to keep on running:</p></li></ul></div> |
|
237 <p id="ch3list_tuples_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> |
|
238 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_1c"></a>Linux is secure and versatile:</p></li></ul></div> |
|
239 <p id="ch3list_tuples_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> |
|
240 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_1e"></a>Linux is scalable:</p></li></ul></div> |
|
241 <p id="ch3list_tuples_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> |
|
242 <div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_20"></a>The Linux OS and Linux applications have very short debug−times:</p></li></ul></div> |
|
243 <p id="ch3list_tuples_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> |
|
244 </div> |
|
245 </div> |
|
246 <div class="section" title="2. Getting Started"> |
|
247 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
248 <a name="id2740663"></a>2. Getting Started</h2></div></div></div> |
|
249 <div class="section" title="2.1. Logging in, activating the user interface and logging out"> |
|
250 <div class="titlepage"><div><div><h3 class="title"> |
|
251 <a name="id2740671"></a>2.1. Logging in, activating the user interface and logging out</h3></div></div></div> |
|
252 <p id="ch3list_tuples_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> |
|
253 <div class="section" title="2.1.1. Graphical Mode"> |
|
254 <div class="titlepage"><div><div><h4 class="title"> |
|
255 <a name="id2740702"></a>2.1.1. Graphical Mode</h4></div></div></div> |
|
256 <p id="ch3list_tuples_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> |
|
257 <p id="ch3list_tuples_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>. |
|
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> |
|
259 <p id="ch3list_tuples_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> |
|
260 <p id="ch3list_tuples_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> |
|
261 <p id="ch3list_tuples_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> |
|
262 <p id="ch3list_tuples_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> |
|
263 <p id="ch3list_tuples_29"></a>Another common form for a prompt is this one: |
|
264 [</p> |
|
265 <div class="reference"> |
|
266 <div class="titlepage"><hr></div>user@host</div> |
|
267 <p> dir]</p> |
|
268 <p id="ch3list_tuples_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> |
|
269 <p id="ch3list_tuples_2b"></a>When you see the login screen again, asking to enter user name and password, logout was successful.</p> |
|
270 </div> |
|
271 <div class="section" title="2.1.2. Text Mode"> |
|
272 <div class="titlepage"><div><div><h4 class="title"> |
|
273 <a name="id2740860"></a>2.1.2. Text Mode</h4></div></div></div> |
|
274 <p id="ch3list_tuples_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> |
|
275 <p id="ch3list_tuples_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 |
|
276 reasons.</p> |
|
277 <p id="ch3list_tuples_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> |
|
278 <p id="ch3list_tuples_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> |
|
279 <p id="ch3list_tuples_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> |
|
280 </div> |
|
281 </div> |
|
282 </div> |
|
283 <div class="section" title="3. Basic Commands"> |
|
284 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
285 <a name="id2740949"></a>3. Basic Commands</h2></div></div></div> |
|
286 <div class="section" title="3.1. ls"> |
|
287 <div class="titlepage"><div><div><h3 class="title"> |
|
288 <a name="id2740958"></a>3.1. ls</h3></div></div></div> |
|
289 <p id="ch3list_tuples_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> |
|
290 <p id="ch3list_tuples_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> |
|
291 <p id="ch3list_tuples_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> |
|
292 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
293 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
294 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
295 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
296 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_37"></a><span class="emphasis"><em>-R</em></span> recursively lists subdirectories. The command ls -R / would therefore list all files.</p></li> |
|
297 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
298 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_39"></a><span class="emphasis"><em>-t</em></span> sort the list of files by modification time.</p></li> |
|
299 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_3a"></a><span class="emphasis"><em>-h</em></span> print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.)</p></li> |
|
300 </ul></div></block_quote></span><p id="ch3list_tuples_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> |
|
301 <pre class="programlisting">$ ls |
|
302 jeeves.rst psmith.html blandings.html |
|
303 $ ls -l |
|
304 drwxr--r-- 1 plum editors 4096 jeeves |
|
305 -rw-r--r-- 1 plum editors 30405 psmith |
|
306 -r-xr-xr-x 1 plum plum 8460 blandings</pre> |
|
307 <p id="ch3list_tuples_3c"></a>Here "$" actually is the beginning of the prompt. This is typical in most Unix-based systems.</p> |
|
308 </div> |
|
309 <div class="section" title="3.2. date"> |
|
310 <div class="titlepage"><div><div><h3 class="title"> |
|
311 <a name="id2741120"></a>3.2. date</h3></div></div></div> |
|
312 <p id="ch3list_tuples_3d"></a>The Unix date command displays the time and date. The super-user can use it to set the system clock.</p> |
|
313 <p id="ch3list_tuples_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> |
|
314 <pre class="programlisting">$date |
|
315 Tue Sep 8 12:01:45 IST 2009</pre> |
|
316 <p id="ch3list_tuples_3f"></a>On some systems to set the current date and time to September 8, 2004 01:22 you type:</p> |
|
317 <pre class="programlisting">$date --set="20040908 01:22"</pre> |
|
318 <p id="ch3list_tuples_40"></a>In order to view the various options for the <span class="emphasis"><em>date</em></span> command, type:</p> |
|
319 <pre class="programlisting">$man date</pre> |
|
320 <p id="ch3list_tuples_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> |
|
321 </div> |
|
322 <div class="section" title="3.3. cd"> |
|
323 <div class="titlepage"><div><div><h3 class="title"> |
|
324 <a name="id2741180"></a>3.3. cd</h3></div></div></div> |
|
325 <p id="ch3list_tuples_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> |
|
326 <p id="ch3list_tuples_43"></a>One dot '.' represents the current directory while two dots '..' represent the parent directory.</p> |
|
327 <p id="ch3list_tuples_44"></a>“ cd -” will return you to the previous directory (a bit like an “undo”).</p> |
|
328 <p id="ch3list_tuples_45"></a>You can also use cd absolute path or cd relative path (see below):</p> |
|
329 <p id="ch3list_tuples_46"></a>Absolute paths:</p> |
|
330 <span style="color: red"><block_quote><p id="ch3list_tuples_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></block_quote></span><p id="ch3list_tuples_48"></a>For example to get to /boot/grub you would type:</p> |
|
331 <pre class="programlisting">$cd /boot/grub</pre> |
|
332 <p id="ch3list_tuples_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> |
|
333 <p id="ch3list_tuples_4a"></a>Relative paths:</p> |
|
334 <span style="color: red"><block_quote><p id="ch3list_tuples_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> |
|
335 <p id="ch3list_tuples_4c"></a>For example if you are in root's home directory and want to get to /root/music, you type:</p> |
|
336 <pre class="programlisting">$ cd music</pre></block_quote></span><p id="ch3list_tuples_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> |
|
337 </div> |
|
338 <div class="section" title="3.4. who"> |
|
339 <div class="titlepage"><div><div><h3 class="title"> |
|
340 <a name="id2741282"></a>3.4. who</h3></div></div></div> |
|
341 <p id="ch3list_tuples_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> |
|
342 <p id="ch3list_tuples_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> |
|
343 <pre class="programlisting">$who |
|
344 beeblebrox tty7 2009-09-08 10:50 (:0) |
|
345 beeblebrox pts/0 2009-09-08 11:25 (:0.0) |
|
346 dumbledore pts/1 2009-09-08 18:11 (potter.xyz.in) |
|
347 beeblebrox pts/2 2009-09-08 18:53 (:0.0)</pre> |
|
348 <p id="ch3list_tuples_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> |
|
349 <p id="ch3list_tuples_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> |
|
350 <pre class="programlisting">$man who</pre> |
|
351 <p id="ch3list_tuples_52"></a>This will take you to the "Manual" page containing details about the <span class="emphasis"><em>who</em></span> command</p> |
|
352 </div> |
|
353 <div class="section" title="3.5. mkdir"> |
|
354 <div class="titlepage"><div><div><h3 class="title"> |
|
355 <a name="id2741364"></a>3.5. mkdir</h3></div></div></div> |
|
356 <p id="ch3list_tuples_53"></a>This command is used to make a new directory. Normal usage is as straightforward as follows:</p> |
|
357 <pre class="programlisting">$mkdir name_of_directory</pre> |
|
358 <p id="ch3list_tuples_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> |
|
359 <div class="section" title="3.5.1. Options"> |
|
360 <div class="titlepage"><div><div><h4 class="title"> |
|
361 <a name="id2741396"></a>3.5.1. Options</h4></div></div></div> |
|
362 <p id="ch3list_tuples_55"></a>On Unix-like operating systems, <span class="emphasis"><em>mkdir</em></span> takes options. Three of the most common options are:</p> |
|
363 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
364 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
365 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_57"></a><span class="emphasis"><em>-v</em></span>: display each directory that mkdir creates. Most often used with -p.</p></li> |
|
366 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_58"></a><span class="emphasis"><em>-m</em></span>: specify the octal permissions of directories created by mkdir.</p></li> |
|
367 </ul></div></block_quote></span><p id="ch3list_tuples_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> |
|
368 </div> |
|
369 <div class="section" title="3.5.2. Examples"> |
|
370 <div class="titlepage"><div><div><h4 class="title"> |
|
371 <a name="id2741457"></a>3.5.2. Examples</h4></div></div></div> |
|
372 <p id="ch3list_tuples_5a"></a>An example of <span class="emphasis"><em>-p</em></span> in action is:</p> |
|
373 <pre class="programlisting">$mkdir -p /tmp/a/b/c</pre> |
|
374 <p id="ch3list_tuples_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> |
|
375 <p id="ch3list_tuples_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> |
|
376 <pre class="programlisting">$mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}</pre> |
|
377 <p id="ch3list_tuples_5d"></a>This will create:</p> |
|
378 <span style="color: red"><definition_list><span style="color: red"><definition_list_item><span style="color: red"><term>tmpdir - branches</term></span><span style="color: red"><definition><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
379 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_5e"></a>tag</p></li> |
|
380 <li class="listitem" style="list-style-type: *"><span style="color: red"><definition_list><span style="color: red"><definition_list_item><span style="color: red"><term>trunk - sources - includes</term></span><span style="color: red"><definition><div class="itemizedlist"><ul class="itemizedlist" type="*"><li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_5f"></a>docs</p></li></ul></div></definition></span></definition_list_item></span></definition_list></span></li> |
|
381 </ul></div></definition></span></definition_list_item></span></definition_list></span> |
|
382 </div> |
|
383 </div> |
|
384 </div> |
|
385 <div class="section" title="4. Getting Help"> |
|
386 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
387 <a name="id2741543"></a>4. Getting Help</h2></div></div></div> |
|
388 <div class="section" title="4.1. apropos and whatis"> |
|
389 <div class="titlepage"><div><div><h3 class="title"> |
|
390 <a name="id2741551"></a>4.1. apropos and whatis</h3></div></div></div> |
|
391 <p id="ch3list_tuples_60"></a>This is a command to search the manual pages files in Unix and Unix-like operating systems.</p> |
|
392 <pre class="programlisting">$ apropos grep |
|
393 egrep egrep (1) Search a file for a pattern using full regular expressions |
|
394 fgrep fgrep (1) Search a file for a fixed-character string |
|
395 fmlgrep fmlgrep (1) Search a file for a pattern |
|
396 grep grep (1) Search a file for a pattern |
|
397 gzgrep gzgrep (1) Search a possibly compressed file for a regular expression |
|
398 nisgrep nismatch (1) Utilities for searching NIS+ tables |
|
399 pgrep pgrep (1) Find or signal a process by name or other attribute |
|
400 zgrep zgrep (1) Search a possibly compressed file for a regular expression |
|
401 ...</pre> |
|
402 <p id="ch3list_tuples_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> |
|
403 <p id="ch3list_tuples_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> |
|
404 <pre class="programlisting">$whatis ls |
|
405 ls (1) - list directory contents</pre> |
|
406 <p id="ch3list_tuples_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> |
|
407 <p id="ch3list_tuples_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> |
|
408 <pre class="programlisting">$apropos browser |
|
409 gmusicbrowser (1) - Jukebox for large collections of audio files |
|
410 infobrowser (1) - read Info documents |
|
411 libsmbclient (7) - An extension library for browsers and that can be used... |
|
412 opera (1) - a standards-compliant graphical Web browser |
|
413 sensible-browser (1) - sensible editing, paging, and web browsing |
|
414 smbtree (1) - A text based smb network browser |
|
415 tvtk_doc (1) - A GUI based TVTK documentation search browser. |
|
416 viewres (1) - graphical class browser for Xt |
|
417 w3m (1) - a text based Web browser and pager |
|
418 www-browser (1) - a text based Web browser and pager |
|
419 ...</pre> |
|
420 </div> |
|
421 <div class="section" title="4.2. man"> |
|
422 <div class="titlepage"><div><div><h3 class="title"> |
|
423 <a name="id2740051"></a>4.2. man</h3></div></div></div> |
|
424 <p id="ch3list_tuples_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> |
|
425 <p id="ch3list_tuples_66"></a>To read a manual page for a Unix command, one can use:</p> |
|
426 <pre class="programlisting">$ man <command_name></pre> |
|
427 <p id="ch3list_tuples_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> |
|
428 <p id="ch3list_tuples_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> |
|
429 <pre class="programlisting">$man 3 printf</pre> |
|
430 <p id="ch3list_tuples_69"></a>Another example:</p> |
|
431 <pre class="programlisting">$man man</pre> |
|
432 <p id="ch3list_tuples_6a"></a>The previous example will take you to the "Manual" page entry about manual pages!</p> |
|
433 <div class="section" title="4.2.1. Layout"> |
|
434 <div class="titlepage"><div><div><h4 class="title"> |
|
435 <a name="id2741853"></a>4.2.1. Layout</h4></div></div></div> |
|
436 <p id="ch3list_tuples_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> |
|
437 <span style="color: red"><definition_list><span style="color: red"><definition_list_item><span style="color: red"><term>NAME</term></span><span style="color: red"><definition><p id="ch3list_tuples_6c"></a>The name of the command or function, followed by a one-line description of what it does.</p></definition></span></definition_list_item></span><span style="color: red"><definition_list_item><span style="color: red"><term>SYNOPSIS</term></span><span style="color: red"><definition><p id="ch3list_tuples_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></definition></span></definition_list_item></span><span style="color: red"><definition_list_item><span style="color: red"><term>DESCRIPTION</term></span><span style="color: red"><definition><p id="ch3list_tuples_6e"></a>A textual description of the functioning of the command or function.</p></definition></span></definition_list_item></span><span style="color: red"><definition_list_item><span style="color: red"><term>EXAMPLES</term></span><span style="color: red"><definition><p id="ch3list_tuples_6f"></a>Some examples of common usage.</p></definition></span></definition_list_item></span><span style="color: red"><definition_list_item><span style="color: red"><term>SEE ALSO</term></span><span style="color: red"><definition><p id="ch3list_tuples_70"></a>A list of related commands or functions.</p></definition></span></definition_list_item></span></definition_list></span><p id="ch3list_tuples_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> |
|
438 <p id="ch3list_tuples_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> |
|
439 <p id="ch3list_tuples_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> |
|
440 <p id="ch3list_tuples_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> |
|
441 <p id="ch3list_tuples_75"></a>Usually the man pages are written in English. Translations into other languages can be also available on the system.</p> |
|
442 <p id="ch3list_tuples_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> |
|
443 </div> |
|
444 </div> |
|
445 <div class="section" title="4.3. info"> |
|
446 <div class="titlepage"><div><div><h3 class="title"> |
|
447 <a name="id2741979"></a>4.3. info</h3></div></div></div> |
|
448 <p id="ch3list_tuples_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> |
|
449 <p id="ch3list_tuples_78"></a>The syntax is</p> |
|
450 <pre class="programlisting">$ info <command_name></pre> |
|
451 <p id="ch3list_tuples_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> |
|
452 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
453 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_7a"></a><span class="emphasis"><em>n</em></span> goes to the next page.</p></li> |
|
454 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_7b"></a><span class="emphasis"><em>p</em></span> goes to the previous page.</p></li> |
|
455 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_7c"></a><span class="emphasis"><em>u</em></span> goes to the upper page.</p></li> |
|
456 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_7d"></a><span class="emphasis"><em>l</em></span> goes to the last(visited) node</p></li> |
|
457 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_7e"></a>To follow a cross reference, the cursor can be moved over a link (a word preceded by a <span style="color: red"><title_reference>*</title_reference></span>) and enter pressed.</p></li> |
|
458 </ul></div></block_quote></span><p id="ch3list_tuples_7f"></a>info was initially written for use with GNU/Linux and then ported to other Unix-like operating systems.</p> |
|
459 </div> |
|
460 <div class="section" title="4.4. --help"> |
|
461 <div class="titlepage"><div><div><h3 class="title"> |
|
462 <a name="id2742070"></a>4.4. --help</h3></div></div></div> |
|
463 <p id="ch3list_tuples_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> |
|
464 <pre class="programlisting">$ userprompt@host: cat --help |
|
465 Usage: cat [OPTION] [FILE]... |
|
466 Concatenate FILE(s), or standard input, to standard output. |
|
467 |
|
468 -A, --show-all equivalent to -vET |
|
469 -b, --number-nonblank number nonempty output lines |
|
470 -e equivalent to -vE |
|
471 -E, --show-ends display $ at end of each line |
|
472 -n, --number number all output lines |
|
473 -s, --squeeze-blank suppress repeated empty output lines |
|
474 -t equivalent to -vT |
|
475 -T, --show-tabs display TAB characters as ^I |
|
476 -u (ignored) |
|
477 -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB |
|
478 --help display this help and exit |
|
479 --version output version information and exit |
|
480 |
|
481 With no FILE, or when FILE is -, read standard input. |
|
482 |
|
483 Examples: |
|
484 cat f - g Output f's contents, then standard input, then g's contents. |
|
485 cat Copy standard input to standard output. |
|
486 |
|
487 Report bugs to <bug-coreutils@gnu.org>.</pre> |
|
488 </div> |
|
489 </div> |
|
490 <div class="section" title="5. Basic file handling"> |
|
491 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
492 <a name="id2742122"></a>5. Basic file handling</h2></div></div></div> |
|
493 <div class="section" title="5.1. cp"> |
|
494 <div class="titlepage"><div><div><h3 class="title"> |
|
495 <a name="id2742131"></a>5.1. cp</h3></div></div></div> |
|
496 <p id="ch3list_tuples_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> |
|
497 <div class="section" title="5.1.1. Usage"> |
|
498 <div class="titlepage"><div><div><h4 class="title"> |
|
499 <a name="id2742148"></a>5.1.1. Usage</h4></div></div></div> |
|
500 <p id="ch3list_tuples_82"></a>To copy a file to another file:</p> |
|
501 <pre class="programlisting">$ cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile</pre> |
|
502 <p id="ch3list_tuples_83"></a>To copy a file to a directory:</p> |
|
503 <pre class="programlisting">$ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory</pre> |
|
504 <p id="ch3list_tuples_84"></a>To copy a directory to a directory:</p> |
|
505 <pre class="programlisting">$ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } |
|
506 SourceDirectory ... TargetDirectory</pre> |
|
507 </div> |
|
508 <div class="section" title="5.1.2. Flags"> |
|
509 <div class="titlepage"><div><div><h4 class="title"> |
|
510 <a name="id2742182"></a>5.1.2. Flags</h4></div></div></div> |
|
511 <p id="ch3list_tuples_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> |
|
512 <p id="ch3list_tuples_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> |
|
513 <p id="ch3list_tuples_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> |
|
514 <p id="ch3list_tuples_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> |
|
515 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
516 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_89"></a>The time of the last data modification and the time of the last access.</p></li> |
|
517 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_8a"></a>The user ID and group ID (only if it has permissions to do this)</p></li> |
|
518 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_8b"></a>The file permission bits and the SUID and SGID bits.</p></li> |
|
519 </ul></div></block_quote></span><p id="ch3list_tuples_8c"></a><span class="emphasis"><em>-R</em></span> (recursive) – copy directories (recursively copying all the contents)</p> |
|
520 </div> |
|
521 <div class="section" title="5.1.3. Examples"> |
|
522 <div class="titlepage"><div><div><h4 class="title"> |
|
523 <a name="id2742280"></a>5.1.3. Examples</h4></div></div></div> |
|
524 <p id="ch3list_tuples_8d"></a>To make a copy of a file in the current directory, enter:</p> |
|
525 <pre class="programlisting">$ cp prog.c prog.bak</pre> |
|
526 <p id="ch3list_tuples_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> |
|
527 <p id="ch3list_tuples_8f"></a>To copy a file in your current directory into another directory, enter:</p> |
|
528 <pre class="programlisting">$ cp zaphod /home/books/hhgg</pre> |
|
529 <p id="ch3list_tuples_90"></a>This copies the jones file to /home/books/hhgg/zaphod.</p> |
|
530 <p id="ch3list_tuples_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> |
|
531 <pre class="programlisting">$ cp -p martin_luther_king martin_luther_king.jr</pre> |
|
532 <p id="ch3list_tuples_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> |
|
533 <p id="ch3list_tuples_93"></a>To copy all the files in a directory to a new directory, enter:</p> |
|
534 <pre class="programlisting">$ cp /home/galactica/clients/* /home/hhgg/customers</pre> |
|
535 <p id="ch3list_tuples_94"></a>This copies only the files in the clients directory to the customers directory.</p> |
|
536 <p id="ch3list_tuples_95"></a>To copy a directory, including all its files and subdirectories, to another directory, enter:</p> |
|
537 <span style="color: red"><block_quote><p id="ch3list_tuples_96"></a>$ cp -R /home/hhgg/clients /home/hhgg/customers</p></block_quote></span><p id="ch3list_tuples_97"></a>This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory.</p> |
|
538 <p id="ch3list_tuples_98"></a>To copy a specific set of files of any extension to another directory, enter:</p> |
|
539 <pre class="programlisting">$ cp zaphod arthur ford /home/hhgg/clients</pre> |
|
540 <p id="ch3list_tuples_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> |
|
541 <p id="ch3list_tuples_9a"></a>To use pattern-matching characters to copy files, enter:</p> |
|
542 <pre class="programlisting">$ cp programs/*.py .</pre> |
|
543 <p id="ch3list_tuples_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> |
|
544 </div> |
|
545 </div> |
|
546 <div class="section" title="5.2. mv"> |
|
547 <div class="titlepage"><div><div><h3 class="title"> |
|
548 <a name="id2742442"></a>5.2. mv</h3></div></div></div> |
|
549 <p id="ch3list_tuples_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> |
|
550 <div class="section" title="5.2.1. Conflicting existing file"> |
|
551 <div class="titlepage"><div><div><h4 class="title"> |
|
552 <a name="id2742466"></a>5.2.1. Conflicting existing file</h4></div></div></div> |
|
553 <p id="ch3list_tuples_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> |
|
554 </div> |
|
555 <div class="section" title="5.2.2. Differences with copy and delete"> |
|
556 <div class="titlepage"><div><div><h4 class="title"> |
|
557 <a name="id2742485"></a>5.2.2. Differences with copy and delete</h4></div></div></div> |
|
558 <p id="ch3list_tuples_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> |
|
559 </div> |
|
560 <div class="section" title="5.2.3. Examples"> |
|
561 <div class="titlepage"><div><div><h4 class="title"> |
|
562 <a name="id2742516"></a>5.2.3. Examples</h4></div></div></div> |
|
563 <pre class="programlisting">$ mv myfile mynewfilename renames a file |
|
564 $ mv myfile otherfilename renames a file and deletes the existing file "myfile" |
|
565 $ mv myfile /myfile moves 'myfile' from the current directory to the root directory |
|
566 $ mv myfile dir/myfile moves 'myfile' to 'dir/myfile' relative to the current directory |
|
567 $ mv myfile dir same as the previous command (the filename is implied to be the same) |
|
568 $ mv myfile dir/myfile2 moves 'myfile' to dir and renames it to 'myfile2' |
|
569 $ mv foo bar baz dir moves multiple files to directory dir |
|
570 $ mv --help shows a very concise help about the syntax of the command |
|
571 $ man mv prints an extensive user manual for 'mv' in the terminal</pre> |
|
572 <p id="ch3list_tuples_9f"></a>In all cases, the file or files being moved or renamed can be a directory.</p> |
|
573 <p id="ch3list_tuples_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> |
|
574 </div> |
|
575 </div> |
|
576 <div class="section" title="5.3. rm"> |
|
577 <div class="titlepage"><div><div><h3 class="title"> |
|
578 <a name="id2742574"></a>5.3. rm</h3></div></div></div> |
|
579 <p id="ch3list_tuples_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> |
|
580 <p id="ch3list_tuples_a2"></a>Here's example to remove a file named "foo" from a directory, here shown with the -i option:</p> |
|
581 <pre class="programlisting">$ rm -i foo |
|
582 remove foo? y</pre> |
|
583 <div class="section" title="5.3.1. Options"> |
|
584 <div class="titlepage"><div><div><h4 class="title"> |
|
585 <a name="id2742604"></a>5.3.1. Options</h4></div></div></div> |
|
586 <p id="ch3list_tuples_a3"></a>Common options that rm accepts include:</p> |
|
587 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
588 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_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> |
|
589 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_a5"></a><span class="emphasis"><em>-i</em></span>, which asks for every deletion to be confirmed ("interactive")</p></li> |
|
590 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_a6"></a><span class="emphasis"><em>-f</em></span>, which ignores non-existent files and overrides any confirmation prompts ("force")</p></li> |
|
591 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_a7"></a><span class="emphasis"><em>-v</em></span>, which shows what is being removed as it happens ("verbose")</p></li> |
|
592 </ul></div></block_quote></span><p id="ch3list_tuples_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> |
|
593 <p id="ch3list_tuples_a9"></a><span class="emphasis"><em>rm -rf</em></span> (variously, rm -rf /, rm -rf <span style="color: red"><title_reference>*</title_reference></span>, 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> |
|
594 <p id="ch3list_tuples_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> |
|
595 <pre class="programlisting">xargs rm < filelist</pre> |
|
596 <p id="ch3list_tuples_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> |
|
597 </div> |
|
598 <div class="section" title="5.3.2. Permissions"> |
|
599 <div class="titlepage"><div><div><h4 class="title"> |
|
600 <a name="id2742705"></a>5.3.2. Permissions</h4></div></div></div> |
|
601 <p id="ch3list_tuples_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> |
|
602 <p id="ch3list_tuples_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> |
|
603 <p id="ch3list_tuples_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> |
|
604 </div> |
|
605 </div> |
|
606 </div> |
|
607 <div class="section" title="6. Command Line Arguments"> |
|
608 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
609 <a name="id2742744"></a>6. Command Line Arguments</h2></div></div></div> |
|
610 <p id="ch3list_tuples_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> |
|
611 <p id="ch3list_tuples_b0"></a>For example, in Unix and Unix-like environments, an example of a command-line argument is:</p> |
|
612 <pre class="programlisting">rm file.s</pre> |
|
613 <p id="ch3list_tuples_b1"></a>"file.s" is a command line argument which tells the program rm to remove the file "file.s".</p> |
|
614 <p id="ch3list_tuples_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> |
|
615 <p id="ch3list_tuples_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> |
|
616 <p id="ch3list_tuples_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> |
|
617 <p id="ch3list_tuples_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> |
|
618 </div> |
|
619 <div class="section" title="7. Basic Text Processing"> |
|
620 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
621 <a name="id2742837"></a>7. Basic Text Processing</h2></div></div></div> |
|
622 <div class="section" title="7.1. head"> |
|
623 <div class="titlepage"><div><div><h3 class="title"> |
|
624 <a name="id2742845"></a>7.1. head</h3></div></div></div> |
|
625 <p id="ch3list_tuples_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> |
|
626 <pre class="programlisting">$ head [options] <file_name></pre> |
|
627 <p id="ch3list_tuples_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> |
|
628 <pre class="programlisting">$ head -n 20 filename</pre> |
|
629 <p id="ch3list_tuples_b8"></a>This displays the first 5 lines of all files starting with <span class="emphasis"><em>foo</em></span>:</p> |
|
630 <pre class="programlisting">$ head -n 5 foo*</pre> |
|
631 <p id="ch3list_tuples_b9"></a>Some versions omit the n and just let you say -5.</p> |
|
632 <div class="section" title="7.1.1. Flags"> |
|
633 <div class="titlepage"><div><div><h4 class="title"> |
|
634 <a name="id2742896"></a>7.1.1. Flags</h4></div></div></div> |
|
635 <pre class="programlisting">-c <x number of bytes> Copy first x number of bytes.</pre> |
|
636 <p id="ch3list_tuples_ba"></a>Other options: <span class="emphasis"><em>sed</em></span></p> |
|
637 <p id="ch3list_tuples_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> |
|
638 <pre class="programlisting">sed 5q foo</pre> |
|
639 <p id="ch3list_tuples_bc"></a>This says to print every line (implicit), and quit after the fifth.</p> |
|
640 </div> |
|
641 </div> |
|
642 <div class="section" title="7.2. tail"> |
|
643 <div class="titlepage"><div><div><h3 class="title"> |
|
644 <a name="id2742932"></a>7.2. tail</h3></div></div></div> |
|
645 <p id="ch3list_tuples_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> |
|
646 <p id="ch3list_tuples_be"></a>The command-syntax is:</p> |
|
647 <pre class="programlisting">$ tail [options] <file_name></pre> |
|
648 <p id="ch3list_tuples_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> |
|
649 <pre class="programlisting">$ tail -n 20 filename</pre> |
|
650 <p id="ch3list_tuples_c0"></a>This example shows the last 15 bytes of all files starting with <span class="emphasis"><em>foo</em></span>:</p> |
|
651 <pre class="programlisting">$ tail -c 15 foo*</pre> |
|
652 <p id="ch3list_tuples_c1"></a>This example shows all lines of filename from the second line onwards:</p> |
|
653 <pre class="programlisting">$ tail -n +2 filename</pre> |
|
654 <p id="ch3list_tuples_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> |
|
655 <pre class="programlisting">$ tail -20 filename |
|
656 $ tail -50c filename</pre> |
|
657 <p id="ch3list_tuples_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> |
|
658 <div class="section" title="7.2.1. File monitoring"> |
|
659 <div class="titlepage"><div><div><h4 class="title"> |
|
660 <a name="id2743009"></a>7.2.1. File monitoring</h4></div></div></div> |
|
661 <p id="ch3list_tuples_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> |
|
662 <pre class="programlisting">$ tail -f /var/adm/messages</pre> |
|
663 <p id="ch3list_tuples_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> |
|
664 <p id="ch3list_tuples_c6"></a>If you have a command's result to monitor, you can use the <span class="emphasis"><em>watch</em></span> command.</p> |
|
665 </div> |
|
666 </div> |
|
667 <div class="section" title="7.3. cut"> |
|
668 <div class="titlepage"><div><div><h3 class="title"> |
|
669 <a name="id2743058"></a>7.3. cut</h3></div></div></div> |
|
670 <p id="ch3list_tuples_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> |
|
671 <p id="ch3list_tuples_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> |
|
672 <p id="ch3list_tuples_c9"></a>Assuming a file named file containing the lines:</p> |
|
673 <pre class="programlisting">foo:bar:baz:qux:quux |
|
674 one:two:three:four:five:six:seven |
|
675 alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu</pre> |
|
676 <p id="ch3list_tuples_ca"></a>To output the fourth through tenth characters of each line:</p> |
|
677 <pre class="programlisting">$ cut -c 4-10 file</pre> |
|
678 <p id="ch3list_tuples_cb"></a>This gives the output:</p> |
|
679 <pre class="programlisting">:bar:ba |
|
680 :two:th |
|
681 ha:beta</pre> |
|
682 <p id="ch3list_tuples_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> |
|
683 <pre class="programlisting">$ cut -d : -f 5- file</pre> |
|
684 <p id="ch3list_tuples_cd"></a>This gives the output:</p> |
|
685 <pre class="programlisting">quux |
|
686 five:six:seven |
|
687 epsilon:zeta:eta:teta:iota:kappa:lambda:mu</pre> |
|
688 </div> |
|
689 <div class="section" title="7.4. paste"> |
|
690 <div class="titlepage"><div><div><h3 class="title"> |
|
691 <a name="id2743144"></a>7.4. paste</h3></div></div></div> |
|
692 <p id="ch3list_tuples_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> |
|
693 <p id="ch3list_tuples_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> |
|
694 <pre class="programlisting">$ paste who where when > www</pre> |
|
695 <p id="ch3list_tuples_d0"></a>If the files contain:</p> |
|
696 <table summary="paste" border="1"><colgroup> |
|
697 <col width="11"> |
|
698 <col width="12"> |
|
699 <col width="12"> |
|
700 </colgroup></table> |
|
701 <p id="ch3list_tuples_dd"></a>This creates the file named <span class="emphasis"><em>www</em></span> containing:</p> |
|
702 <pre class="programlisting">Batman GothamCity January 3 |
|
703 Trillian Andromeda February 4 |
|
704 Jeeves London March 19</pre> |
|
705 </div> |
|
706 </div> |
|
707 <div class="section" title="8. Shell Meta Characters"> |
|
708 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
709 <a name="id2743276"></a>8. Shell Meta Characters</h2></div></div></div> |
|
710 <p id="ch3list_tuples_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> |
|
711 <p id="ch3list_tuples_df"></a>The shell meta characters include:</p> |
|
712 <p id="ch3list_tuples_e0"></a>/ < > ! $ % ^ & * | { } [ ] " ' ` ~ ;</p> |
|
713 <p id="ch3list_tuples_e1"></a>Different shells may differ in the meta characters recognized.</p> |
|
714 <p id="ch3list_tuples_e2"></a>As an example,</p> |
|
715 <pre class="programlisting">$ ls file.*</pre> |
|
716 <p id="ch3list_tuples_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> |
|
717 <pre class="programlisting">$ ls file.?</pre> |
|
718 <p id="ch3list_tuples_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> |
|
719 <pre class="programlisting">$ more c*</pre> |
|
720 <p id="ch3list_tuples_e5"></a>because the c* matches that long file name.</p> |
|
721 <p id="ch3list_tuples_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 <span style="color: red"><title_reference>"``*`|more</title_reference></span>"` can be accomplished with:</p> |
|
722 <pre class="programlisting">$ rm ??more</pre> |
|
723 <p id="ch3list_tuples_e7"></a>or:</p> |
|
724 <pre class="programlisting">$ rm $\backslash$*$\backslash$|more</pre> |
|
725 <p id="ch3list_tuples_e8"></a>or:</p> |
|
726 <pre class="programlisting">$ rm ''*|more''</pre> |
|
727 </div> |
|
728 <div class="section" title="9. Looking At Files"> |
|
729 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
730 <a name="id2743383"></a>9. Looking At Files</h2></div></div></div> |
|
731 <div class="section" title="9.1. cat"> |
|
732 <div class="titlepage"><div><div><h3 class="title"> |
|
733 <a name="id2743392"></a>9.1. cat</h3></div></div></div> |
|
734 <p id="ch3list_tuples_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> |
|
735 <p id="ch3list_tuples_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> |
|
736 <p id="ch3list_tuples_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> |
|
737 <div class="section" title="9.1.1. Jargon File Definition"> |
|
738 <div class="titlepage"><div><div><h4 class="title"> |
|
739 <a name="id2743435"></a>9.1.1. Jargon File Definition</h4></div></div></div> |
|
740 <p id="ch3list_tuples_ec"></a>The Jargon File version 4.4.7 lists this as the definition of <span class="emphasis"><em>cat</em></span>:</p> |
|
741 <pre class="programlisting">1. To spew an entire file to the screen or some other output sink without |
|
742 pause (syn. blast). |
|
743 |
|
744 2. By extension, to dump large amounts of data at an unprepared target or |
|
745 with no intention of browsing it carefully. Usage: considered silly. |
|
746 Rare outside Unix sites. See also dd, BLT. |
|
747 |
|
748 Among Unix fans, *cat(1)* is considered an excellent example of |
|
749 user-interface design, because it delivers the file contents without |
|
750 such verbosity as spacing or headers between the files, and because |
|
751 it does not require the files to consist of lines of text, but works |
|
752 with any sort of data. |
|
753 |
|
754 Among Unix critics, *cat(1)* is considered the canonical example of |
|
755 bad user-interface design, because of its woefully unobvious name. |
|
756 It is far more often used to blast a single file to standard output |
|
757 than to concatenate two or more files. The name cat for the former |
|
758 operation is just as unintuitive as, say, LISP's cdr. |
|
759 |
|
760 Of such oppositions are holy wars made...</pre> |
|
761 </div> |
|
762 <div class="section" title="9.1.2. Useless Use of 'cat'"> |
|
763 <div class="titlepage"><div><div><h4 class="title"> |
|
764 <a name="id2743474"></a>9.1.2. Useless Use of 'cat'</h4></div></div></div> |
|
765 <p id="ch3list_tuples_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> |
|
766 <p id="ch3list_tuples_ee"></a>Nevertheless one sees people doing:</p> |
|
767 <pre class="programlisting">$ cat file | some_command and its args ...</pre> |
|
768 <p id="ch3list_tuples_ef"></a>instead of the equivalent and cheaper:</p> |
|
769 <pre class="programlisting"><file some_command and its args ...</pre> |
|
770 <p id="ch3list_tuples_f0"></a>or (equivalently and more classically):</p> |
|
771 <pre class="programlisting">some_command and its args ... <file</pre> |
|
772 <p id="ch3list_tuples_f1"></a>Since 1995, occasional awards for UUOC have been given out. The activity of fixing instances of UUOC is sometimes called 'demoggification'.</p> |
|
773 <p id="ch3list_tuples_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> |
|
774 </div> |
|
775 <div class="section" title="9.1.3. zcat"> |
|
776 <div class="titlepage"><div><div><h4 class="title"> |
|
777 <a name="id2743544"></a>9.1.3. zcat</h4></div></div></div> |
|
778 <p id="ch3list_tuples_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> |
|
779 </div> |
|
780 </div> |
|
781 <div class="section" title="9.2. more"> |
|
782 <div class="titlepage"><div><div><h3 class="title"> |
|
783 <a name="id2743578"></a>9.2. more</h3></div></div></div> |
|
784 <p id="ch3list_tuples_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> |
|
785 <div class="section" title="9.2.1. Usage"> |
|
786 <div class="titlepage"><div><div><h4 class="title"> |
|
787 <a name="id2743597"></a>9.2.1. Usage</h4></div></div></div> |
|
788 <p id="ch3list_tuples_f5"></a>The command-syntax is:</p> |
|
789 <pre class="programlisting">$ more [options] [file_name]</pre> |
|
790 <p id="ch3list_tuples_f6"></a>If no file name is provided, <span class="emphasis"><em>more</em></span> looks for input from stdin.</p> |
|
791 <p id="ch3list_tuples_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> |
|
792 <p id="ch3list_tuples_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> |
|
793 <p id="ch3list_tuples_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> |
|
794 </div> |
|
795 </div> |
|
796 <div class="section" title="9.3. less"> |
|
797 <div class="titlepage"><div><div><h3 class="title"> |
|
798 <a name="id2743688"></a>9.3. less</h3></div></div></div> |
|
799 <p id="ch3list_tuples_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> |
|
800 <div class="section" title="9.3.1. Usage"> |
|
801 <div class="titlepage"><div><div><h4 class="title"> |
|
802 <a name="id2743717"></a>9.3.1. Usage</h4></div></div></div> |
|
803 <p id="ch3list_tuples_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> |
|
804 <p id="ch3list_tuples_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> |
|
805 <p id="ch3list_tuples_fd"></a>The command-syntax is:</p> |
|
806 <pre class="programlisting">$ less [options] file_name</pre> |
|
807 </div> |
|
808 <div class="section" title="9.3.2. Frequently Used Options"> |
|
809 <div class="titlepage"><div><div><h4 class="title"> |
|
810 <a name="id2743772"></a>9.3.2. Frequently Used Options</h4></div></div></div> |
|
811 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
812 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_fe"></a>-g: Highlights just the current match of any searched string.</p></li> |
|
813 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_ff"></a>-I: Case-insensitive searches.</p></li> |
|
814 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_100"></a>-M: Shows more detailed prompt, including file position.</p></li> |
|
815 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_101"></a>-N: Shows line numbers (useful for source code viewing).</p></li> |
|
816 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_102"></a>-S: Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling.</p></li> |
|
817 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_103"></a>-?: Shows help.</p></li> |
|
818 </ul></div></block_quote></span> |
|
819 </div> |
|
820 <div class="section" title="9.3.3. Frequently Used Commands"> |
|
821 <div class="titlepage"><div><div><h4 class="title"> |
|
822 <a name="id2743824"></a>9.3.3. Frequently Used Commands</h4></div></div></div> |
|
823 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
824 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_104"></a>[Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.</p></li> |
|
825 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_105"></a>[Space bar]: Next page.</p></li> |
|
826 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_106"></a>b: Previous page.</p></li> |
|
827 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_107"></a>ng: Jump to line number n. Default is the start of the file.</p></li> |
|
828 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_108"></a>nG: Jump to line number n. Default is the end of the file.</p></li> |
|
829 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_109"></a>/pattern: Search for pattern. Regular expressions can be used.</p></li> |
|
830 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10a"></a>'^ or g: Go to start of file.</p></li> |
|
831 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10b"></a>'$ or G: Go to end of file.</p></li> |
|
832 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10c"></a>s: Save current content (got from another program like grep) in a file.</p></li> |
|
833 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10d"></a>=: File information.</p></li> |
|
834 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10e"></a>h: Help.</p></li> |
|
835 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_10f"></a>q: Quit.</p></li> |
|
836 </ul></div></block_quote></span> |
|
837 </div> |
|
838 <div class="section" title="9.3.4. Examples"> |
|
839 <div class="titlepage"><div><div><h4 class="title"> |
|
840 <a name="id2743913"></a>9.3.4. Examples</h4></div></div></div> |
|
841 <pre class="programlisting">$ less -M readme.txt #Read "readme.txt." |
|
842 $ less +F /var/log/mail.log #Follow mode for log |
|
843 $ file * | less #Easier file analysis. |
|
844 $ grep -i void *.c | less -I -p void #Case insensitive search for "void" in all .c files</pre> |
|
845 </div> |
|
846 </div> |
|
847 </div> |
|
848 <div class="section" title="10. Directory Structure"> |
|
849 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
850 <a name="id2743929"></a>10. Directory Structure</h2></div></div></div> |
|
851 <p id="ch3list_tuples_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> |
|
852 <p id="ch3list_tuples_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> |
|
853 <table summary="Directory Structure" border="1"><colgroup> |
|
854 <col width="15"> |
|
855 <col width="48"> |
|
856 </colgroup></table> |
|
857 <div class="section" title="10.1. man hier"> |
|
858 <div class="titlepage"><div><div><h3 class="title"> |
|
859 <a name="id2744198"></a>10.1. man hier</h3></div></div></div> |
|
860 <p id="ch3list_tuples_136"></a>This is the manual page on the UNIX filesystem. The syntax for this is:</p> |
|
861 <pre class="programlisting">$ man hier</pre> |
|
862 </div> |
|
863 <div class="section" title="10.2. ls -l"> |
|
864 <div class="titlepage"><div><div><h3 class="title"> |
|
865 <a name="id2744214"></a>10.2. ls -l</h3></div></div></div> |
|
866 <p id="ch3list_tuples_137"></a>Shows you huge amounts of information (permissions, owners, size, and when last modified) for folders and files. The syntax is</p> |
|
867 <pre class="programlisting">$ ls -l</pre> |
|
868 <p id="ch3list_tuples_138"></a>This can be done after entering the required directory.</p> |
|
869 </div> |
|
870 </div> |
|
871 <div class="section" title="11. Permissions and Ownership"> |
|
872 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
873 <a name="id2744237"></a>11. Permissions and Ownership</h2></div></div></div> |
|
874 <div class="section" title="11.1. chmod"> |
|
875 <div class="titlepage"><div><div><h3 class="title"> |
|
876 <a name="id2744246"></a>11.1. chmod</h3></div></div></div> |
|
877 <p id="ch3list_tuples_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> |
|
878 <div class="section" title="11.1.1. Usage"> |
|
879 <div class="titlepage"><div><div><h4 class="title"> |
|
880 <a name="id2744272"></a>11.1.1. Usage</h4></div></div></div> |
|
881 <p id="ch3list_tuples_13a"></a>The <span class="emphasis"><em>chmod</em></span> command options are specified like this:</p> |
|
882 <pre class="programlisting">$ chmod [options] mode[,mode] file1 [file2 ...]</pre> |
|
883 <p id="ch3list_tuples_13b"></a>To view what the permissions currently are, type:</p> |
|
884 <pre class="programlisting">$ ls -l file</pre> |
|
885 </div> |
|
886 <div class="section" title="11.1.2. Command line options"> |
|
887 <div class="titlepage"><div><div><h4 class="title"> |
|
888 <a name="id2744300"></a>11.1.2. Command line options</h4></div></div></div> |
|
889 <p id="ch3list_tuples_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> |
|
890 <span style="color: red"><block_quote><div class="itemizedlist"><ul class="itemizedlist" type="*"> |
|
891 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_13d"></a>-R: Changes the modes of directories and files recursively</p></li> |
|
892 <li class="listitem" style="list-style-type: *"><p id="ch3list_tuples_13e"></a>-v: Verbose mode; lists all files as they are being processed</p></li> |
|
893 </ul></div></block_quote></span><div class="section" title="11.1.2.1. Symbolic modes"> |
|
894 <div class="titlepage"><div><div><h5 class="title"> |
|
895 <a name="id2744336"></a>11.1.2.1. Symbolic modes</h5></div></div></div> |
|
896 <p id="ch3list_tuples_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> |
|
897 <pre class="programlisting">$ chmod [references][operator][modes] file1 ...</pre> |
|
898 <p id="ch3list_tuples_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> |
|
899 <table summary="Symbolic modes" border="1"><colgroup> |
|
900 <col width="14"> |
|
901 <col width="8"> |
|
902 <col width="45"> |
|
903 </colgroup></table> |
|
904 <p id="ch3list_tuples_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> |
|
905 <table summary="Symbolic modes" border="1"><colgroup> |
|
906 <col width="14"> |
|
907 <col width="54"> |
|
908 </colgroup></table> |
|
909 <p id="ch3list_tuples_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> |
|
910 <table summary="Symbolic modes" border="1"><colgroup> |
|
911 <col width="5"> |
|
912 <col width="14"> |
|
913 <col width="48"> |
|
914 </colgroup></table> |
|
915 <p id="ch3list_tuples_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> |
|
916 </div> |
|
917 <div class="section" title="11.1.2.2. Symbolic examples"> |
|
918 <div class="titlepage"><div><div><h5 class="title"> |
|
919 <a name="id2744892"></a>11.1.2.2. Symbolic examples</h5></div></div></div> |
|
920 <p id="ch3list_tuples_16e"></a>Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory:</p> |
|
921 <pre class="programlisting">$ chmod ug+rw mydir |
|
922 $ ls -ld mydir |
|
923 drw-rw---- 2 starwars yoda 96 Dec 8 12:53 mydir</pre> |
|
924 <p id="ch3list_tuples_16f"></a>For a file, remove <span class="emphasis"><em>write</em></span> permissions for all classes:</p> |
|
925 <pre class="programlisting">$ chmod a-w myfile |
|
926 $ ls -l myfile |
|
927 -r-xr-xr-x 2 starwars yoda 96 Dec 8 12:53 myfile</pre> |
|
928 <p id="ch3list_tuples_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> |
|
929 <pre class="programlisting">$ chmod ug=rx mydir |
|
930 $ ls -ld mydir |
|
931 dr-xr-x--- 2 starwars yoda 96 Dec 8 12:53 mydir</pre> |
|
932 </div> |
|
933 <div class="section" title="11.1.2.3. Octal numbers"> |
|
934 <div class="titlepage"><div><div><h5 class="title"> |
|
935 <a name="id2744933"></a>11.1.2.3. Octal numbers</h5></div></div></div> |
|
936 <p id="ch3list_tuples_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> |
|
937 <pre class="programlisting">$ chmod 664 myfile |
|
938 $ ls -l myfile |
|
939 -rw-rw-r-- 1 57 Jul 3 10:13 myfile</pre> |
|
940 <p id="ch3list_tuples_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> |
|
941 <pre class="programlisting">$ chmod 0664 myfile</pre> |
|
942 </div> |
|
943 <div class="section" title="11.1.2.4. Special modes"> |
|
944 <div class="titlepage"><div><div><h5 class="title"> |
|
945 <a name="id2744973"></a>11.1.2.4. Special modes</h5></div></div></div> |
|
946 <p id="ch3list_tuples_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> |
|
947 <p id="ch3list_tuples_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> |
|
948 </div> |
|
949 </div> |
|
950 </div> |
|
951 </div> |
|
952 <div class="section" title="12. Redirection and Piping"> |
|
953 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
954 <a name="id2745018"></a>12. Redirection and Piping</h2></div></div></div> |
|
955 <p id="ch3list_tuples_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> |
|
956 <p id="ch3list_tuples_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> |
|
957 <div class="section" title="12.1. Redirecting standard input and standard output"> |
|
958 <div class="titlepage"><div><div><h3 class="title"> |
|
959 <a name="id2745055"></a>12.1. Redirecting standard input and standard output</h3></div></div></div> |
|
960 <p id="ch3list_tuples_177"></a>Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows:</p> |
|
961 <pre class="programlisting">$ command1 > file1</pre> |
|
962 <p id="ch3list_tuples_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> |
|
963 <pre class="programlisting">$ command1 < file1</pre> |
|
964 <p id="ch3list_tuples_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> |
|
965 <pre class="programlisting">$ command1 < infile > outfile</pre> |
|
966 <p id="ch3list_tuples_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> |
|
967 </div> |
|
968 <div class="section" title="12.2. Piping"> |
|
969 <div class="titlepage"><div><div><h3 class="title"> |
|
970 <a name="id2745124"></a>12.2. Piping</h3></div></div></div> |
|
971 <p id="ch3list_tuples_17b"></a>Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file: |
|
972 A pipeline of three programs run on a text terminal:</p> |
|
973 <pre class="programlisting">$ command1 | command2</pre> |
|
974 <p id="ch3list_tuples_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> |
|
975 <p id="ch3list_tuples_17d"></a>This is equivalent to using two redirects and a temporary file:</p> |
|
976 <pre class="programlisting">$ command1 > tempfile |
|
977 $ command2 < tempfile |
|
978 $ rm tempfile</pre> |
|
979 <p id="ch3list_tuples_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> |
|
980 <pre class="programlisting">$ echo -e "user\npass" | ftp localhost</pre> |
|
981 <p id="ch3list_tuples_17f"></a>This runs the ftp client with input user, press return, then pass.</p> |
|
982 </div> |
|
983 <div class="section" title="12.3. Redirecting to and from the standard file handles"> |
|
984 <div class="titlepage"><div><div><h3 class="title"> |
|
985 <a name="id2745186"></a>12.3. Redirecting to and from the standard file handles</h3></div></div></div> |
|
986 <p id="ch3list_tuples_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> |
|
987 <table summary="Redirecting to and from the standard file handles" border="1"><colgroup> |
|
988 <col width="12"> |
|
989 <col width="13"> |
|
990 <col width="24"> |
|
991 </colgroup></table> |
|
992 <p id="ch3list_tuples_18d"></a>For example:</p> |
|
993 <pre class="programlisting">$ command1 2> file1</pre> |
|
994 <p id="ch3list_tuples_18e"></a>executes <span class="emphasis"><em>command1</em></span>, directing the standard error stream to <span class="emphasis"><em>file1</em></span>.</p> |
|
995 <p id="ch3list_tuples_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> |
|
996 <p id="ch3list_tuples_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> |
|
997 <pre class="programlisting">$ find / -name .profile > results 2>&1</pre> |
|
998 <p id="ch3list_tuples_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> |
|
999 <p id="ch3list_tuples_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> |
|
1000 <p id="ch3list_tuples_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> |
|
1001 <pre class="programlisting">$ find / -name .profile 2>&1 | less</pre> |
|
1002 <p id="ch3list_tuples_194"></a>A simplified form of the command:</p> |
|
1003 <pre class="programlisting">$ command > file 2>&1</pre> |
|
1004 <p id="ch3list_tuples_195"></a>is:</p> |
|
1005 <pre class="programlisting">$ command &>file</pre> |
|
1006 <p id="ch3list_tuples_196"></a>or:</p> |
|
1007 <pre class="programlisting">$command >&file</pre> |
|
1008 </div> |
|
1009 <div class="section" title="12.4. Chained pipelines"> |
|
1010 <div class="titlepage"><div><div><h3 class="title"> |
|
1011 <a name="id2745390"></a>12.4. Chained pipelines</h3></div></div></div> |
|
1012 <p id="ch3list_tuples_197"></a>The redirection and piping tokens can be chained together to create complex commands. For example:</p> |
|
1013 <pre class="programlisting">$ ls | grep '\.sh' | sort > shlist</pre> |
|
1014 <p id="ch3list_tuples_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> |
|
1015 </div> |
|
1016 <div class="section" title="12.5. Redirect to multiple outputs"> |
|
1017 <div class="titlepage"><div><div><h3 class="title"> |
|
1018 <a name="id2745424"></a>12.5. Redirect to multiple outputs</h3></div></div></div> |
|
1019 <p id="ch3list_tuples_199"></a>The standard command <span class="emphasis"><em>tee</em></span> can redirect output from a command to several destinations.</p> |
|
1020 <pre class="programlisting">$ ls -lrt | tee xyz</pre> |
|
1021 <p id="ch3list_tuples_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> |
|
1022 </div> |
|
1023 </div> |
|
1024 <div class="section" title="13. More Text Processing"> |
|
1025 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
1026 <a name="id2745454"></a>13. More Text Processing</h2></div></div></div> |
|
1027 <div class="section" title="13.1. grep"> |
|
1028 <div class="titlepage"><div><div><h3 class="title"> |
|
1029 <a name="id2745463"></a>13.1. grep</h3></div></div></div> |
|
1030 <p id="ch3list_tuples_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> |
|
1031 <div class="section" title="13.1.1. Usage"> |
|
1032 <div class="titlepage"><div><div><h4 class="title"> |
|
1033 <a name="id2745494"></a>13.1.1. Usage</h4></div></div></div> |
|
1034 <p id="ch3list_tuples_19c"></a>This is an example of a common <span class="emphasis"><em>grep</em></span> usage:</p> |
|
1035 <pre class="programlisting">$ grep apple fruitlist.txt</pre> |
|
1036 <p id="ch3list_tuples_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> |
|
1037 <p id="ch3list_tuples_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> |
|
1038 <pre class="programlisting">$ grep -i apple fruitlist.txt</pre> |
|
1039 <p id="ch3list_tuples_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> |
|
1040 <p id="ch3list_tuples_1a0"></a>To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match):</p> |
|
1041 <pre class="programlisting">$ grep -w apple fruitlist.txt</pre> |
|
1042 <p id="ch3list_tuples_1a1"></a>Regular expressions can be used to match more complicated queries.</p> |
|
1043 <div class="section" title="13.1.1.1. Variations"> |
|
1044 <div class="titlepage"><div><div><h5 class="title"> |
|
1045 <a name="id2745573"></a>13.1.1.1. Variations</h5></div></div></div> |
|
1046 <p id="ch3list_tuples_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> |
|
1047 <p id="ch3list_tuples_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> |
|
1048 <p id="ch3list_tuples_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> |
|
1049 </div> |
|
1050 </div> |
|
1051 </div> |
|
1052 <div class="section" title="13.2. tr"> |
|
1053 <div class="titlepage"><div><div><h3 class="title"> |
|
1054 <a name="id2745652"></a>13.2. tr</h3></div></div></div> |
|
1055 <p id="ch3list_tuples_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> |
|
1056 <p id="ch3list_tuples_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> |
|
1057 <pre class="programlisting">$ tr 'abcd' 'jkmn'</pre> |
|
1058 <p id="ch3list_tuples_1a7"></a>maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'.</p> |
|
1059 <p id="ch3list_tuples_1a8"></a>Sets of characters may be abbreviated by using character ranges. The previous example could be written:</p> |
|
1060 <pre class="programlisting">$ tr 'a-d' 'jkmn'</pre> |
|
1061 <p id="ch3list_tuples_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> |
|
1062 <p id="ch3list_tuples_1aa"></a>The <span class="emphasis"><em>-c</em></span> flag complements the first set of characters.</p> |
|
1063 <pre class="programlisting">$ tr -cd '[:alnum:]'</pre> |
|
1064 <p id="ch3list_tuples_1ab"></a>therefore removes all non-alphanumeric characters.</p> |
|
1065 <p id="ch3list_tuples_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> |
|
1066 <pre class="programlisting">$ tr -s '\n' '\n'</pre> |
|
1067 <p id="ch3list_tuples_1ad"></a>replaces sequences of one or more newline characters with a single newline.</p> |
|
1068 <p id="ch3list_tuples_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> |
|
1069 <pre class="programlisting">$ tr -d '\r'</pre> |
|
1070 <p id="ch3list_tuples_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> |
|
1071 <p id="ch3list_tuples_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> |
|
1072 </div> |
|
1073 </div> |
|
1074 <div class="section" title="14. Elementary Regex"> |
|
1075 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
1076 <a name="id2745806"></a>14. Elementary Regex</h2></div></div></div> |
|
1077 <p id="ch3list_tuples_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> |
|
1078 <p id="ch3list_tuples_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> |
|
1079 <p id="ch3list_tuples_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> |
|
1080 <p id="ch3list_tuples_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> |
|
1081 <table summary="Elementary Regex" border="1"><colgroup> |
|
1082 <col width="13"> |
|
1083 <col width="60"> |
|
1084 </colgroup></table> |
|
1085 <div class="section" title="14.1. Lazy quantification"> |
|
1086 <div class="titlepage"><div><div><h3 class="title"> |
|
1087 <a name="id2746019"></a>14.1. Lazy quantification</h3></div></div></div> |
|
1088 <p id="ch3list_tuples_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> |
|
1089 <pre class="programlisting">Another whale explosion occurred on <January 26>, <2004>.</pre> |
|
1090 <p id="ch3list_tuples_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 <span style="color: red"><title_reference>*</title_reference></span> 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> |
|
1091 <p id="ch3list_tuples_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> |
|
1092 </div> |
|
1093 </div> |
|
1094 <div class="section" title="15. One Liners"> |
|
1095 <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
|
1096 <a name="id2746095"></a>15. One Liners</h2></div></div></div> |
|
1097 <p id="ch3list_tuples_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> |
|
1098 <p id="ch3list_tuples_1cd"></a>The one liner can be</p> |
|
1099 <span style="color: red"><block_quote><div class="orderedlist"><ol class="orderedlist" type="1"> |
|
1100 <li class="listitem"><p id="ch3list_tuples_1ce"></a>An expression written in the language of the shell.</p></li> |
|
1101 <li class="listitem"><p id="ch3list_tuples_1cf"></a>The invocation of an interpreter together with program source for the interpreter to run.</p></li> |
|
1102 <li class="listitem"><p id="ch3list_tuples_1d0"></a>The invocation of a compiler together with source to compile and |
|
1103 instructions for executing the compiled program.</p></li> |
|
1104 </ol></div></block_quote></span><p id="ch3list_tuples_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> |
|
1105 <p id="ch3list_tuples_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> |
|
1106 <p id="ch3list_tuples_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> |
|
1107 <pre class="programlisting">“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> |
|
1108 <p id="ch3list_tuples_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> |
|
1109 <p id="ch3list_tuples_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> |
|
1110 <p id="ch3list_tuples_1d6"></a>Here are the first few of them:</p> |
|
1111 <span style="color: red"><block_quote><div class="orderedlist"><ol class="orderedlist" type="1"> |
|
1112 <li class="listitem"> |
|
1113 <p id="ch3list_tuples_1d7"></a>Print the total number of input lines:</p> |
|
1114 <p id="ch3list_tuples_1d8"></a>END { print NR }</p> |
|
1115 </li> |
|
1116 <li class="listitem"> |
|
1117 <p id="ch3list_tuples_1d9"></a>Print the tenth input line:</p> |
|
1118 <p id="ch3list_tuples_1da"></a>NR == 10</p> |
|
1119 </li> |
|
1120 <li class="listitem"> |
|
1121 <p id="ch3list_tuples_1db"></a>Print the last field of every input line:</p> |
|
1122 <p id="ch3list_tuples_1dc"></a>{ print $NF }</p> |
|
1123 </li> |
|
1124 </ol></div></block_quote></span><p id="ch3list_tuples_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> |
|
1125 <p id="ch3list_tuples_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> |
|
1126 <pre class="programlisting">main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return |
|
1127 *t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}</pre> |
|
1128 <p id="ch3list_tuples_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> |
|
1129 <p id="ch3list_tuples_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> |
|
1130 <pre class="programlisting">$ prog foo 'f??'; echo $? |
|
1131 |
|
1132 $ prog 'best short program' '??st*o**p?*'; echo $?</pre> |
|
1133 <p id="ch3list_tuples_1e1"></a>Here is a one line shell script to show directories:</p> |
|
1134 <pre class="programlisting">$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'</pre> |
|
1135 </div> |
|
1136 </div> |
|
1137 </div></body> |
|
1138 </html> |