preface

Only a bald head can be strong

This semester, I opened a Linux course, and the teacher is also a responsible one. In general, it is also a systematic study of Linux ~~~

This article is to summarize the basic operation of Linux and some simple concepts ~ if not familiar with the students can next Linux to play (or to buy a server to play [student version is not very expensive]), for developers, can use Linux to do some basic operations is necessary!

So here we go, of course, my Linux is just the beginning level, please bear with me if there are any mistakes, and don’t hesitate to point them out in the comments section

Why should we learn Linux

I believe that most people’s PC end is using Windows system, then why do we learn Linux this operating system?? Windows does such a good graphical interface that it costs almost nothing to learn to use it on a daily basis.

Linux, on the other hand, may be new to Linux people will think: Linux is so troublesome oh, not fun, all character interface. Not intuitive, this broken system is used for what ~~

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

Then some people may ask: Windows is also an operating system, why put under Linux, but not under Windows? I believe Windows can also run the program we wrote.

I summarized several advantages of Linux:

  1. free
  2. A lot of software is native to run under Linux, huge community support, good ecological environment.
  3. Open source, customizable, open, multi-user web operating system.
  4. Relatively safe and stable

References:

  • www.zhihu.com/question/19…

So the developers chose Linux to run the programs we wrote ourselves.

Second, Linux basics

Components of a Linux system:

  1. Linux kernel (managed by Linus team)
  2. Shell: Interface between user and kernel
  3. File systems: ext3, ext4, etc. Windows has FAT32, NTFS
  4. Third-party applications

2.1 Basic Shell knowledge

Except Shell, the other should be quite easy to understand, so what is Shell??

The Shell is the user interface of the system and provides an interface (command interpreter) for the user to interact with the kernel.

The Shell can execute:

  • Internal command
  • The application
  • A shell script

Using the type command, you can distinguish internal commands from external commands

As a result, we can use the Shell to do the following:

  • Command line interpretation (this is the most used!)
  • Multiple execution sequences of commands
  • Wild-card characters
  • Command completion, alias mechanism, command history
  • I/O redirection (Input/ Output redirection)
  • Pipes
  • Command replacement (Or $())
  • Shell Programming Language (Shell Script)

The major versions of the Shell are as follows:

  • The one we use (by default) is bash(Bourne Again Shell)

Use the ps command to observe the shell being executed

2.2Linux Basic Directory Structure

On Windows, there is a basic directory structure:

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

A Linux file system is a directory tree structure. The file system structure starts with a root directory. The root directory can have any number of files and subdirectories, and the subdirectories can have any number of files and subdirectories

  • Bin Stores binary executable files (ls,cat,mkdir, etc.)
  • Boot stores various files used for system boot
  • Dev is used to store device files
  • Etc stores system configuration files
  • Home The root directory where all user files are stored
  • Lib holds shared libraries and kernel modules needed to run programs on the file system
  • MNT Installation point for system administrator to install temporary file systems
  • The location where optional application packages for opt additional installation are placed
  • Proc Virtual file system that holds the current memory mapping
  • Root Superuser directory
  • Sbin stores binary executable files that can be accessed only by root
  • TMP is used to store various temporary files
  • Usr stores system applications. Important directory /usr/local Indicates the installation directory of the local administrator software
  • Var is used to store files that need to change data at run time

2.3 Basic Command formats

CMD [options] [arguments], options, arguments

Options and arguments are input to Shell commands, separated by Spaces.

  • Linux is case sensitive

In general, the following option is preceded by a minus sign – if the single-character option is used. Use two minus signs — before words

  • This is the general case, and some commands do not belong to this rule (relatively few)
  • Example:ls -aandls -all.aUse one for each character-, a wordallThe use of two--

In Linux, executable files are also classified:

  • Built-in commands: Interpreters of some commonly used commands are constructed inside the Shell for efficiency.
  • External commands: commands stored in the /bin or /sbin directories
  • Utilities: Utilities stored in /usr/bin, /usr/sbin, /usr/share, and /usr/local/bin
  • User programs: User programs can be run as Shell commands after being compiled to generate executable files
  • Shell scripts: batch files written in the Shell language that can be run as Shell commands

2.4 the wildcard

Wildcards are familiar to those of you who have studied regular expressions or have a little background in them. They are also available on Linux (useful for searching).

  • * : Matches any character and any number of characters
  • ? : Matches a single number of any characters
  • [] : Matches any character within []
  • [!] : matches except for [!] Any character other than! It means not

2.5 File Types

