After reading this basic Linux operation, if you don’t know how to do it, then no one can save you

foreword

Only the bald head can become stronger 

This article mainly summarizes the basic operation of Linux and some simple concepts~ If you are not familiar with students, you can download Linux to play (or buy a server to play [student version is not very expensive]), for developers , It is necessary to be able to use Linux to do some basic operations!

Then let’s start. Of course, my Linux is only at the entry level. If there are mistakes, please forgive me and point out the mistakes in the comment area~

1. Why do we need to learn Linux
? I believe that most people’s PCs use the Windows system, so why should we learn the Linux operating system? ? ? The Windows graphical interface is so good that the learning cost is almost zero for basic daily use.

But Linux is different. People who are new to Linux may think: Linux is so troublesome, not fun, and it is all character interfaces. Not intuitive, what is this broken system for~~

Windows is more convenient for daily use, but what we need to know is: the programs we develop are generally run under Linux.

Then some people may ask questions: Windows is also an operating system, why should it be placed under Linux instead of Windows? ? I believe that Windows can also run the programs we write.

I have summarized several advantages of Linux:

Free
Many software natively run under Linux, with huge community support and a good ecological environment.
Open source, customizable, open, multi-user network operating system.
Relatively safe and stable
, so the developers chose Linux to run the programs we wrote ourselves.

Second, the basics of Linux
The composition of the Linux system:

linux kernel (linus team management)
shell: interface for users to interact with the kernel
file system: ext3, ext4, etc. Windows has fat32, ntfs
third-party application software
2.1 Basic knowledge of
Shell Except Shell, others should be easy to understand, so what is Shell? ? ?

Shell is the user interface of the system, providing  an interface for users to interact with the kernel (command interpreter)

Shell can execute:

  • internal command
  • application
  • shell script

Use the type command to distinguish between internal and external commands

Ever since, we can use Shell to do the following things:

Command line interpretation (this is the most used!)
Various execution orders of commands
Wild-card characters (wild-card characters)
command completion, alias mechanism, command history
I/O redirection (Input/output redirection)
pipeline (pipes)
command Replacement (or $( ))
Shell programming language (Shell Script)
The major versions of Shell are as follows:

Our commonly used (default) is bash (bourne again shell)

 Use psthe command to observe the shell being executed

2.2Linux basic directory structure

Under Windows, there will be a basic directory structure :

 

Linux is no exception, there is also a basic directory structure:  

The Linux file system is a directory tree structure. The file system structure starts from a root directory. There can be any number of files and subdirectories under the root directory, and there can be any number of files and subdirectories in the subdirectories.

bin stores binary executable files (ls, cat, mkdir, etc.)
boot stores various files used for system
booting dev stores device files
etc stores system configuration files
home stores root directory
lib of all user files stores and file system The shared libraries and kernel modules required for the running of the program in
mnt The installation point where the system administrator installs the temporary file system
opt The location where the optional application package for additional installation is placed
proc The virtual file system stores the mapping of the current memory
root Superuser directory
sbin stores binary executable files, only root can access
tmp is used to store various temporary files
usr is used to store system applications, the more important directory /usr/local local administrator software installation directory
var is used to store data that needs to be changed during operation The file
2.3 command basic format
cmd [options] [arguments], options are called options, and arguments are called parameters

Both options and parameters are used as input when the Shell command is executed, and they are separated by spaces.

Linux is case-sensitive.
In general, if the option followed by a single-character option is preceded by a minus sign -. Use two minus signs before word options --

This is the general situation, some commands still do not belong to this rule (relatively few)~~~
Example: ls -a and ls -all, a single character uses one -, a word all uses two --
in Linux , executable files are also categorized:

Built-in commands: For the sake of efficiency, the interpreter of some commonly used commands is constructed inside the shell.
External commands: commands stored in /bin and /sbin directories
Utilities: utility programs stored in /usr/bin, /usr/sbin, /usr/share, /usr/local/bin and other directories User programs
: After the user program is compiled to generate an executable file, it can be used as a shell command to run a shell script: a batch
file written in the shell language can be used as a shell command to run Not unfamiliar, there are wildcards in Linux (useful when searching)

*: Match any character and any number of characters
?: Match any character with a single number
[ ]: Match any character within [ ]
[! ]: Match any character except [! ], ! means not Meaning
2.5 Types of files
There are so many types of files under Linux:

