Essential Linux basics for front-end & back-end programmers

One starts with understanding the operating system

1.1 Introduction to Operating System

I introduce what operating system through the following four points:

  • The operating system (Operation System, referred to as OS) is a program that manages computer hardware and software resources, and is the core and cornerstone of a computer system;
  • An operating system is essentially a software program that runs on a computer;
  • Provide users with an operation interface to interact with the system;
  • The operating system is divided into a kernel and a shell (we can understand the shell as an application program surrounding the kernel, and the kernel is a program that can operate the hardware).

1.2 Simple Classification of Operating Systems

  1. Windows:  Currently the most popular personal desktop operating system, everyone knows it without much introduction.
  2. Unix:  The earliest multi-user, multi-tasking operating system. According to the classification of the operating system, it belongs to the time-sharing operating system. Unix is ​​mostly used on servers, workstations, and now also on personal computers. It plays a very important role in creating the internet, computer network or client/server model.
  3. Linux:  Linux is a set of Unix-like operating systems that are free to use and spread freely. There are many different Linux versions of Linux, but they all use the  Linux kernel  . Linux can be installed on a variety of computer hardware devices such as cell phones, tablets, routers, video game consoles, desktop computers, mainframes and supercomputers. Strictly speaking, the word Linux itself only refers to the Linux kernel, but in fact people have become accustomed to using Linux to describe the entire operating system based on the Linux kernel and using various tools and databases of the GNU project.

Two preliminary exploration of Linux

2.1 Introduction to Linux

We have already introduced Linux above, and we only emphasize three points here.

  • Unix-like system:  Linux is a free, open-source Unix-like operating system
  • Linux Kernel:  Strictly speaking, the word Linux itself only means the Linux Kernel
  • The father of Linux:  a legendary figure in the field of programming. He is the earliest author of the Linux kernel, then initiated this open source project, served as the chief architect and project coordinator of the Linux kernel, and is one of the most famous computer programmers and hackers in the world today. He also initiated the Git open source project and is the main developer.

2.2 Introduction to the Birth of Linux

  • In 1991, Linus Torvalds, a Finnish amateur computer enthusiast, wrote a Minix-like system (Unix-like operating system based on microkernel architecture) named Linux by the ftp administrator and joined the GNU project of the Free Software Foundation;
  • Linux uses a cute penguin as a symbol, which symbolizes daring and loving life.

2.3 Classification of Linux

According to the degree of nativeness, Linux is divided into two types:

  1. Kernel version:  Linux is not an operating system. Strictly speaking, Linux is just the kernel in an operating system. What is the kernel? The kernel establishes a communication platform between computer software and hardware, and the kernel provides system services, such as file management, virtual memory, device I/O, etc.;
  2. Release version:  A version reissued by some organizations or companies for secondary development on the basis of the kernel version. There are many kinds of Linux distributions (ubuntu and CentOS are used a lot, beginners are recommended to choose CentOS), as shown in the following figure: 

Three Linux file system overview

3.1 Introduction to Linux file system

In the Linux operating system, all resources managed by the operating system, such as network interface cards, disk drives, printers, input and output devices, ordinary files or directories, are regarded as a file.

That is to say, there is an important concept in the LINUX system: everything is a file . In fact, this is a manifestation of the UNIX philosophy, and Linux is a rewrite of UNIX, so this concept has been passed down. In the UNIX system, all resources are regarded as files, including hardware devices. The UNIX system regards each piece of hardware as a file, usually called a device file, so that users can access the hardware by reading and writing files.

3.2 File Type and Directory Structure

Linux supports 5 file types: 

The directory structure of Linux is as follows:

The structure of the Linux file system is distinct, like an upside-down tree, with the root directory at the top: 

Common directory description:

  • /bin:  store binary executable files (ls, cat, mkdir, etc.), commonly used commands are generally here;
  • /etc:  store system management and configuration files;
  • /home:  the root directory for storing all user files, which is the base point of the user's home directory. For example, the home directory of the user user is /home/user, which can be represented by ~user;
  • /usr:  used to store system applications;
  • /opt:  Where additionally installed optional application packages are placed. Under normal circumstances, we can install tomcat and so on here;
  • /proc:  Virtual file system directory, which is a mapping of system memory. You can directly access this directory to get system information;
  • /root:  The home directory of the super user (system administrator) (privileged class ^o^);
  • /sbin:  store binary executable files, only root can access. Stored here are system-level management commands and programs used by system administrators. Such as ifconfig, etc.;
  • /dev:  used to store device files;
  • /mnt:  The system administrator installs the installation point of the temporary file system. The system provides this directory to allow users to temporarily mount other file systems;
  • /boot:  stores various files used for system booting;
  • /lib :  stores library files related to system operation;
  • /tmp:  used to store various temporary files, and is a public temporary file storage point;
  • /var:  It is used to store files that need to change data during operation, and it is also an overflow area for some large files, such as log files of various services (system startup logs, etc.), etc.;
  • /lost+found:  This directory is usually empty, and the "homeless" file (what is called .chk under Windows) left by the system abnormal shutdown is here.

