In this tutorial, we will show you how to use regular expressions to search for text patterns in a file. Next, we'll introduce you to the file system hierarchy standard (filesystem hierarchy Standard, or FHS), and show you how to locate files on your system. We will then show you how to fully control the Linux process by running the Linux process in the background, listing the process manifest, removing the process from the terminal, and more. Finally, we'll give you a brief introduction to shell piping, redirection, and text processing commands. After completing this tutorial, you will have a solid grasp of the basics of Linux management and start learning some of the more advanced Linux system management skills.
one. About this tutorial
What is covered in this tutorial.
In this tutorial, we will show you how to use regular expressions to search for text patterns in a file. Next, we'll introduce you to the file system hierarchy standard (filesystem hierarchy Standard, or FHS), and show you how to locate files on your system. We will then show you how to fully control the Linux process by running the Linux process in the background, listing the process manifest, removing the process from the terminal, and more. Finally, we'll give you a brief introduction to shell piping, redirection, and text processing commands.
After completing this tutorial, you will have a solid grasp of the basics of Linux management and start learning some of the more advanced Linux system management skills.
At the end of this series of tutorials (8 parts), you will have the knowledge necessary to become a Linux system administrator, and if you choose to take the LPI exam, you will be able to obtain the Lpic Level 1 certificate issued by the Linux Professional Institute.
The LPI logo is the trademark of the Linux Professional Institute.
Should I study this tutorial?
This tutorial (also part 2nd of this series) is ideal for people who have good bash basics and want to get a thorough introduction to basic Linux management tasks. If you are new to Linux, we recommend that you complete the 1th part of this tutorial series before continuing with your studies. For some people, many of the content in this tutorial are new, and more experienced Linux users may find this tutorial a way to "get the most out of their basic Linux management skills."
There are three more other tutorials in this series: Part 1:linux Foundation Parts 3: Middle Management Parts 4: Advanced Management
about the author
For technical questions about the content of this tutorial, please contact these authors: Daniel robbins,drobbins@gentoo.org Chris houser,chouser@gentoo.org Aron Griffis, agriffis@gentoo.org
Daniel Robbins , who lives in New Mexico State Albuquerque, is the president and CEO of Gentoo Technologies, Inc., and he has a Gentoo Linux, an advanced Linux for PCs. , as well as the Portage system, is a next-generation porting system for Linux. He was also a contributor to several books published by Macmillan Caldera OpenLinux Unleashed, SuSE Linux unleashed and Samba unleashed. Daniel has had a bond with the computer since his second year, when he first came into contact with the Logo programming language and indulged in Pac man games. That may be why he has been the chief graphic designer of SONY Electronic publishing/psygnosis. Daniel likes to spend time with his wife Mary and their daughter Hadassah.
Chris Houser , known among many of his friends as "deception experts", has been a champion of UNIX since he joined the Computer Science network management team at the University of Indiana in 1994, where he received a bachelor's degree in Computer Science and mathematics. Since then, he has been committed to WEB application programming, user interface design, professional video software support, and currently engaged in Tru64 UNIX device driver programming in Compaq. He is also involved in a variety of free software projects, and the most recent project is Gentoo Linux. He lives in New Hampshire with his wife and two cats.
Aron Griffis graduated from Taylor University with a degree in computer science and was awarded the honorary title of "The future founder of the ideal UNIX commune". To achieve that goal, Aron works in Compaq to write Tru64 UNIX network drivers and play the piano or develop Gentoo Linux in his spare time. He and his wife Amy (also a UNIX engineer) live in the Nashuni of New Hampshire State.
two. Regular Expressions
what is a regular expression.
A regular expression, also known as "regex" or "regexp", is a special syntax used to describe text patterns. On Linux systems, regular expressions are often used to find patterns of text and perform "search-replace" operations and other functions on text streams.
comparison with the Glob
When we see a regular expression, you may find that the syntax of the regular expression looks similar to the "FileName matching substitution" syntax that we studied in the previous tutorial (see "Part 1th", which is listed in the "Resources" section at the end of this tutorial). But don't let it deceive you; their similarity is superficial. Although regular expressions and file name matching replacement patterns may look similar, they are two fundamentally different types.
Simple substring
Remember that warning, let's take a look at the most basic regular expressions, simple substrings. To do this, we're going to use grep, which is a scan of the contents of a file to find the appropriate command for a particular regular expression. grep prints each row that matches the regular expression and ignores each row that does not match:
$ grep bash/etc/passwd
operator:x:11:0:operator:/root:/bin/bash
root:x:0:0::/root:/bin/bash
ftp:x : 40:1::/home/ftp:/bin/bash
In the above command, the first argument of grep is a regular expression, and the second argument is a filename. grep reads each row in the/etc/passwd and applies simple substring regular expression bash to it to find a match. If a match is found, grep prints the entire row, otherwise the row is ignored.
In general, if you are searching for a substring, you may not provide any "special" characters, but simply specify the text verbatim. It is only necessary to do something special if the substring contains + 、.、 *,, or (in such cases, these characters need to be enclosed in quotes and use backslashes in front of them). Here are a few other examples of simple substring regular Expressions:/tmp (scan for text string/tmp) "[Box]" (Scan find text string [box]) "*funny*" (Scan find text string *funny*) "ld.so" (scan for text string ld.so)
Meta character
Using regular expressions, you can use a meta character to perform a much more complex search than the examples we have studied so far. One of these meta characters is. (point), which matches any single character:
$ grep dev.hda/etc/fstab
/dev/hda3 / reiserfs noatime,ro 1 1
/dev/hda1 /boot ReiserFS noauto,noatime,notail 1 2
/dev/hda2 swap SW 0 0
#/dev/hda4 /mnt/extra ReiserFS NOATIME,RW 1 1
In this example, the literal text Dev.hda does not appear in any row in the/etc/fstab. However, grep scans these rows without finding the literal Dev.hda string, but instead looks for the dev.hda pattern. Please remember. will match any single character. As you can see,. Metacharacters is functionally equivalent to Glob extensions? The working principle of metacharacters.
Use []
If we want to compare with. To match characters more specifically, we can use and (square brackets) to specify a subset of characters to match:
$ grep dev.hda[12]/etc/fstab
/dev/hda1 /boot reiserfs noauto,noatime,notail 1 2
/dev/hda2 swap SW 0 0
As you can see, this particular syntax has the same effect as [] in the "glob" file name extension. Again, this is one of the difficulties of learning regular expressions-this syntax is similar to the "glob" file name extension syntax, but it is not the same, and it often confuses people who learn regular expressions.
use [^]
By keeping the back followed by a ^, you can make the opposite of the meaning in square brackets. In this case, the brackets will match any character not listed in the square brackets. Again, note that we use [^] in regular expressions and use [!] in Glob:
$ grep dev.hda[^12]/etc/fstab
/dev/hda3 / reiserfs noatime,ro 1 1
#/dev/hda4 /mnt/extra ReiserFS noatime,rw 1 1
difference Syntax
It is important to note that the syntax inside square brackets is fundamentally different from the syntax in other parts of regular expressions. For example, if you place one inside square brackets. , then it allows square brackets with text. Match, just like 1 and 2 in the example above. In contrast, unless there is a prefix, the text outside the square brackets. is interpreted as a meta character. By entering the following command, we can use this fact to print a list of all the rows in the/etc/fstab that contain text strings Dev.hda:
$ grep dev[.] Hda/etc/fstab
$ grep "Dev.hda"/etc/fstab
None of these two regular expressions can match any of the rows in your/etc/fstab file.
"*" Meta character
Some metacharacters themselves do not match any character, but they modify the meaning of the previous character. One such metacharacters is * (asterisk), which is used to match 0 or more occurrences of the previous character. Here are some examples: ab*c (matching with ABBBBC but not matching ABQC) ab*c (matching with ABC but not matching with ABBQBBC) ab*c (matching with AC but not with CBA) B[cq]*e (matches with BQE but does not match EB) B[cq ]*e (matched with BCCQQE but not matched with BCCC) b[cq]*e Bqqcce (matching cqe with b[cq]*e). * (matches any string) Bbbeee (matches any string starting with Foo)
The AC line matches the regular expression ab*c, because the asterisk also allows the preceding expression (b) to appear 0 times. Note that the method used to interpret the * regular expression metacharacters is fundamentally different from the method that interprets the * glob character.
start and end of line
The last few metacharacters we're going to detail here are the ^ and $ metacharacters, which are used to match the start and end of the line, respectively. By using a ^ at the beginning of the regular expression, you can "anchor" your pattern at the beginning of the line. In the following example, we use the ^# regular expression to match any line starting with the # character:
$ grep ^#/etc/fstab
#/etc/fstab:static file system information.
#
full line Regular expression
You can combine ^ and $ to match a complete row. For example, the following regular expression starts with the # character and takes a. The end of the character line matches, and there can be any number of other characters between them:
$ grep ' ^#.*.$ '/etc/fstab
#/etc/fstab:static file system information.
In the example above, we enclose our regular expression in single quotes to prevent the shell from interpreting $. Without using single quotes, grep does not even have a chance to view $,$ and disappears from our regular expression.
Three. FHS and find Files
File System Hierarchy Standards
/(root directory)/boot (static file for boot loader)/dev (device file)/etc (host-specific system configuration)/lib (basic shared library and Core module)/mnt (temporary mount file system mount)/opt (attached application package)/ Sbin (Basic system binaries)/tmp (temporary file)/usr (secondary hierarchy)/var (variable data)
two separate FHS categories
The FHS layout specification is based on the existence of two separate file categories: shareable and unshared, and variable and static. Shareable data can be shared between hosts; non-shared data is specific to a given host (for example, a configuration file). Variable data can be modified, and static data cannot be modified (except during the system installation and maintenance phase).
The following table outlines four possible combinations and lists examples of directories that match those categories. This table is also directly taken from the FHS specification:
+---------+-----------------+-------------+
| | can be shared | not Shared |
+---------+-----------------+-------------+
| static |/usr |/etc | | |/opt |/boot |
+---------+-----------------+-------------+
| variable |/var/mail |/var/run | | |/var/ Spool/news | /var/lock |
+---------+-----------------+-------------+
secondary hierarchy in/usr
Under/usr, you will find a secondary hierarchy that looks very similar to the root file system. When the machine is open and running, the presence of/USR does not matter, so you can share it on the network ("shareable"), or mount it ("static") from the CD-ROM. Most Linux settings do not utilize the share of/usr, but it is valuable to understand the usefulness of the distinction between the primary hierarchy in the root directory and the secondary hierarchy in/usr.
That's all we have to say about filesystem hierarchy Standard. The document itself is very readable, so you should take a look. We promise that if you read it, you will understand more about the Linux file system.
Find Files
A Linux system usually contains hundreds of thousands of files. You may be very smart and never lose any of them, but it is more likely that you will occasionally need help finding a file. There are several different tools on Linux to find files. The following demo will introduce you to them and help you choose the tools that are right for your work.
PATH
When you run a program on the command line, bash actually searches the directory list to find the program you are requesting. For example, when you enter Ls,bash essentially do not know that the LS program is located in/usr/bin. However, bash references an environment variable named PATH, which is a colon-delimited list of directories. We can check the value of the PATH:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/x11r6/bin
Given the value of PATH (yours can be different), bash first checks/usr/local/bin and then/use/bin to search for the LS program. LS is most likely to be saved in/usr/bin, so bash stops there.
Modify Path You can expand it by assigning elements to the path on the command line:
$ path= $PATH: ~/bin
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/x11r6/bin:/home/ Agriffis/bin
You can also remove elements from the PATH, although this is not easy because you cannot reference existing $PATH. The best way to do this is to simply enter the new PATH you want:
$ path=/usr/local/bin:/usr/bin:/bin:/usr/x11r6/bin:~/bin
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr /x11r6/bin:/home/agriffis/bin
about "which."
By using which, you can see if there is a given program in PATH. For example, we discovered from the following command that the Linux system has no (normal) sense:
$ which sense
which:no sense in (/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/x11r6/bin)
In this example, we successfully locate the LS:
$ which LS
/usr/bin/ls
"Which-a"
Finally, you should know the-a flag, which enables which to show you all instances of a given program in your PATH:
$ which-a ls
/usr/bin/ls
/bin/ls
whereis
If you are not only interested in the location of the program, but also want to find more information, you can try the Whereis program:
$ whereis ls
ls:/bin/ls/usr/bin/ls/usr/share/man/man1/ls.1.gz
Here we see LS appearing in two common binary positions/bin and/usr/bin. In addition, we were told that the manual page was positioned in/usr/share/man. If you want to enter man LS, then this is the man page you will see.
The Whereis program also has the ability to search for source code, specify alternate search paths, and search for unusual items. For further information, refer to the Whereis man page.
Find
The Find command is another tool in your toolbox. With find, you are not limited to programs; You can search for any file you want by using a variety of search criteria. For example, to search for a file named README under the/usr/share/doc directory:
$ find/usr/share/doc-name README
/usr/share/doc/ion-20010523/readme
/usr/share/doc/bind-9.1.3-r6/ Dhcp-dynamic-dns-examples/readme
/usr/share/doc/sane-1.0.5/readme Find
and wildcard characters
You can use the "Glob" wildcard in-name arguments if you refer to them in double quotes or are escaped with backslashes (so that they can be passed completely to find rather than being expanded by bash). For example, we might want to search for a README file with an extension:
$ find/usr/share/doc-name readme*
/usr/share/doc/iproute2-2.4.7/readme.gz
/usr/share/doc/iproute2-2.4.7/ readme.iproute2+tc.gz
/usr/share/doc/iproute2-2.4.7/readme.decnet.gz
/usr/share/doc/iproute2-2.4.7/ examples/diffserv/readme.gz
/usr/share/doc/pilot-link-0.9.6-r2/readme.gz
/usr/share/doc/ gnome-pilot-conduits-0.8/readme.gz
/usr/share/doc/gimp-1.2.2/readme.i18n.gz
/usr/share/doc/ gimp-1.2.2/readme.win32.gz
/usr/share/doc/gimp-1.2.2/readme.gz
/usr/share/doc/gimp-1.2.2/ README.perl.gz
[578 additional lines snipped]
ignore case in find