Ordinary file-
directory d
symbolic link l
hard link: no different from ordinary files, inodes all point to the block of the same file in the hard disk
soft link: save the absolute path of the file it represents, which is another kind of file, on the hard disk There is an independent block on it, which replaces its own path when accessing (simply understood as a common shortcut in Windows).
Character device file c
block device file b
socket s
named pipe p
Our common ones are ordinary files, directories and symbolic links. Find out more about it~

2.5.1 User home directory
There may be a noun such as user home directory when looking up information on the Internet, so what is it? ? ?

As mentioned earlier, our Linux is a multi-user network system! Therefore, we can create multiple users under Linux, and each user will have his own exclusive space.

Therefore, when creating a user, the system administrator will create a home directory for each user, usually under the /home/ directory.
For example: the home directory of the user osmond is: /home/osmond
The user owns the files in his home directory, You can perform related operations in your own home directory.


To be honest, it is more convenient to learn Linux commands and buy a server directly, which will save you a lot of time, and now a server only costs 89 yuan a year. If you don't have special needs, don't spend your time on a virtual machine, it's not worth it

3. Commonly used commands
A bunch of basic concepts have been mentioned above. This is to lay a little foundation for us before typing commands. When typing commands, we will also encounter some more important knowledge points. Then let's talk about it when the time comes~~~

3.1 Commonly used file and directory operation commands
This is the command we use the most, the most basic command of Linux!

The pwd command can be used to view the user's current directory.
The cd command can be used to switch directories
. Indicates the current directory
.. Indicates the upper level directory (parent directory) of the current directory
- indicates the directory where the user's home directory was before using the cd command
~ Indicates the absolute value of the user's home directory pathname
absolute path:

Beginning with a slash (/), the description goes to the full description of the file location, anytime you want to specify a filename you can use a
relative path:

Do not start with a slash (/), specify the location relative to your current working directory, and can be used as a shortcut to specify the file name
tips: When entering commands, use the tab key to complete

ls: display file or directory information
mkdir: create an empty directory under the current directory
rmdir: require the directory to be empty
touch: generate an empty file or change the time of the file
cp: copy the file or directory
mv: move the file or directory, rename the file or directory
rm: Delete a file or directory
ln: Create a link file
find: Find a file
file/stat: View the file type or file attribute information
cat: View the content of a text file
more: You can view it
by page Turn over and wait for operations
tail -10: View the 10 lines at the end of the file
head -20: View the 20 lines at the head of the file
echo: Redirect the content to the specified file, open it if there is one, and create a pipeline if there is none
Command | : Change the front Give the result of the ls to the following command, for example: ls -la | wc, use the wc command to count the word count
redirection > is the overwrite mode, >> is the append mode, for example: echo "Java3y,zhen de hen xihuan ni" > qingshu.txt Put the output on the left into the file on the right.
What can we do after learning these commands? In fact, it is to copy files, paste files, create files, and view files under Windows~~~

3.1.1 Commonly used file and directory operation exercises
Consolidate the foundation and do the questions:

(1) The shell program of Linux is the bash program by default;
(2) The Linux command format consists of three parts, namely: command, option, parameter
; Single character, before the option is a double minus sign (--), followed by a word;
(4) What are the wildcards used in Linux commands? * [];
(5) The effect of the command ls /usr/bin/w* is to list all files or directories beginning with w in the specified directory; (6) The effect of the command ls /usr/bin/w??
is List all files or directories under the specified directory whose names start with w and have a length of 3;
(7) The effect of the command ls /usr/bin/[xyz]* is to list the files under the specified directory whose names start with x or y or z All files or directories beginning with ;
(8) The effect of the command ls /usr/bin/[!ah]* is to list all files or directories whose file names in the specified directory do not start with letters from a to h; (9
) During directory operation, "." indicates the current directory;
(10) During directory operation, ".." indicates the upper level directory;
(11) During directory operation, "-" indicates the last working directory;
(12) During directory operation , "~" indicates the user's home directory;
(13) The command ln can establish a file link, which is divided into: hard link and soft link;
(14) The command touch can change the three times of the file, namely: access time, modify time, change time;