Four Linux Basic Commands

The following are just some of the more commonly used commands. Recommend a Linux command quick check website, very good, if you forget some commands or don't understand some commands, you can get solutions here.

Linux Command Encyclopedia: man.linuxde.net/

4.1 Directory switching command

  • cd usr:  Switch to the usr directory under this directory
  • cd ..(或cd../):  Switch to the previous directory
  • cd /:  Switch to the system root directory
  • cd ~:  switch to user home directory
  • cd -:  Switch to the previous directory

4.2 Directory operation commands (addition, deletion, modification and query)

  1. mkdir 目录名称:  add directory

  2. ls或者ll(ll is the abbreviation of ls -l, ll command to see the detailed information of all directories and files in the directory): view directory information

  3. find 目录 参数:  Find directory (check)

    Example:

    • List all files and folders in the current directory and subdirectories: find .
    • /homeLook for filenames ending with .txt in the directory :find /home -name "*.txt"
    • Same as above, but ignoring case: find /home -iname "*.txt"
    • Find all files ending with .txt and .pdf in the current directory and subdirectories: find . \( -name "*.txt" -o -name "*.pdf" \)orfind . -name "*.txt" -o -name "*.pdf"
  4. mv 目录名称 新目录名称:  Modify the name of the directory (change)

    Note: The syntax of mv can not only rename directories but also rename various files, compressed packages, etc. The mv command is used to rename files or directories, or to move files from one directory to another. Another usage of the mv command will be introduced later.

  5. mv 目录名称 目录的新位置:  The location of the mobile directory --- cut (change)

    Note: The mv syntax can not only cut directories, but also cut files and compressed packages. In addition, the results of mv and cp are different. Mv seems to have "moved" the files, and the number of files has not increased. While cp copies the files, the number of files increases.

  6. cp -r 目录名称 目录拷贝的目标位置:  Copy directory (change), -r means recursive copy

    Note: The cp command can not only copy directories but also copy files, compressed packages, etc., do not write -r recursively when copying files and compressed packages

  7. rm [-rf] 目录:  delete directory (delete)

    Note: rm can not only delete directories, but also delete other files or compressed packages. In order to enhance everyone’s memory, regardless of deleting any directory or file, use rm -rf directory/file/compressed package directly

4.3 File operation commands (add, delete, modify, check)

  1. touch 文件名称:  File creation (increase)

  2. cat/more/less/tail 文件名称 file viewing (check)

    • cat:  Only the last screen content can be displayed
    • more:  It can display the percentage, press enter to go down one line, space to go down one page, q to exit and view
    • less:  You can use PgUp and PgDn on the keyboard to turn pages up and down, q to end viewing
    • tail-10 :  View the last 10 lines of the file, Ctrl+C to end

    Note: The command tail -f file can dynamically monitor a file, such as the log file of tomcat, as the program runs, the log will change, you can use tail -f catalina-2016-11-11.log to monitor the file Variety

  3. vim 文件:  Modify the content of the file (change)

    The vim editor is a powerful component in Linux. It is an enhanced version of the vi editor. There are many commands and shortcuts of the vim editor, but they are not explained here one by one. You don’t need to study thoroughly. Use vim to edit and modify files The method can basically be used.

    In actual development, the main function of using the vim editor is to modify the configuration file. The following are the general steps:

    vim file ------> enter file -----> command mode ------> press i to enter edit mode -----> edit file --------> press Esc to enter Bottom line mode -----> input: wq/q! (Input wq means write content and exit, that is, save; input q! means force exit without saving.)

  4. rm -rf 文件:  delete file (delete)

    Delete the same directory:  rm -rf just memorize the file

4.4 Operation commands for compressed files

1) Pack and compress the files:

Packaged files in Linux generally end with .tar, and compressed commands generally end with .gz.

In general, packaging and compression are performed together, and the suffix of the packaged and compressed file is generally .tar.gz. command: tar -zcvf 打包压缩后的文件名 要打包压缩的文件 where:

z: Call the gzip compression command for compression

c: package file

v: display the running process

f: specify the file name

For example: there are three files in the test directory: aaa.txt bbb.txt ccc.txt, if we want to pack the test directory and specify the name of the compressed package as test.tar.gz, we can use the command: or tar -zcvf test.tar.gz aaa.txt bbb.txt ccc.txt:tar -zcvf test.tar.gz /test/

2) Unzip the compressed package:

Command: tar [-xvf] Compress files

Where: x: stands for decompression

Example:

1 Unzip the test.tar.gz under /test to the current directory and use the command:tar -xvf test.tar.gz

2 Decompress the test.tar.gz under /test to the root directory /usr: tar -xvf xxx.tar.gz -C /usr(- C represents the specified decompression location)

4.5 Linux permission commands

Each file in the operating system has specific permissions, owned users, and owned groups. Permissions are the mechanism used by the operating system to limit access to resources. In Linux, permissions are generally divided into three groups: readable, writable, and excutable. Corresponding to the owner (owner), group (group) and other users (other) of the file respectively, through this mechanism to restrict which users and which groups can perform certain operations on specific files. Through  ls -l the command we can view the permissions of files or directories under a certain directory