There are many types of files under Linux:

  • Common file-
  • directoryd
  • A symbolic link l
    • Hard links: No different from regular files, inodes all point to the same file block on the hard disk
    • Soft link: An absolute path that holds the file it represents, which is another file that has a separate block on the hard disk and replaces its own path when accessed (simply interpreted as a common Shortcut in Windows).
  • Character device filec
  • Block device fileb
  • The sockets
  • A named pipep

Common files, directories, and symbolic links. Other understanding can ~

Symbolic link Resources:

  • www.jianshu.com/p/dde6a01c4…

2.5.1 User home Directory

May be on the net when looking up data can appear to use a household main directory so a noun, what is that he??

As mentioned, our Linux is a multi-user network system! So, we can create multiple users under Linux, and each user will have their own dedicated space.

  • So, when creating a user, the system administratorA home directory is created for each user, often in a/home/directory
  • For example, user Osmond’s home directory is:/home/osmond

Users have the ownership of files in their home directories and can perform related operations in their home directories.

Common commands

Above said a bunch of basic concepts, this is to give us a little foundation before typing the command, while typing the command will also encounter some important knowledge points. Well, we’ll talk about it then

3.1 Common File and Directory Operation Commands

This is the command we use the most, the most basic Linux command!

  • availablepwdCommand to view the current directory of the user
  • availablecdCommand to switch directories
  • .Represents the current directory
  • .Represents the level above the current directory (parent directory)
  • -The CD command is used to switch the directorybeforeDirectory where
  • ~saidUser home directoryThe absolute pathname of

Absolute path:

  • Start with a slash (/) to give a full description of the file location, whenever you want to specify a file name

Relative path:

  • Instead of starting with a slash (/), specifying the location relative to your current working directory can be used as a neat way to specify a file name

Tips: Use the TAB key to complete commands

  • ls: Displays file or directory information
  • mkdir: Creates an empty directory under the current directory
  • rmdir: The directory must be empty
  • touch: Generates an empty file or changes the time of the file
  • cp: Copies files or directories
  • mv: Move files or directories, rename files or directories
  • rm: Deletes a file or directory
  • ln: Create a link file
  • find: Find a file
  • file/stat: Displays the file type or file attribute information
  • Cat:View text file contents
  • More:You can page it
  • Less:Not only can be paginated, but also easy to search, flip back and other operations
  • tail -10: Look at the last 10 lines of the file
  • head -20: Look at the first 20 lines of the file
  • echo: Redirects the content to the specified file. If yes, open it. If no, create it
  • Piping |: passes the previous result to the following command, for example:ls -la | wc, add the RESULT of LS to the WC command to count the words
  • Redirection > is overwrite mode, >> is append mode, such as:echo "Java3y,zhen de hen xihuan ni" > qingshu.txtPut the output on the left into the file on the right

What can we do with these commands? In fact, in Windows copy files, paste files, create files, view files these kinds ~~~

3.1.1 Common File and Directory Operation Exercises

To strengthen the foundation, let’s do the problem:

  • (1) The Linux shell program default isBash program;
  • (2) The Linux command format contains three parts, which are:The commandoptionsparameter
  • (3) Linux command options are preceded by a single minus sign (-) and generally followed byA single character, is preceded by a double minus sign (–)The word
  • (4) The wildcard characters used in Linux commands are? * []
  • Ls /usr/bin/w*List all files or directories under the specified directory starting with w
  • (6) Run ls /usr/bin/w?? The effect isList all files or directories in the specified directory whose names start with w and contain 3 characters
  • Ls /usr/bin/[xyz]*Lists all files or directories under the specified directory whose file names start with x, y, or z
  • Ls /usr/bin/[! A-h]*List all files or directories in the specified directory whose file names do not start with a letter from A to H
  • (9) Directory operation, “. saidIn the current directory
  • (10) Directory operation, “..” saidUpper level directory
  • (11) For directory operations, hyphens (-) indicatesLast Working directory
  • (12) In directory operations, ~ indicates thatUser home directory
  • The ln command can be used to establish a file link, which can be divided into:Hard linksSoft links
  • (14) Command touch can change three kinds of file time, respectively:access timemodify timechange time

Go to the/TMP directory, create a file, goldXX (XX is the last two digits of the student id), check the time of the file

Create a dog directory in the home directory of the account. After entering the dog directory, create a catXX directory (XX is the last two digits of the student id). Enter the catXX directory and display the current directory. Then go back up to the directory and delete the catXX directory.

Copy the /etc/passwd file to the home directory of the account and change passwd to passwdXX (XX is the last two digits of the student ID).