Enter the /tmp directory, create a file, goldXX (XX is the last two digits of the student number), check the time of the file

 In the main directory of the account, create a dog directory. After entering the dog directory, create a catXX (XX is the last two digits of the student number) directory, enter the catXX directory, and display the current directory; then return to the previous directory and delete the catXX directory ;

 Copy the /etc/passwd file to the main directory of the account, and modify the passwd in the main directory of the account to passwdXX (XX is the last two digits of the student number)

 Find a file named zcat; copy the file to the /tmp directory; list the file in long format; then delete all files in the /tmp directory and check whether the file has been deleted

 Copy the /etc/hosts file to the main directory of the account; create a hard link file in the main directory of the account (the file name is hostsYYY (YYY is the pinyin abbreviation of the student’s name)), and link to the hosts file in the main directory; check the hosts and The inode information of the hostsYYY file 

  Copy the /usr/bin/vdir file to the main directory of the account; create a soft link file (named newdir) in the main directory of the account, link to the vdir file in the main directory; check the inode information of the vdir and newdir files respectively

 List all files in the directory /usr/bin in the long format, redirect the output to the file outXX (XX is the last two digits of the student’s student number), and check the results; list all the files in the directory /etc in the long format, The output is appended to the end of the file outXX

 List all files in the /usr/bin directory in long format, and connect with the more command through pipelines to browse the file list

 

>和>>What's the difference? 

Answer: >It is redirected to a new file by overwriting>> ; it is added to the end of the original file in a supplementary way

Where is the convenience of browsing the list of files through pipes and the more command? 

Answer: For the case where the file list exceeds one screen, this method can be browsed in split screens, which is more convenient.

3.2 File packaging and compression commands
Under the Windows operating system, we will use compression software such as WinRAR or Kuaizi to compress or decompress.

Of course, there are compression or decompression operations under Linux. Let's learn how to compress and decompress under Linux!

There are also several compression methods, the following three are commonly used:

The commonly used compression commands of gzip
bzip2
tar are:

gzip filename
bzip2 filename
tar -czvf filename
Commonly used decompression commands are:

gzip -d filename.gz
bzip2 -d filename.bz2
tar -xzvf filename.tar.gz
3.3 Regular expressions + grep
We have already learned the commands of cat, more, less, and tail to view text files, but I think Quickly check whether some keywords under this text file exist, so what should I do? ? ?

It is relatively simple under Windows. Almost all text editors (Notepad) support CTRL+F, and you can find it by entering keywords:

Then under Linux, without a graphical interface, without CTRL+F, if you don't understand some commands, it is really difficult to find the corresponding characters. Let me talk about how to quickly find certain characters in a text file.

3.3.1 Regular expressions
First of all, let's talk about regular expressions. If you have been in contact with students, you will know: this thing is not easy to remember. Once you don't use it, it's easy to forget, so you can only look it up when you use it~~~ So I will give some rules directly below, so I won't say much.

3.3.2 grep command
grep (global search regular expression) is a powerful text search tool. grep searches text using regular expressions and prints the matching lines.

Format: grep [options] PATTERN [FILE...]

PATTERN is the search condition: it can be an ordinary string or a regular expression, and RE is usually enclosed in single quotes.
FILE is the file to be searched, which can be multiple files separated by spaces, or use shell wildcards to search for PATTERN in multiple files. When omitted, it means to search in the standard input.
The grep command will not modify or affect the input file in any way, you can use output redirection to save the result as a file
Example:

