Experience the difficulties and experiences:1. In the course of operation, the Youdao job is to create a opt/forloutest file, and I have established a
Opt file, cd opt, but show not a directory, by reading "Linux embedded" this book using mkdir to create a directory, probably because the OPT file exists, and cannot create a new directory
With online search, learn that you should create opt folders instead of files.
2. Ibid, in the establishment of fortest this file, accidentally pressed two times touch fortest, listed files, found that two forloutest files, modify access permissions, even if the modification two times, was modified only one file, the other file can not be modified, And it doesn't seem to work with RM Delete.
Experience: sometimes in accordance with the guidance of the experiment, but not every step is to tell us, so before doing the experiment it is best to combine the knowledge of the textbook. And when I do the experiment, I think I have a lot to learn.
iv. Linux directory structure and file basic Operation1, the Linux file organization directory structure. Linux is a tree-shaped directory structure to build the entire systemLinux disks are "hung on" (mounted on) the directoryeach directory can not only use the file system of the local disk partition, but also the file system on the network. FHS (English: Filesystem Hierarchy Standard Chinese: File system hierarchy Standards)
2, relative path and absolute path.
Knowledge Points:
Go to the top level directory:$ cd ..
Go to your "home" directory:$ cd ~ # 或者 cd /home/<你的用户名>
Use to pwd get the current path
Absolute Path , the full path to the root "/" directory as the starting point for the directory you want to go to
The expression form, such as: /usr/local/bin , represents the Bin directory in the local directory in the USR directory under the root directory.
relative path, relative to the path of your current directory, the relative path is the current directory . starting point, the directory you want to end,
The expressions are as follows: usr/local/bin (this assumes your current directory is the root directory).
Experiment:
3, the movement of files, copying, renaming, editing and other operations. 1. Create a new blank file
touchCreate a blank file with a command
Create a blank file named Test, because you do not have permissions in other directories, so you need to cd ~ switch back to the user's /home/shiyanlou directory first:
New Catalog
Use the Make mkdir directories command to create an empty directory or to specify permission properties for creating a directory
2. Copying and Copying files
Use cp the (copy) command to copy a file or directory to the specified directory.
3. Delete deleted files
rmDelete a file or directory using the (remove files or directories) command:
Using rm Delete directly will display a prompt,
To ignore this hint, delete the file directly, you can use the -f parameter to force delete
Delete directory
As with the copy directory, to delete a directory, you also need to add -r or -R parameter
4. Move file and file renameMoving files
Use mv the (move or rename files) command to move the file (cut).
5. View File Usage
cat,
tacAnd
nlcommand to view files
catFor positive sequence display, tac reverse display.
Standard input and output: when we execute a shell command line normally will automatically open three standard files, namely standard input file (stdin), default corresponding to the terminal's keyboard, standard output file (STDOUT) and standard error output file (stderr), All two files correspond to the screen being redirected to the terminal so that we can see the output directly. The process will get input data from the standard input file, output normal output data to the standard output file, and send the error message to the standard error file.
-nParameter Display line number
nlcommand, add line numbers and print
A few common parameters:
-b : 指定添加行号的方式,主要有两种: -b a:表示无论是否为空行,同样列出行号("cat -n"就是这种方式) -b t:只列出非空行的编号并列出(默认为这种方式)-n : 设置行号的样式,主要有三种: -n ln:在行号字段最左端显示 -n rn:在行号字段最右边显示,且不加 0 -n rz:在行号字段最右边显示,且加 0-w : 行号字段占用的位数(默认为 6 位)
Use
moreAnd
lesscommand paging to view files
tailcommand, and have to mention it a very good parameter -f , this parameter can be implemented to read the contents of a file and display. This allows us to dynamically view the role of the log as real-time monitoring
6. View File Types
You typically use file commands to view the types of files
7. Edit the file
Dedicated command-line editor such as (Emacs,vim,nano)
Experiment five environment variables and file searchI. Environment Variables
Use declare the command to create a variable named tmp:
Variable names can only be English letters, numbers, or underscores, and cannot begin with a number.
Environment variables are scoped to larger than custom variables
The environment variables of the Shell Act on itself and its child processes.
There are three types of environment variables that we will typically relate to:
- The current shell process private user-defined variables, such as the TEMP variable we created above, are only valid in the current shell.
- The built-in variables of the Shell itself.
- An environment variable exported from a custom variable.
There are also three commands related to the above three environment variables,, set , env export . These three commands are similar and can be used to print related environment variables, except that they are related to different ranges of environment variables, as described in the following table:
| Command |
Description |
set |
Displays all environment variables for the current shell, including built-in environment variables (related to shell appearance, etc.), user-defined variables, and exported environment variables |
env |
Displays the environment variables that are relevant to the current user, and lets the command run in the specified environment |
export |
Displays variables that are exported from the Shell to an environment variable, and can also be exported to a custom variable as an environment variable |
4. Add a custom path to the "path" environment variable
Each user's home directory has a Shell that executes a configuration script by default each time it starts to initialize the environment, including adding some user-defined environment variables, and so on. Zsh profile is, the .zshrc corresponding Bash configuration file is.bashrc
5. Modifying and deleting existing variable variables
There are several ways to modify variables:
| How variables are set |
Description |
${变量名#匹配字串} |
Start the match from the back to delete the shortest data matching the matched string |
${变量名##匹配字串} |
Start the match from the back to delete the longest data matching the matched string |
${变量名%匹配字串} |
Match from tail forward to delete the shortest data matching string |
${变量名%%匹配字串} |
Match from tail forward to delete maximum data matching string |
${变量名/旧的字串/新的字串} |
Replace the first string that matches the old string with a new string |
${变量名//旧的字串/新的字串} |
Replace all strings that match the old string with a new string |
For example, modify the environment variables we added to PATH earlier.
Second, search documents
Search-related commands are commonly used in the following sections,,, whereis which find locate .
whereisOnly binary (-B), man Help file (-m), and source code file (-s) can be searched. You can use the command if you want to get a more comprehensive search result locate .
whichitself is a shell built-in command that we typically use which to determine if a specified software is installed because it only PATH searches for the command from the path specified by the environment variable:
findShould be the most powerful of these commands, not only can be found by file type, file name and can be based on the properties of the file (such as the file's timestamp, file permissions, etc.) to search.
Note the path to the Find command is the first parameter, and the basic command format is find [path] [option] [action]
Homework:
"Digital Rain" in the "Matrix" movie full of screen code, you can also easily achieve this effect in Linux
Experiment six document packaging and compression experiment introduction
The use of Zip,rar,tar is described in the compression/decompression tools commonly used on Linux.
One, file packaging and decompression 1.
zipCompression Packager
In the first line of the command, the -r parameter indicates that the recursive package contains the entire contents of the subdirectory, and the -q parameters are expressed in quiet mode, that is, not outputting information to the screen, -o indicating the output file, followed by packaging the output filename immediately thereafter. Later use du the command to view the size of the packaged file
2. Use
unzipcommand to unzip the zip file
Do not want to unzip just want to view the contents of the compressed package you can use the -l parameters
Use -O the (English letter, capital O) parameter to specify the encoding type:
3.
rarPackage Compression command
- To create a compressed package or add a file to a compressed package from a specified file or directory:
- Use
a parameters to add a directory ~ to an archive, which is created automatically if the file does not exist.
Homework: When it's cold, it's good to have a stove.
Experiment seven file system operation with Disk Management one, simple file system operation 1. View disk and directory capacity use the DF command to view the capacity of a disk
Ii. Simple Disk Management 1. Introduction to creating a virtual Disk DD command
Mount a disk to a directory tree using the Mount command to establish an association of mirroring with a loopback device with the Losetup command
Then use mkfs the formatting of each partition
Experimental eight command execution sequence control and pipeline experiment Introduction
Sequential execution, select execution, pipe, cut command, grep command, WC command, sort command, etc.
I. Control of order execution sequence 1. Execute multiple commands sequentially 2. Selective execution commands
&&is used to implement selective execution, which means that if the previous command execution results (not the contents of the terminal output, but the result of the command execution state) return 0 to perform the following, otherwise do not execute, you can get the result of the $? last command from the environment variable
Second, the pipeline
Pipelines are divided into anonymous and named pipes.
In the use of some filter program is often used in the anonymous pipeline, the command line is represented by the | delimiter, | in the previous content we have been used many times. A named pipe is simply a named pipe, usually a named pipe in the source program. Below we will use some common "filter programs" that can be used to help you get used to the plumbing.
The Cut command, which prints a field of each line of the grep command, finds the matching string in the text or stdin the WC command, a simple and compact counting tool. Sort Sort command uniq to reset command
uniqCommands can be used to filter or output duplicate rows.
Experiment nine, simple text processing one, commonly used Text Processing command two, Text Processing command 1.tr command
The TR command can be used to delete some text from a piece of textual information. or convert it.
2.col command
The col command can be Tab built with a number of equivalent spaces, or reverse the operation.
3.join command
Used to merge the rows that contain the same content in two files.
4.paste command
pasteThis command join is similar to a command, which simply merges multiple files together to separate them without comparing the data Tab .
Experimental ten-stream redirection one, data stream redirection 1. Simple redirection
Be careful not to confuse pipelines and redirects, the pipeline defaults to the input of the output of the previous command to the next command , and redirection usually requires a file to establish a connection of two commands
2. Use
teecommand to redirect to multiple files at the same time
In addition to redirecting the output to a file, you will need to print the information in the terminal
3. Permanent Redirection
execThe purpose of the command is to replace the current shell with the specified command, to replace the current process with a process, or to specify a new redirect 4. Create output file descriptor
Use exec the command to create a new file descriptor
5. Close the file descriptor 6. The output of a fully shielded command
In Linux there is a device file that becomes a "black hole", so the data that is imported into it will be "swallowed".
In a UNIX-like system, a/dev/null, or empty device, is a special device file that is typically used to discard unwanted output streams or as empty files for input streams, which are usually done by redirection. Reading it will immediately get an EOF.
7. Using Xargs to split the parameter list
Xargs is a common command for UNIX and UNIX-like operating systems. It does this by converting the argument list to small pieces to pass to other commands to avoid the problem of too long a parameter list.
Experiment 11 the basis of regular expressions
is Expressions , also known as formal expressions, formal representations, regular expression, regular expressions, general notation
A regular expression is often referred to as a pattern,which is used to describe or match a series of strings that conform to a certain syntactic rule.
Choose
|Vertical separators indicate selections, such as "Boy|girl" to Match "boy" or "Girl"
Quantity limit
Quantity limit Except for the example we use * , + Plus, ? question mark, . dot, if the number qualifier in a pattern appears once and only once:
Priority level
The priority is from top to bottom, from left to right, descending:
repThe command is used to print the matching pattern string in the output text, which uses regular expressions as criteria for pattern matching. grepsupport for three regular expression engines, specified by three parameters, respectively:
| Parameters |
Description |
-E |
POSIX extended regular expression, ERE |
-G |
POSIX basic regular expression, BRE |
-P |
Perl Regular Expressions, PCRE |
An introduction to the basic experiment of regular expression
Although the title of our section is a regular expression, the actual section of this experiment is just an introduction grep to sed awk These three commands, while regular expressions are used as one of these three commands (the command output can contain regular expressions). The regular expression itself has a lot of content and it needs to be explained clearly in a separate course, but the relevant content in this section can often meet the needs of many situations.
To learn more about using regular expressions, here is the basis for regular expressions.
Examples of SED operations
Let's find a text file for practice:
$ cp /etc/passwd ~
Print the specified line
# 打印2-5行$ nl passwd | sed -n ‘2,5p‘# 打印奇数行$ nl passwd | sed -n ‘1~2p‘
In-line substitution
# 将输入文本中"shiyanlou" 全局替换为"hehe",并只打印替换的那一行,注意这里不能省略最后的"p"命令$ sed -n ‘s/shiyanlou/hehe/gp‘ passwd
Note: in-line substitution can be used in conjunction with regular expressions.
Inline substitution
$ nl passwd | grep "shiyanlou"# 删除第21行$ sed -n ‘21c\www.shiyanlou.com‘ passwd
So much about the sed command, if you want to learn more about the advanced uses of SED, you can refer to the following links:
- A concise tutorial on SED
- Sed single-line scripting Quick Reference
- SED Complete Brochure
Iv. awk Text Processing language
You might be amazed to see the headline above, is it a "language" we're going to learn here, or rather, we're going to learn the awk text-processing language here, but we're not going to learn more about it here, awk or because of the reason, it's too powerful, Its application everywhere, we can not be here with a short text description of everything, if you have a goal to become a Linux system administrator, do want to learn awk , you do not worry, the experimental building will be on the line after the Linux system Administrator learning Path, there will be a separate about 正则表达式 , awk, and sed Other related courses, please look forward to it. Below, let's awk take the introductory Experience section, which describes some awk of the most common operations.
1.awk Introduction
AWKis an excellent text processing tool, one of the most powerful data processing engines available in Linux and UNIX environments. Its name comes from its founder Alfred Aho (Alfred Aihou), Peter Jay Weinberger (Peter Wimberg) and Brian The first letter of the Wilson Kernighan (Blaine Collingham) surname. In the AWK programming language, the three-bit creator has formally defined it as "style scanning and processing language." It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features. In the simplest sense, awk is a programming language tool for working with text.
On most Linux distributions, we're actually using the GNU version of Gawk (GNU Awk,awk), in our environment Ubuntu, which is provided by default is Mawk, but we can usually use the awk command directly (the awk language interpreter), Because the system has created a symbolic link that awk points to Mawk.
$ ll /usr/bin/awk
Nawk: In the mid 1980s, the awk language was updated and, to varying degrees, replaced with an enhanced version called Nawk (New awk). The old awk interpreter still exists in many systems, but it is usually installed as the Oawk (older awk) command, while the Nawk interpreter is installed as the primary awk command, or you can use the Nawk command. Dr. Kernighan is still maintaining the Nawk, which, like Gawk, is open source and available free of charge; Gawk: is an open source implementation of the AWK interpreter for GNU Project. Although the earlier GAWK release was an alternative to the old AWK, it was constantly updated to include Nawk features; Mawk is also an interpreter for the AWK programming language, Mawk the awk language as defined in POSIX 1003.2 (draft 11.3), contains some features not mentioned in the AWK manual, while Mawk provides a small number of extensions, and Mawk is said to be the fastest AWK
Some basic concepts of 2.awk
All of AWK's operations are done based on the pattern (pattern)-action (action) pair, as in the following form:
$ pattern {action}
As you can see, as in many programming languages, it encloses all action actions with a pair of {} curly braces. Where pattern is usually a "relational" or "regular expression" representing the text used to match the input, the action is the action that will be performed after the match. In a full awk operation, both can have only one, and if there is no pattern the default is to match all the text entered, and if there is no action, the default is to print the matching content to the screen.
awkThe way to work with text is to split the text into "fields" and then process the fields, and by default, Awk takes a space as a separator for a field, but it's not fixed, you can specify the delimiter, and here's how to do it.
3.awk Command Basic format
awk [-F fs] [-v var=value] [-f prog-file | ‘program text‘] [file...]
Where -F parameters are used to pre-specify the previously mentioned field delimiter (and how other fields are specified), to -v pre-specify the variables for the awk program, -f to specify awk the program files to be executed by the command, or -f to parameters, the program statements are placed directly here, and finally the awk text input that needs to be processed, and multiple text files can be entered at the same time. Now let's go straight to the concrete experience.
4.awk Operation Experience
Create a new text document with Vim first
test
Contains the following content:
I like linuxwww.shiyanlou.com
- Use awk to print text content to a terminal
# "quote>" 不用输入$ awk ‘{> print> }‘ test# 或者写到一行$ awk ‘{print}‘ test
Description: In this operation I was omitted patter , so it awk will default to match the entire contents of the input text, and then execute the action in the "{}" curly braces, that print is, all occurrences are printed, here is the full text content
- Displays each field in the first row of test as a single row
‘{> if(NR==1){> print $1 "\n" $2 "\n" $3> } else {> print}> }‘ test# 或者$ awk ‘{> if(NR==1){> OFS="\n"> print $1, $2, $3> } else {> print}> }‘ test
Note: The first thing you should notice is, here I use the awk language of the branch selection statement if , its use and many high-level languages such as C/C++ language basically consistent, if you have the basis of these languages, here will be well understood. Another you need to note is that NR with OFS , these two are awk built-in variables that represent the number of NR records currently being read, and you can simply understand the number of rows currently being processed, representing the OFS field separators at the time of the output, the default is "" space, as seen, we set the field delimiter to \nnewline characters, so the first line of content separated by a space is output to a separate line. Then there is $N N as the corresponding field number, which is also awk the built-in variable, which means referencing the corresponding field, because we have only three fields in the first row here, so only reference to the $3 . In addition to the other, it does not appear here $0 , it represents the entire contents of the current record (the current row).
- Replace the second line of test with a dot-segmented field with a space-delimited
$ awk -F‘.‘ ‘{> if(NR==2){> print $1 "\t" $2 "\t" $3> }}‘ test# 或者$ awk ‘> BEGIN{> FS="."> OFS="\t" # 如果写为一行,两个动作语句之间应该以";"号分开 > }{> if(NR==2){> print $1, $2, $3> }}‘ test
Experiment 12, Linux under the Software Installation experiment Introduction
First, the software installation on Linux
Typically, there are three main ways to install software on Linux:
- Online installation
- Installing the Deb package from disk
- Installing from a binary package
- Compiling the installation from source code
2. Apt Package management tool Introduction
APT is the abbreviation for the Advance Packaging tool (advanced packaging tools) and is the package manager for Debian and its derived distributions, and apt can automatically download, configure, install binary or source-code packages, thus simplifying the process of managing software on UNIX systems. Apt was originally designed as the front end of the dpkg to handle packages in the Deb format. Now that the APT-RPM organization has been modified, APT can be installed in the RPM-enabled system management RPM package. This Package Manager contains apt- several tools to start with, such as, for apt-get apt-cache apt-cdrom use in the release of the Debian series.
Experiment one Linux preliminary understanding