Find the file named zcat; Copy the file to the/TMP directory. List the file in a long format; Then delete all files in the/TMP directory and check whether the files have been deleted

Copy the /etc/hosts file to the account home directory. Create a hard link file (hostsYYY (YYY is the abbreviation of student name)) in the home directory of your account and link it to the hosts file in the home directory. View the inode information of the hosts and hostsYYY files respectively

Copy the /usr/bin/vdir file to the account home directory. Create a soft link file (newdir) in the account home directory to link to the vdir file in the home directory. View the inode information of files in vdir and newdir respectively

List all files in the /usr/bin directory in a long format, redirect the output to the file outXX (XX is the last two digits of the student id), and check the result. All files in the /etc directory are listed in long format, with the output appended to the end of the file outXX

All files in the /usr/bin directory are listed in a long format and connected to the more command through a pipe to browse the file list

What’s the difference between > and >>?

A: > is overwrite redirection to a new file; >> is added to the end of the original file as a supplement.

Where is it convenient to browse the list of files through pipes and more commands?

A: For the case that the file list is displayed on more than one screen, this method can be split screen browsing, which is convenient.

3.2 File Packaging and Compression Commands

Under The Windows operating system, we will use WinRAR or quick compression software to compress or decompress.

In Linux, of course, there are also compression or decompression operations, let’s learn how to compress and decompress in Linux!

There are several ways of compression, and we commonly use the following three:

  • gzip
  • bzip2
  • tar

Common compression commands are:

  • gzip filename
  • bzip2 filename
  • tar -czvf filename

Common decompression commands are as follows:

  • gzip -d filename.gz
  • bzip2 -d filename.bz2
  • tar -xzvf filename.tar.gz

3.3 Regular Expression +grep

We have already learned the cat, more, less, tail and other commands to view the text file, but I want to quickly check the text file to see if some keywords exist, what should I do?

Under Windows is relatively simple, almost all text editors (Notepad) support CTRL+F, enter the key words to find out:

On Linux, without a graphical interface and CTRL+F, it’s really hard to find the corresponding characters if you don’t understand some commands. Here’s how to quickly find some characters in a text file.

3.3.1 Regular expressions

Let’s start with regular expressions, which, if you’ve been around them, are not easy to remember. It’s easy to forget once you don’t use it, so you can only look it up when you use it.

3.3.2 rainfall distribution on 10-12 grep command

Grep (Global Search Regular Expression) is a powerful text search tool. Grep uses regular expressions to search for text and print out matching lines.

Format: grep [options] PATTERN [FILE…]

  • PATTERN is a lookup condition: it can be a regular string or a regular expression, and the RE is usually enclosed in single quotes.
  • FILE is the FILE to look for. It can be multiple files separated by Spaces, or the Shell’s wildcard character can be used to look for PATTERN in multiple files. When omitted, the PATTERN is looked for in standard input.
  • The grep command does nothing to modify or affect the input file, and you can use output redirection to save the result as a file