Example: in any directoryls -l

The information of the content of the first column is explained as follows:

The following will explain in detail the types of files, permissions in Linux, and the owner, group, and other groups of the file.

file type:

  • d: represents the directory
  • -: represents a file
  • l: Represents a link (can be considered as a shortcut in the window)

Permissions in Linux are divided into the following categories:

  • r: Represents that the permission is readable, and r can also be represented by the number 4
  • w: Represents that the permission is writable, and w can also be represented by the number 2
  • x: Represents that the permission is executable, and x can also be represented by the number 1

The difference between file and directory permissions:

For files and directories, reading and writing execution means different meanings.

For files:

permission name Actionable
r You can use cat to view the contents of the file
w Can modify the content of the file
x It can be run as a binary

For directories:

permission name Actionable
r You can view the list under the directory
w Can create and delete files under the directory
x You can use cd to enter the directory

Each user in linux must belong to a group and cannot be independent from the group. In Linux, each file has the concept of owner, group, and other groups.

  • owner

    Generally, it is the creator of the file. Whoever created the file will naturally become the owner of the file. You can use the ls ‐ahl command to see the owner of the file, and you can also use the chown username file name to modify the owner of the file.

  • file group

    When a user creates a file, the group of the file is the group of the user. You can use the ls-ahl command to see all the groups of the file. You can also use chgrp group name file name to modify the group of the file.

  • other groups

    Except for the owner of the file and the users of the group, other users of the system are all other groups of the file

Let's take a look at how to modify the permissions of files/directories.

Command to modify the permissions of a file/directory:chmod

Example: Modify the permissions of aaa.txt under /test to the owner with full permissions, the owner’s group with read and write permissions, and other users with only read permissions

chmod u=rwx,g=rw,o=r aaa.txt

The above example can also be represented numerically:

chmod 764 aaa.txt

Add a more commonly used thing:

If we have installed a zookeeper, what should we do if we ask it to start automatically every time we boot?

  1. Create a new script zookeeper
  2. Add executable permission to the newly created script zookeeper, the command is:chmod +x zookeeper
  3. Add the zookeeper script to the startup item, the command is: chkconfig --add zookeeper
  4. If you want to see if the addition is successful, the command is:chkconfig --list

4.6 Linux user management

The Linux system is a time-sharing operating system with multiple users and multiple tasks. Any user who wants to use system resources must first apply for an account from the system administrator, and then enter the system as this account.

On the one hand, user accounts can help system administrators track users who use the system and control their access to system resources; on the other hand, they can also help users organize files and provide users with security protection.

Linux user management related commands:

  • useradd 选项 用户名: Add user account
  • userdel 选项 用户名: delete user account
  • usermod 选项 用户名: modify account
  • passwd 用户名: Change or create a user's password
  • passwd -S 用户名 : Display user account password information
  • passwd -d 用户名: clear user password

The useradd command is used to create new system users in Linux. useradd can be used to create user accounts. After the account is built, use passwd to set the password of the account. And userdel can be used to delete the account. The account created by using the useradd command is actually stored in the /etc/passwd text file.

The passwd command is used to set user authentication information, including user password and password expiration time. System administrators can use it to manage system user passwords. Only administrators can specify user names, and general users can only change their own passwords.

4.7 Management of Linux system user groups

Each user has a user group, and the system can centrally manage all users in a user group. Different Linux systems have different regulations on user groups. For example, a user under Linux belongs to a user group with the same name as it, and this user group is created at the same time when creating a user.

The management of user groups involves adding, deleting and modifying user groups. The addition, deletion, and modification of groups are actually updates to the /etc/group file.

Commands related to the management of Linux system user groups:

  • groupadd 选项 用户组 : Add a new user group
  • groupdel 用户组: To delete an existing user group
  • groupmod 选项 用户组 : modify the attributes of the user group

4.8 Other common commands

  • pwd:  Display the current location

  • grep 要搜索的字符串 要搜索的文件 --color:  Search command, --color stands for highlighting

  • ps -ef/ ps aux:  These two commands are to view the current running process of the system. The difference between the two is that the display format is different. If you want to view a specific process, you can use this format: ps aux|grep redis (view processes including redis strings)

    Note: If you use the ps ((Process Status)) command directly, the status of all processes will be displayed, usually combined with the grep command to view the status of a process.

  • kill -9 进程的pid:  Kill the process (-9 means force termination.)

    Use ps to find the process first, then kill it with kill

  • Network communication command:

    • View the network card information of the current system: ifconfig
    • Check the connection with a machine: ping
    • View the port of the current system using: netstat -an
  • shutdown: shutdown -h now : Specify to shut down immediately; shutdown +5 "System will shutdown after 5 minutes": Specify to shut down after 5 minutes, and send a warning message to the login user at the same time.

  • reboot::  rebootReboot  . reboot -w:  Make a simulation of restarting (only the record will not restart).

Guess you like

Origin blog.csdn.net/am_Linux/article/details/130175687