Find lines in the file myfile that contain the string mystr grep
-n mystr myfile
Display all lines in myfile whose first character is a letter
grep '^[a-zA-Z]' myfile
Find lines in the file myfile whose first character is not # line (that is, filter out the comment line)
grep -v '^#' myfile
lists the file names of all files in the /etc directory (including subdirectories) that contain the string "root"
grep -lr root /etc/*

Use grep to find the line starting with a in the /etc/passwd file, and ask to display the line number; find the line ending with login in the /etc/passwd file;

 

3.4 Several ways to improve work efficiency

  • Autocomplete commands
    • TAB key
  • historical order
    • up and down arrow keys
  • alias

Here I feel that the only thing to talk about is the alias alias, let's take a look at the example below to understand!

Display the aliases currently defined by the shell; execute two of the commands that define the aliases; define an alias grep and require it to display the results in color

3.5 Shell variables and Shell environment

Under Windows, there are user environment variables and system environment variables. There are also some in Linux.

Shell variables can be roughly divided into three categories:

Internal variable: provided by the system, the user can only use it and cannot modify it.
?
GROUPS
environment variables: These variables determine the user's working environment. They do not need to be defined by the user, and can be used directly in the shell. Some of the variables can be modified by the user.
User variables: Created and modified by users, often used in shell scripting.
Variable assignment (defining variables)
varName=Value
export varName=Value
refers to the variable $varName
The scope of shell variables:

The scope of local variables is only limited to the shell or shell script file where the command line is located; the
scope of global variables includes the shell process and all its subprocesses.
Local variables are interchangeable with global variables: You can use the export built-in command to set a local variable as a global variable. Global variables can be made local using the export built-in command.
export command:

Display the global variables visible to the current shell.
export [-p]
defines the variable value and declares it as a global variable.
export <variable name 1=value 1> [<variable name 2=value 2> ...]
Declare some (some) local variables that have been assigned as global variables.
export <variable name 1> [<variable name 2> ...]
declares some (some) global variables that have been assigned as local variables.
export -n <variable name 1> [<variable name 2> ...]
Shell environment variable:

Environment variables define the operating environment of the Shell to ensure the correct execution of Shell commands.
Shell uses environment variables to determine the search path, registration directory, terminal type, terminal name, user name, etc.
All environment variables are global variables (that is, can be passed to subprocesses of the shell), and can be reset by the user.

Shell variables: query, display and cancel:

Display all variables currently defined All
environment variables: env
All variables and functions (including environment variables): set
Display the value of some (some) variables
echo $NAME1 [$NAME2 ...]
cancel variable declaration or assignment
unset <NAME >
3.5.1 Shell Variables and Shell Environment Exercises

Define the shell variable stuXX (XX is the last two digits of the student’s student number), the initial value is the full spelling of the student’s name, use the echo command to display the value of the stuXX variable; use the unset command to cancel the stuXX variable, and check the results; use the env command to observe what is currently Defined shell environment variables

 

 Can the value of Shell environment variable be modified? Why?

Answer: Generally, the value of the environment variable can be modified. But you must modify it carefully, because once you modify the error, it will have a serious impact on the normal operation of the shell, and even cause the shell to fail to run.

4. VI editor
I believe that students who have never used Linux will see two editors when reading some jokes:

vim
emacs
Let's learn how to use vi simply. vi is the abbreviation of "Visual interface". It can perform many text operations such as output, delete, search, replace, block operation, etc., and users can customize it according to their own needs, which is not available in other editing programs.


Vi can be regarded as vim, the notepad under Windows, namely Vi IMproved, one of the cloned versions of vi
. Use Vi to edit files:

 Vi has three modes:

4.1 Normal mode
G is used to jump directly to the end of the file
ZZ is used to save and exit Vi
ZQ is used to exit Vi without saving
/ and ? It is used to find the string
n continue to find the next
yy copy a line
p paste in the next line, P paste in the previous line
dd delete a line of text
x delete the character where the cursor is located
u cancel the last editing operation (undo)
4.2 insert mode
in Normal mode Enter the insert command i, a, o to enter the insert mode. Any characters entered by the user are saved by vim as file content and displayed on the screen.

During the text input process, if you want to return to the Normal mode, press the Esc key.
4.3 Command line mode
In Normal mode, the user presses the colon : to enter the Command mode. At this time, vim will display a ":" on the last line of the display window (the last line of the screen) as the prompt of the Command mode, waiting for the input command.

:w saves the current edited file, but does not exit
:w newfile saves it as another file named "newfile"
:wq is used to save and exit Vi
:q! is used to exit Vi without saving
:q is used to exit Vi directly (not Make changes)
Set up the Vi environment:

:set autoindent indentation, often used in program writing
: set noautoindent cancel indentation
: set number display line number when editing files
: set nonumber do not display line number
: set tabstop=value set the number of space characters to display tabs
: set displays all the options set
: set all displays all the options that can be set
4.4Vi exercises

In the user's home directory, execute the vi program and edit the file install.log; move the cursor to the 10th line, the fifth character; press the capital letter G to reach the end of the file; exit without saving;

 

In the user's home directory, execute the vi program, edit the file install.log; use the / command to find the string sudo, and copy the line containing the string sudo

 In the user's home directory, execute the vi program, edit the file install.log; enter the command mode, set the display line number; use ? command to find the string openssh, use the command n to find the next

 

Four. Summary
This article mainly explains the most basic knowledge of Linux, and you can easily operate Linux. Commands can be said to be endless, you can only remember that Linux has this function, and it will be very convenient to check it online at that time.

The main thing is to practice more and play more to remember some commonly used Linux commands~~~~

If there is something wrong in the article, please correct me and communicate with each other. Students who are used to reading technical articles on WeChat and want to get more software testing resources can follow me

 

 

Guess you like

Origin blog.csdn.net/2301_76643199/article/details/132249082