Example:

  • Look for the line containing the string mystr in the file myfile
    • grep -n mystr myfile
  • Displays all lines in myFile whose first character is a letter
    • grep '^[a-zA-Z]' myfile
  • Find lines in myfile that do not begin with # (That is, comment lines are filtered out)
    • grep -v '^#' myfile
  • List all file names containing the string root in the contents of files in the /etc directory (including subdirectories)
    • grep -lr root /etc/*

Run grep to find the line starting with a in the /etc/passwd file and display the line number. Find the line ending with login in the /etc/passwd file;

3.4 Several ways to improve work efficiency

  • Automatic completion command
    • The TAB key
  • The history command
    • Up and down arrow keys
  • The alias alias

Alias alias alias alias alias alias alias alias

Displays the aliases currently defined by the shell. Execute two of the commands that define aliases; Define an alias grep to display the results in color

3.5Shell Variables and Shell environment

Under Windows there are environment variables for the user, environment variables for the system. The same is true for Linux.

Shell variables can be roughly divided into three categories:

  • Internal variables: Provided by the system. Users can only use it but cannot modify it.
    • ?
    • GROUPS
  • Environment variables: These variables determine the environment in which the user works. They do not need to be defined by the user and can be used directly in the shell. Some of these variables can be modified by the user.
  • The user variables: created and modified by the user. It is often used in shell scripting.
    • Variable assignment (defining variables)
      • varName=Value
      • export varName=Value
    • Reference variables$varName

Shell variable scope:

  • The scope of a local variable is limited only to the Shell or Shell script file in which the command line resides;
  • The scope of a global variable includes the Shell process and all its children.
  • Local and global variablesswap: AvailableexportThe built-in commands set local variables to global variables. You can useexportBuilt-in commands set global variables to local variables.

The export command:

  • According toGlobal variables visible to the current Shell
    • export [-p]
  • Define the value of a variable and declare it as a global variable.
    • Export < variable name 1= value 1> [< variable name 2= value 2>...
  • Declare something (something) that has been assignedLocal variables are global variables.
    • Export < variable name 1> [< variable name 2>...
  • Declare something (something) that has been assignedGlobal variables are local variables.
    • Export -n < variable name 1> [< variable name 2>...

Shell environment variables:

  • Environment variables define the environment of the Shell and ensure the correct execution of Shell commands.
  • Shell uses environment variables to determine the search path, registered directory, terminal type, terminal name, user name, and so on.
  • All environment variables are global variables (that is, children that can be passed to the Shell) and can be reset by the user.

Shell variables: Query, show, and cancel:

  • Displays all variables that are currently defined
    • allThe environment variable:env
    • allVariables and functions(including environment variables) :set
  • Displays the value of a variable
    • Echo $NAME1 [$] NAME2...
  • Undeclare or assign a variable
    • unset <NAME>

3.5.1Shell Variables and Shell Environment exercises

Define Shell variable stuXX (XX is the last two digits of the student number), the initial value is the full name of the student, with echo command display stuXX variable value; Unset the stuXX variable and check the result. Use the env command to see what shell environment variables are currently defined

Can the values of Shell environment variables be changed? Why is that?

A: The value of an environment variable can be changed under normal circumstances. However, you must be careful to modify, because once the modification is wrong, the normal operation of the shell will be seriously affected, or even the shell cannot run.

VI editor

For those of you who have never used Linux, you may have seen two editors while watching some of the jokes:

  • vim
  • emacs

Let’s learn how to use VI simply. Vi, short for “Visual Interface,” can perform output, delete, find, replace, block and many other text operations, and users can customize it according to their needs, which is not available in other editing programs.

  • Vi can be seen as our Notepad under Windows
  • Vim is Vi IMproved, one of the Vi clones

Using Vi to edit files:

Vi has three modes:

4.1 Common Mode

  • GUsed to jump directly to the end of a file
  • ZZUsed to save the disk and exit the Vi
  • ZQExit Vi without saving the disk
  • / and?Used to find strings
  • nMove on to the next one
  • yyCopy one line
  • pPaste on the next line, P on the previous line
  • ddDelete a line of text
  • xDeletes the character where the cursor is located
  • uCancel last edit (undo)

4.2 Insertion Mode

Enter insert commands I, a, and o in Normal mode to enter insert mode. Any characters the user enters are saved as file contents by Vim and displayed on the screen.

  • To return to Normal mode during text input, press Esc.

4.3 Cli Mode

In Normal mode, you can press colon: to enter the Command mode. In this case, VIM displays a “:” on the last line of the display window (the last line of the screen) as the prompt of the Command mode, waiting for commands to be entered.

  • :wSave the current edited file, but do not exit
  • :wNewfile is saved as another file named “newfile”
  • :wqUsed to save the disk and exit the Vi
  • :q!Exit Vi without saving the disk
  • :qUsed to exit Vi directly (not modified)

Setting the Vi environment:

  • :set autoindent, often used in program writing
  • :set noAutoindent Unindent
  • :set number Displays the line number when editing the file
  • :set nonumber Does not display line numbers
  • :set tabstop=value Sets the number of Spaces to display tabs
  • :set Displays all options for setting
  • :set all Displays all options that can be set

4.4 Vi exercises

In the user home directory, run the vi program to edit the install.log file. Move the cursor to line 10, character 5; Press G to reach the end of the file. Exit without saving;

In the user home directory, run the vi program to edit the install.log file. Find the string sudo with the/command and copy the line containing the string sudo

In the user home directory, run the vi program to edit the install.log file. Enter the command mode and set the line number to be displayed. To use? Command to find the string openssh, use the command n to find the next

Four,

This article mainly explains the most basic knowledge of Linux, you can simply operate Linux. Command can be said to be learning, can only be remembered Linux is this function, when the time comes to check online is also very convenient.

Mainly practice, play can remember some common Linux commands ~~~~

I’m going to take a moment to draw a brain map of a Linux command (of course, the command is only for the knowledge of this article, and I’ll add it when I get to user, network, etc.).

If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y

Article table of Contents navigation:

  • Zhongfucheng.bitcron.com/post/shou-j…