Han Shunping Linux Basics

1. Course content

2. Linux application fields

1. Where is Linux used?

  •  The most powerful application of Linux: server

 3. Linux Overview

 

 3. The relationship between Linux and Unix

 5. Installation of VM and Linux

  • Basic instructions
    • Learning Linux requires an environment, we need to create a virtual machine, and then install a Centos system on the virtual machine to learn
  1. Install virtual machine 15.5 first
  2. Then install Linux (CentOS 7.6/centOS8.1)
  3. Principle diagram (drawing)

  •  vmware15.5 download

Official address: https://www.vmware.com/cn.html

Other addresses: https://www.nocmd.com/windows/740.html

  • VM installation steps
  1. Go to the BIOS and modify the settings to enable virtualization device support (F2, F10)
  2. Install virtual machine software (VM15.5)
    1. Enable CPU virtualization support in BOIS Enable BIOS virtualization settings | Enable CPU virtualization support - Baidu Experience
    2. Install again

6. centOS7.6 installation

 7. Three ways of network connection

  • Bridge mode : The virtual system can communicate with external systems, but it is easy to cause IP conflicts, because in the example there are a total of 255 IPs in the 192.168.0 network segment. If they all use the 192.168.0 network segment, once the number is too large, it is easy to cause repeat
  • The gateway of Zhang San's virtual system Linux is set to 192.168.0 to communicate with Li Si and Wang Wu.

  • NAT mode : Network address translation mode. The virtual address can communicate with external systems without causing IP conflicts.
  • Wangwu's host uses NAT mode . At this time, the virtual network Linux is set to 192.168.100.88. At this time, a corresponding virtual network card will be generated on the Wangwu host, and the range is also 192.168.100; for example, it is 192.168.100.99 at this time.
  • At this time, the virtual network card 192.168.100.99 generated by the Wangwu host and the virtual system Linux192.168.100.88 are interoperable.
  • The virtual system Linux192.168.100.88 finds the external classroom network through the proxy of ip192.168.0.50 of the Wangwu host.
  • NAT is network address translation, which is equivalent to giving your virtual machine a virtual IP, and then automatically converts it to your mother machine IP to access the outside when you need to access the outside.

  • Host mode : standalone system

8. Virtual machine cloning

If you already have a Linux operating system installed and want more, there is no need to reinstall it, just clone it.

  • Method 1: Directly copy an installed virtual machine file
  • Method 2: Use vmware’s cloning operation; Note: When cloning, you need to shut down the Linux system first
    • Right click on the virtual machine--Manage--Clone
    • Select Clone From: Current State of Virtual Machine
    • Select Create full clone

9. Virtual machine snapshot

If you are using a virtual machine system (such as Linux) and you want to return to a certain original state, that is to say, you are worried that some misoperations may cause system abnormalities and you need to return to an original normal running state. vmware also Providing such a function is called snapshot management

  • Applications
  1. After installing the system, first make a snapshot A
  2. After entering the system, create a folder and save a snapshot B
  3. Go back to the state where the system has just been installed, namely snapshot A
  4. Try to see if you can go back to snapshot B again

 

  1. Right-click the virtual machine--snapshot--take a snapshot--name the snapshot and add notes
  2. Create a new folder on the desktop -- name it whatever you want
  3. Do three snapshot operations in sequence
  4. Right-click the virtual machine--select Snapshot Management--click the one you want to return to, and then click Go in the lower right corner to return to the first snapshot.

  • Now return to state B, create a hello3 folder in state B, and take a snapshot, named snapshot D, as shown below.

10. Virtual machine migration and deletion

After the virtual machine is installed, it is essentially a file (placed in a folder), so it is very convenient to move the virtual system. You can copy or cut the entire folder of the installed virtual system to another location for use, and it is also easy to delete . Simple, use vmware to remove, and then click the menu --> delete from disk, or directly manually delete the folder corresponding to the virtual system,

Demo: Migrate and delete the previously cloned system

  • First, right-click on vnware and select Remove, but only the directory is removed.
  • Then right-click on the file directory and select Delete to truly remove it.

11. vmtools

1. Install vmtools

  • introduce
  1. After vmtools is installed, it allows us to better manage vm virtual machines under windows.
  2. Shared files for windows and centos can be set up

  • Example
  1. There is a shared folder in the D drive (for example) under the host
  2. The host and virtual system Linux can read, write and delete shared folders.
  3. In the virtual system Linux writes a copy of data to the file in the shared folder, and the host can immediately see the changes in the files in the shared folder.

  •  Steps to install vmtools
  1. Enter centos
  2. First right-click the CD-ROM drive to eject it.
  3. Click the virtual machine in the vm menu in the upper left corner -> Reinstall vmware tools (a WMware Tools CD will appear on the desktop, if not, right-click to cancel the installation and then reinstall)
  4. A vm CD will appear in centos. Right-click the CD to open it; when you see xx.tar.gz,

  1. Right-click to copy xx.tar.gz, open the desktop main folder-->select another location-->click computer-->select the opt folder-->paste xx.tar.gz in the opt folder

  1.  Right click on an empty space on the desktop --> open terminal
  2. Copy to /opt
    1. In the terminal cd /opt enter the opt directory
    2. ls View files in a directory
    3. Unzip the file tar -zxvf xx.tar.gz (enter VM and press the Tab key to automatically complete)
  3. Use the decompression command tar to get an installation file

    1. ls to view the unzipped directory
  4. Enter the directory where the vm was decompressed, under the /opt directory
    1. cd vmware-tools-distrib type decompression directory
    2. ls to view installation files
  5. install./vmware-install.pl
  6. Use all the default settings and press Enter, and the installation is successful. The following figure shows that the installation is successful

  1. Note: gcc is required to install vmtools
  2. To check whether gcc is installed, type gcc -v in the terminal, if it is installed, there will be a relevant version prompt

 2. Set up shared folders

  • basic introduction
  1. For convenience, you can set up a shared folder, such as D:/myshare on the D drive
  • Specific steps
  1. First create a shared folder myshare somewhere on the D drive, create a file hello.txt inside the folder and write the relevant content
  2. Menu in the upper left corner-->Virtual Machine-->Settings-->Options-->Select shared folder
  3. Select Always Enable-->Select the path to the shared folder-->Enable this path--Click Finish

  1. Windows and centos can share the D:/share directory and can read and write files.
  2. Shared files are in centos-->main folder-->other locations-->computer-->mnt folder-->hgfs folder-->mySHare folder
  •  Precautions
  1. Windows and centos can share files, and the reading and writing are synchronized. However, in the actual development process, the upload and download of files need to be completed remotely .
  2. Remote login, which will be explained in detail later.

12. Directory structure of Linux

  • basic introduction
  1. The Linux file system adopts a hierarchical tree directory structure. The top level of this structure is the root directory "/"; then other directories are created under this directory.
  2. It is very important to have a deep understanding of the Linux tree file directory
  3. A classic saying: In the Linux world, everything is a file

  • In the win operating system, there are CDE disks below. These disks can be named independently , and there are subdirectory files under the disk.

  •  In the Linux operating system, there is only one root directory "/", and there are many subdirectories under the root directory. The names of these subdirectories are stipulated and cannot be written indiscriminately.

 13. Detailed Explanation of Directory Structure

  • Specific directory structure (you don’t need to memorize it, just know it)
  1. /bin [ Commonly used ] (usr/bin,/usr/local/bin),
    1. It is the abbreviation of Binary, this directory stores the most frequently used directories
  2. /sbin  (/usr/sbin、/usr/local/sbin),
    1. s means Super User. What is stored here is the system management program used by the system administrator.
  3. /home【commonly used
    1. Stores the home directory of ordinary users. In Linux, each user has his own directory. Generally, the directory name is named after the user's account.
    2. For example, if you create a huomu account, there will be a huomu folder in the home directory.
    3. Open the terminal --> enter the command useradd jack --> create a folder for jack in the home directory (create a user for jack)
    4. userdel -r jack --> delete jack folder (delete jack user)
  4. /root [ commonly used ]
    1. This directory is the user home directory of the system administrator, also known as the super authority
    2. /lib is the most basic dynamic link shared library required for system startup. Its function is similar to DLL files in Windows. Almost all applications need to use these shared libraries.
    3. /lost+found This directory is usually empty. When the system is shut down illegally, some files are stored here.

      1. /lost+found is usually hidden. You need to run cd / --> ls in the terminal to see /lost+found.
  5. /etc [ commonly used ]
    1. All configuration files and subdirectories required for system management, such as installing the mysql database my.conf
  6. /usr [commonly used]
    1. This is a very important directory. Many applications and files of users are placed in this directory, similar to the program files directory under windows
  7. /boot【commonly used
    1. It stores some core files used when starting Linux , including some connection files and mirror files
  8. /proc [ cannot move ]
    1. This directory is a virtual directory, which is a mapping of system memory. Access this directory to obtain system information.
  9. /srv [ cannot move ]
    1. service abbreviation. This directory stores some data that needs to be extracted after the service is started.
  10. /sys [ cannot move ]
    1. This is a big change in the Linux 2.6 kernel. A new file system sysfs in the 2.6 kernel is installed in this directory.
  11. /tmp 
    1. This directory is used to store some temporary files
  12. /dev
    1. Similar to Windows Device Manager, all hardware is stored in the form of files.
  13. /media [ commonly used ]
    1. The Linux system will automatically recognize some devices, such as USB flash drives and optical drive lights. After recognition, Linux will mount the recognized devices to this directory.
  14. /mnt【Commonly used
    1. The system provides this directory to allow users to temporarily mount other file systems. We can mount external storage on /mnt/, and then enter the directory to view the contents.
  15. /opt
    1. This is the directory where additional software is installed for the host. For example, if you install the ORACLE database, you can place it in this directory. It is empty by default.
  16. /usr/local [ commonly used ]
    1. This is another directory for installing additional software for the host. It is usually a program installed by compiling source code.
  17. /var [ Commonly used ]
    1. This directory stores things that are constantly expanding. It is customary to put directories that are frequently modified in this directory, including various log files.
  18. /selinux [security-enhanced linux]
    1.        SELinux is a security subsystem that can control programs to only access specific files. It has three working modes. Can be set by yourself

 14. Log in to the Linux server remotely

Explanation: When the company develops, the specific application scenario is like this

  1. The linux server is shared by the development team
  2. Officially launched projects run on the public network
  3. Therefore, programmers need to remotely log in to Linux for project management or development
  4. Draw a simple network topology diagram (to help understanding)
  5. Remote login clients include Xshell6 and Xftp6. We learn to use Xshell and Xfpt6. Other remote tools are similar.

1. Introduction to Xshell

Description: Xshell is currently the best software for logging in to Linux remotely. It has smooth speed and perfectly solves the problem of Chinese garbled characters. It is currently the preferred software for programmers.

  • Xshell is a powerful secure terminal emulation software that supports SSH1, SSH2, and the TELNET protocol of the Microsoft Windows platform.

Xshell can be used under the Windows interface to access remote servers under different systems, so as to better achieve the purpose of remote control terminals

2. Download, install and configure

Download the free-for-home-school version

Address: https://www.netsarang.com/en/free-for-home-school/

  • Right-click to open the terminal in centos. Enter the command ifconfig to view the ip (the public network ip in the above figure)
  1.  Ping in the command prompt to ensure that the network can be pinged.
  2. Open Xshell to remotely log in to the public network, right click to open Xshell --> New --> fill in the ip address of the public network in the host --> OK
  3. Click to connect
  4. Accept once (you have to enter your username and password every time, recommended here), accept and save (you only need to enter it once, recommended at work)

15. Remote file transfer

  • introduce

It is a powerful SFTP and FTP file transfer software based on windows platform. After using Xfpt, windows users can safely transfer files between UNIX/Linux and Windows PC

  1. After installing xftp
  2. Click to open xfpt-->Click New-->Fill in the public network ip for the host-->OK
  3. Click OK-->Click Connect
  4. Can realize mutual file transfer from windows to linux 

 16. Quick start with vim

The Linux system has a built-in vi text editor

Vim has the ability to edit programs and can be regarded as an enhanced version of Vi. It can actively identify the correctness of syntax by font color, which facilitates program design, code completion, compilation and error jumps. It is particularly rich in functions that facilitate programming. widely used by programmers.

 1. Three commonly used modes of vi and vim

  • normal mode
    • Opening a file with vim will directly enter the normal mode (this is the default mode). In this mode, you can use the [up, down, left, and right] keys to move the cursor, and you can also use [delete characters] or [delete entire lines] To process the file content, you can also use [Copy and Paste] to process your file data
  • insert mode
    • Press i, I, o, O. a, A, r, R and other letters will enter the edit mode, generally press i
  • command line mode
    • Switch from insert mode to command line mode: press esc to exit, then enter colon :
    • In this mode, you can provide relevant instructions to complete reading, saving, replacing, leaving vim, displaying line numbers, etc., which are completed in this mode.

  • Exercise: Use vim to develop a Hello.java program and save step instructions and demonstrations
  1. vim Hello.java --> use the vim tool to create a Hello.java program

  1.  Enter i to enter the edit mode, and the words inserted will appear below
  2. In edit mode, after writing the program
  3.  If you need to save, you need to enter the command line mode from insert mode .
  4. Insert mode to enter command line mode: first press esc to exit and then enter :
  5. Press esc first , then enter : wq  ; w means write; q means exit; press Enter
  6. Check with ls ; there is a Hello.java file in the directory

  1. If you want to modify the file, enter vim Hello.java  and re-enter the editing interface.

 17. vim shortcut keys

Take a look at the switching diagrams of various modes of vi and vim

 1. Practice using shortcut keys

  1. Copy the current line: yy
    1. Copy 5 lines below the current line   : 5yy, and paste (enter p)
    2. Note: The copying action is performed in normal mode !
    3. The position set by the cursor is the yy position of the current line
  2. Delete the current line: dd
    1. Delete 5 lines downward from the current line : 5dd
    2. Note: The deletion operation is performed in normal mode !
    3. The position specified by the cursor is the dd position of the current line.
  3. Find a certain word in the file [ under the command line / keyword , press Enter to search, enter n to find the next one]

    1. General mode input: or / ; Enter command line mode 
    2. Enter / to search for characters (I don’t know why: the search characters are useless here) , for example /hello; then press Enter to search, there will be a green cursor, press n to search for the next one
  4. Set the line number of the file, cancel the line number of the file [under the command line : set nu and : set nonu ]

  5. For example, vim /etc/profile file enters this file. Since the file is relatively large and has a lot of content, in normal mode , use the shortcut keys to go to the last line [G] and the first line [gg] of the document.

  6. Enter "hello" in a file and then undo this action u

  7. For example, enter the vim /etc/profile file. In normal mode , move the cursor to, enter 20, and then enter shift+g.

    1. First enter the vim /etc/profile file into the file

    2. Then  : set nu to list the line numbers. Now you are in the command line mode. Press ESC to return to the normal mode.

    3. Enter line number 20, then enter shift + g

    4. Complete positioning

  8.  For more information, please see the documentation

 17. vim content organization

  • Shortcut key map 

18. Shut down and restart

1. Shutdown & restart command

  • basic introduction
  1. shutdown -h now shutdown immediately
  2. shudown -h 1 "hello, it will shut down in 1 minute
  3. shutdown -r now Restart the computer now
  4. halt shuts down, the function is the same as above
  5. reboot Now reboot the computer
  6. sync synchronizes memory data to disk
  • Attention to detail
  1. Whether you are restarting the system or shutting down the system, you must first run the sync command to write the data in the memory to the disk.
  2. The current shutdown/reboot/halt and other commands have been synced before shutdown.
  3. It is possible to execute the shutdown command in Xshell and execute the shutdown command in Linux

    1. Execute shutdown command in linux
    2. Right click to open terminal
    3. Enter shutdown -h now
    4. Open the virtual machine again, right click to open the terminal, and enter reboot ; restart the device
    5. The standard practice is to perform sync first; then perform shutdown or restart operations

20. Login and logout

  • basic introduction
  1. When logging in, try to log in with the root account as little as possible, because it is the system administrator and has the greatest authority to avoid operational errors. You can log in as an ordinary user. After logging in, use the "su - username" command to switch to the system administrator identity .
    1. Log in to Xshell with a normal user account,  
    2.  cd / root switch to the root directory, showing insufficient permissions
    3. Log in to the root account  through su - root , enter the password, and then enter the root directory through cd /root/
  2. Enter lgout at the prompt to log out of the account
    1. When the user is root, enter logout; it will return to when the user is huomu
    2. Executing logout again will log you out.
  3. Note: Entering logout in the terminal in centOS is useless. It will only prompt exit , and typing exit will only exit the terminal.

  • Usage details
  1. logout The logout command is invalid at the graphics run level and is valid at run level 3.
  2. The concept of run level will be introduced later.

21. User management 1

basic introduction

The Linux system is a multi-user and multi-tasking operating system. 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.

Add user

  • basic grammar
    •  useradd username
  • Applications
    • Case 1: Add a user millan
    • By default, the user's home directory is /home /milan
    • When logging in with millan, you will automatically switch to this directory.

  • Details
    1. When the user is successfully created, a home directory with the same name as the user will be automatically created
    2. You can also specify a new username for the directory through useradd -d and specify a home directory for the newly created user.
      1. Create a user in the specified home directory, the user name is test king

Specify/modify password

  • basic grammar
    • passwd username
  • Applications
    • Specify password for milan
    • Note: The command pwd displays which directory you are currently in.

delete users

  • basic grammar
    • userdel username
  • Applications
  1. Delete user milan, but keep the home directory: userdel username
    1. I deleted milan, but if I check it through ls, milan ’s home directory is still there.
    2. Although milan 's home directory is still there, it will not work if you log in with milan 's username and password at this time
  2. Delete user huomu and user huomu's home directory: userdel -r username
    1. Deleted huomu, checked through ls , huomu 's home directory is gone
    2. Note: This operation must be done with caution
  • Details
    • Before deleting, use su - root to switch to the user with the highest authority to delete
    • It is best not to delete users when ordinary users are already logged in, but to log out and directly log in to the account with the highest authority to delete them.
    • Discussion on whether to keep home directory
      • Under normal circumstances, it is best to keep the home directory and use the userdel username   to delete it and keep the home directory.

22. User Management 2

Query user information command

  • basic grammar
    • id username
    • Returns the group where the root user's uid group id belongs.
  • Applications
    • Case: Please check root information
  • Details
    • When the user does not exist, return no such user

Switch user

  • introduce
    • When operating Linux, if the current user's permissions are insufficient, you can use the su - command to switch to the highest authority user, such as root.
  • basic grammar
    •  su - switch username
  • Applications
    • Create a user jack, specify the password, and then switch to jack
  • Details
    1. To switch from a user with high authority to a user with low authority, you do not need to enter a password, and vice versa.
    2. When you need to return to the original user, use the exit/ logout command

 View current user/logged in user

  • basic grammar
    • whoami/ who am i

  • Display the login time and address from which to log in to the Linux server

23. User Management 3

user group

  • introduce
    • Similar to roles, the system can uniformly manage multiple users with common characteristics.
  • Add new group
    • Command:  groupadd group name
  • Delete group
    • Command:  groupdel group name
  • When adding users, add groups directly
    • Command: useradd -g user group username
  • Modify user's group
    • Command: usermod -g user group username
    • Change the group of zwj to mojiao

  • Applications
    • Add a user zwj and directly assign him to the wudan user group
      1. First groupadd the user group  and add the wudan group
      2. Then use useradd -g user group username add the user to the group
      3. Then use the id username to view user information
  • Details
    • If you have not created a group before and create a user directly, a group name with the same name as the user will be created by default.

User and group related files

  • /etc/passwd file
    • The user's configuration file records various information about the user.
    • The meaning of each line: Username: Password: User ID: Group ID: Commentary description: Home directory: Login Shell
      • The password is generally invisible or marked with an ×; user ID: uid: group identification number: gid; home directory: the home directory of the user;
    • Login Shell:

      • Send a cd command to the Linux kernel. The kernel cannot directly recognize it. The kernel does not have the ability to parse.
      • There is a Shell in the middle, which is responsible for parsing the cd message and sending it to the Linux kernel.
      • There are many types of Shells, the most commonly used ones are bash Shell and tc Shell.
  • /etc/shadow file
    • password configuration file
    • The meaning of each line: Login: Encrypted password: Last modified: Minimum interval: Maximum interval: Warning time: Inactivity time: Expiration time: Flag
  • /etc/group file
  • The configuration file of the group records the information of the groups included in Linux.
  • The meaning of each line : group name: password: group identification number: list of users in the group

25. Run level

Specify run level

  • basic introduction
    • Run level description
    • 0: Shut down
    • 1: Single user [retrieve lost password]
    • 2: There is no network service in multi-user state
    • 3: There is network service in multi-user state (the most used)
    • 4: Not used by the system and reserved for users
    • 5: Graphical interface
    • 6: System restart
  • Commonly used run levels are 3 and 5. You can also specify a default run level, which will be demonstrated later.
    • init 3 enters a network service without a graphical interface

  • Then use init 5 to switch back to the image interface
  • Applications
    • Command: init[0123456]
    • Switch to different running levels through init, such as 5-3, and then shut down

Specify run level

CentOS post-runlevel instructions

  • In the /ect/inittab file,
  • Simplified as follows:
    • multi-user.target ( after CentOS7): equivalent to: analogous to runlevel 3 (before CentOS7)
    • graphical.target ( after CentOS7): equivalent to: analogous to runlevel 5 (before CentOS7)
  • Check what the current runlevel is
    • Command: systemctl get-default
  • Set runlevel
    • Instruction: systemctl set-default TARGET.target (set target run level )
  1. View current default level
  2. Display is graphical level
  3. Set to multi-user level
  4. Then use the reboot command to restart, then CentOS will directly enter the non-graphical interface instead of entering the desktop.
  5.  
  6. Reset back to graphical interface

  7.   
  • important point
    • I use level 3 at work.
    • The lectures use level 5

 26. Retrieve root password

  1. Interview questions

    • How to retrieve the root password (see reference documentation)
    1. First, start the system, enter the startup interface, and press "e" in the interface to enter the editing interface.
    2. Be quick when pressing "e" and try to finish within 5 seconds
    3. Enter the editing interface and use the up and down keys on the keyboard to move the cursor down.
    4. Find the number of lines that start with "Linux16"
    5. At the end of the line enter: init=/bin/sh
    6.  After the input is completed, press the shortcut key: Ctrl+x to enter user single mode
    7. Enter the position where the cursor flashes: mount -o remount,rw /  
    8. Enter: passwd at the end of the new line. After completion, press the Enter key on the keyboard, enter the password, and confirm the password again. After the password is successfully changed, the passwd... style will be displayed, indicating that the password has been changed successfully.

    9.   In the position where the mouse blinks (in the last line), enter: touch /.autorelabel (there is a space behind touch and /), and press the Enter key on the keyboard after completion
    10. Continue to enter the position where the cursor flashes: exec /sbin/init (note: there is a space after exef and /), press Enter after completion, and wait for the system to change the password (it may take a long time).
    11. After completion, the system will automatically restart and the new password will take effect
    12. Next time you log in, you can use the passwd username to change the password. 

27. Help instructions

 man Get help instructions

  • Basic syntax: man [command or configuration file] (function description: get help information)

Case: View the help information of the ls command

  • Type man ls
  • For the content that is not displayed, press the space bar and it will continue to go down
  • Under linux, hidden files begin with .
  • Under Linux, ls -l means single column output.
  • ls -la (or al also works) means to list all hidden files in a single line
  • Options can be used in combination, such as ls -al/root to view all files in the root directory, including hidden files

help command

  • Basic syntax: help command (function description: get help information for shell built-in commands)
  • Applications
    • View help information for the CD command

     28. File directory command 1

Relative paths and absolute paths

Relative path: Start from the current directory and locate downwards

Absolute path: start from the root directory and locate downwards

File directory class

  •  pwd command

    • Basic syntax: pwd 
    • Function description: Display the absolute path of the current working directory
    • Application example: Display the absolute path of the current working directory
  1. Add a tom user
  2. Enter the tom user, create an a.txt file, and press Enter
  3. Press i to enter the edit mode (there will be the word insert at the bottom of the edit mode), enter the content,
  4. To save, you need to enter the command line mode. First press ESC to enter the command line mode from the edit mode, then enter : wq to save and exit.
  5. Enter the tom directory and use pwd to view the absolute path

  •  cd command

    • Basic syntax: cd [parameters]  
      • Function description: switch to the specified directory
      • cd ~ or cd    back to your home directory
      • cd .. Back to the previous directory of the current directory
    • Applications
      • Case 1: Use absolute path to switch to the root directory  cd /root
      • Case 2: Use a relative path to the /root directory
        • For example, currently in /home/tom
        •  cd ../../root

      • Case 3: Go back to the upper directory of the current directory, cd ..
      • Case 4: Go back to the home directory, cd ~

Twenty-nine, file directory command 2

File directory class

  • mkdir command

    • The mkdir command is used to create a directory
    • Basic syntax: mkdir [options] Directory to create
      • [options ]
        • -p : represents creating a multi-level directory
    • Applications
      • Case 1: Create a directory /home/dog
      •  mkdir /home/dog
      • Case 2: Create a multi-level directory /home/animal/tiger 

      • mkdir -p /home/animal/tige

  • rmdir command

    • The rmdir command deletes empty directories

    • Basic syntax: rmdir [options] Empty directory to delete

    • Applications

      • Case: Delete a directory /home/dog

        • /home/dog The following is an empty directory, you can delete it directly with rmdir

        • ​​​​​Note:

          • rmdir deletes an empty directory, if there is content in the directory, it cannot be deleted

          • If you need to delete a non-empty directory, you need to use rm -rf   to delete the directory ( please operate with caution )

  • touch command

    • touch command creates an empty folder
    • Basic syntax : touch file name
    • Applications
      • Case: Create an empty file hello.txx in the /home directory

30. File directory command 3

  • cp command

    • The cp command copies files to the specified directory
    • Basic syntax: cp [options] source dest
      • [options] 
        • -r : copy the entire folder recursively
    • Applications
      • Case 1: Copy /home/hello.txt to the /home/bbb directory

         

        • First create the bbb folder in the home directory: mkdir bbb,
        • Then copy the hello.txt file to the bbb/ directory
      • Case 2: Copy entire folder recursively
      • Example: For example, copy the entire directory of /home/bbb to /opt
      • cp -r /home/bbb/ /opt/    copy the directory itself together
    • Notice:
      • Force override without prompting: \cp
        • It will ask when forcing coverage, and you need to press y to confirm.
      • When you do not need to display a prompt message, you can use \cp: \cp -r /home/bbb/ /opt/
  • rm command

    • The rm command removes files or directories
    • Basic syntax: rm [options] File or directory to delete
      • [Options]  : -r  recursively delete entire folders
      • [Option]: -f    forces deletion without prompting
    • Applications
      • Case 1: Delete /home/hello.txt
        • cd /home/ enter the home folder
        • To delete the document rm hello.txt , enter Y to confirm. If you don’t want to see the deletion prompt, use the command rm -f hello.txt
      • Case 2: Recursively delete the entire folder /home/bbb
        • rm -r /home/bbb   deletes files. It will prompt you every time you delete a file.
        • rm -rf /home/bbb deletes files, force deletion without prompting
    • Notice:
      • Method to force deletion without prompting: bring the -f parameter

31. File directory command 4

  • mv command

    • mv command: 1. Move files and directories 2. Rename
    • basic grammar
      1. mv oldNameFile newNameFile (Function description: Rename)
      2. mv /temp/movefile /targetFolder (Function description: move files)
    • Applications
      1. Case 1: Rename the /home/cat.txt file to /home/pig.txt
        1. touch cat.txt creates an empty folder cat.txt in the /home directory
        2. mv cat.txt pig.txt Rename cat.txt to pig.txt
      2. Case 2: Move the /home/pig.txt file to the /root directory

        1. mv pig.txt /root/ Move the pig.txt file to the /root/ folder
        2. mv pig.txt /root/ ant.txt is called move and rename

      3. Case 3: Move the entire directory, such as moving /opt/bbb ​​to /home

        1. mv /opt/bbb /home

  • cat command

    • cat to view the file content (cat can only view but not modify)
    • Basic syntax:
      • cat [options] file to view
      • [options] : -n show line number
    • Applications
      • Case 1: View the contents of the /etc/profile file and display the line number
    •  Note :
      • cat can only browse files, but cannot modify files. For convenience of browsing, pipe commands are generally used | more
      • After adding the pipe command | more, the entire number of lines will not be displayed, and the word more will appear.
        • Press Enter and an extra line of content will appear.
        • If you want to turn the entire page of the displayed content, press the space button. 
  • more command

    • The more command is a text filter based on the VI editor. It displays the contents of the text file on a page-by-page basis in a full-screen manner. There are several shortcut keys (interactive commands) built into the more command.
    • Basic syntax:
      • more files to view
    • Instructions
    • Applications

      • Case: use more to view the file /etc/profile 

32. File directory command 5

  • less command

    • The less command is used to view the file content in split screen. Its function is similar to the more command, but it is more powerful and supports various display terminals. When the less command displays the file content, it does not display the entire file at once. Instead, the content is loaded according to the display needs, which is more efficient for displaying large files

    •  Basic syntax:

      • less file to view

    • Applications

      • Example 1: Use less to view a large file less /opt/essay.txt

        1. First use xfpt5 to upload the essay.txt to the /opt folder

  • echo command

    • echo outputs content to the console

    • Basic syntax:

      • echo [options] [output content]

    • Applications

      • Case 1: Use the echo command to output environment variables, such as outputting $HOSTNAME

        • echo $HOSTNAME

      • Case 2: Use the echo command to output hello, world!

    • head command

      • The head command is used to display the content at the beginning of the file. By default, the head command displays the first ten lines of the file.
      • Basic syntax:
        • head file (function description: view the content of the first ten lines of the file)
        • head -n 5 file (Function description: View the first five lines of the file, five can also be any number of lines)
        • Applications

          • Case 1: View the first 5 lines of code in /etc/profile (blank lines count as one line)
            • head -n 5 /etc/profile
      • tail command

        • tail is used to output the tail content of the file, by default the tail command displays the first 10 lines of the file
        • Basic syntax:
          • tail file (function description: view the contents of the last 10 lines of the file)
          • tail -n 5 file (function description: view the content of the last 5 lines of the file, 5 can be any number of lines)
          • tail -f file (function description: track all updates of this document in real time)
        1. Applications:

          • Case 1: View the last 5 lines of code in /etc/profile (empty lines are also counted as one line)

            • tail -n 5 /etc/profile
          • Case 2: Monitor mydate.txt in real time to see if the file changes, whether you can see it and add the date in real time
          1. First cd /home/ to enter the home directory
          2. Then touch mydate.txt to create a mydate.txt document
          3. tail -f mydate.txt tracks document updates in real time. To exit, use Ctrl + C
          4. In order to facilitate the observation of document updates, open the terminal

          5. Enter echo "hello" > /home/mydate.txt Enter the content "hello" into the txt document

          6. Later, I saw hello entered by the terminal into mydate.txt in XShell.
          7. Ctrl + C exit real-time tracking mode                                                         
  • > directives and >> directives

    • > Instruction: overwrite the original content and redirect the output
    • >> Command: Will not overwrite the original content, but append it to the end.
    • Basic syntax:
      • ls -l > file (function description: write the contents of the list into a.txt (overwrite)
      • ls -al >> file (function description: append the list content to the end of the file aa.txt)
      • cat file 1 > file 2 (function description: overwrite the contents of file 1 to file 2)
      • echo "content" >> file
    • Applications
      • Case 1: Write the file list in the /home directory to /home/info.txt and overwrite it.
        • ls -l /home > /home/info.txt [If info.txt does not exist, it will be created]

          • ls -l /home (or ls -l is also written to the terminal) to write the displayed content to the terminal
          •  ls -l /home > /home/info.txt writes the contents of the list into info.txt, because there is no info.txt file, so an info.txt file will be created.

          • cat info.txt View info.txt document

      • Case 2: Append the current calendar information to the /home/mycal file
        • cal displays current calendar information
        •   cal >> /home/mycal Append calendar information to the mycal folder
        •     

33. File directory command 6

  • ln command

    • Soft links are also called symbolic links. They are similar to shortcuts in Windows. They mainly store paths to other files.

      • There is a home directory under the root directory, and there is a symbolic link myroot under the home directory.
      • The symbolic link myroot points to another directory root under the root directory
    • basic grammar
      • ln -s [original file or directory] [soft link name] (Function description: Create a soft link to the source file)
    • Applications
      • Case 1: Create a soft link myroot in the /home directory and connect to the /root directory
        • ln -s /root/ /home/myroot creates a soft connection myroot
        • ls -l writes the displayed content to the terminal,
        • myroot points to the root directory, and l at the front means link link

      • Case 2: Delete the soft connection myroot

        • rm /home/myroot
    •  history command

      • View the historical commands that have been executed, and you can also execute historical commands
      • Basic syntax:
        • history (function description: view the history commands that have been executed)
        • !n (function description: view the nth instruction that has been executed)
        • Applications
          • Case 1: Display all historical commands
          • Case 2: Display the 10 most recently used instructions

            • history 10
          • Case 3: Execute the instruction with history number 5
            • !5

34. Time and date instructions

  • date command - displays the current date

    • Basic syntax:
      • date (function description: display current time)
      • date %Y (Function description: display the current year)
      • date %m  (Function description: display the current month)
      • date %d  (function description: display the current day)
      • date %Y- %m- %d %H:%M:%S (function description: display year, month, day, hour, minute, second)
    • Applications
      • Case 1: Display current time information
      • Case 2: Display the current time year, month and day
        • date "%Y- %m- %d"
      • Case 3: Display the current time year, month, day, hour, minute and second

        • date "%Y- %m- %d %H:%M:%S"
  • date directive - set the date 

    • Basic syntax:
      • date -s string time
    • Applications
      • Case 1: Set the current time of the system, such as 2021-11-11 11:22:22
        • date -s "2021-11-11 11:22:22"
  • cal command

    • view calendar command cal
    • Basic syntax:
      • cal [option] (Function description: without options, display this month’s calendar)
    • Applications
      • Case 1: Display the current calendar cal
      • Case 2: Display the 2020 calendar: cal 2020

Thirty-five, search command 1

  • find command

    • The find command will recursively traverse its subdirectories downwards from the specified directory, and display the files or directories that meet the conditions in the terminal.
      • Basic syntax:

        • find [search scope] [options]
        • [options]
      • Applications

        • Case 1: By file name: Find the hello.txt file in the /home directory based on the name

          • find /home -name hello.txt

        • Case 2: By owner: Find files with the user name nobody in the /opt directory

          • find /opt -user nobody

          • Case 3: Find files larger than 200M in the entire Linux system (+n is greater than -n, less than n and equal to n refers to the size of the file, such as the 200M below )

            • find /( / represents searching the entire system from the root directory) -size +200M

            • ls -lh converts the display of 453365760 into a more intuitive 433M

  • local directive

    •  The local command can quickly locate the file path. The local command uses the pre-established local database of all file names and paths in the system to quickly locate a given file. The local command does not need to traverse the entire file system, and the query speed is faster. In order to ensure the accuracy of the query results For accuracy, the administrator must update the loacal moment

    • basic grammar

      •    local search files     

      • Notice:

        • Since the local command queries based on the database , you must use the updatedb command to create the locate database before running it for the first time.

        • Applications

          • Case 1: Use the local command to quickly locate the directory where the hello.txt file is located

            • First execute updatedb to create a local database

            • local hello.txt Check which directory the hello.txt file is in

  • which command

    • Check which directory a command is in,

    • Applications

      • For example, in which directory is the ls command located? 

36. Search command 2

  • grep command and pipe symbol

    • grep filter search,
    • Pipe character: "|", indicating that the processing result output of the previous command is passed to the subsequent command processing

      • Use cat a.txt to display the content of the document to the terminal, but only want to display a certain content in the txt document
      • Use cat a.txt | grep "the document content keywords you want to find"  to filter the display
    • basic grammar

      • grep [options] Find content source files
      • [options]
    • Applications:
      • Case 1: Please find the line where "yes" is located in the hello.txt file, and display the symbol

        • Writing method 1: cat/home/hello.txt | grep "yes"
        •  
        • Writing method 2: grep -n "yes" /home/hello.txt

Thirty-seven, compression and decompression 1

  • gzip/gunzip directive

    • gzip is used to compress files, gunzip is used to decompress them
    • basic grammar
      • gzip file (function description: compressed file, can only compress the file into a .gz file )
      • gunzip file.gz (function description: decompress file command)
    • Applications
      • Case 1: gzip compression, compress the hello.txt file under /home
        • gzip /home/hello.zip
      • Case 2: Gunzip compression, decompress the hello.txt.gz file under /home
        • gunzip /home/hello.txt.gz
  • zip/unzip command

    • zip is used to compress files and unzip is used to decompress them. This is very useful in project packaging and release.
    • Basic syntax:
      • zip [option] The content to be compressed by XXX.zip (function description: command to compress files and directories)
        •  [options]
          • -r: Recursive compression, that is, compressed directories
      • unzip [option] XXX.zip (function description: decompress files)
        •  [options]
        • -d<directory>: refers to the directory where the decompressed files are stored
      • Applications
        • Case 1: Compress all files under /home into myhome.zip
          • zip -r myhome.zip /home/ (including home itself is also compressed)
        • Case 2: Extract myhome.zip to the /opt/tmp directory
          • First create mkdir /opt/tmp
          • unzip -d /opt/tmp /home/myhome.zip

Thirty-eight, compression and decompression 2

  • tar command

    • The tar command is a packaging command, and the final packaged file is a .tar.gz file.
    • Basic syntax:

      • tar [option] XXX.tar.gz packaged content (function description: packaging directory, compressed file format.tar.gz)
      • Option description
    •  Applications
    • Case 1: Compress multiple files, compress /home/pig.txt and /home/cat.txt into pc.tar.gz
      • tar -zcvf pc.tar.gz /home/pig.txt /home/cat.txt
    • Case 2: Compress the /home folder into myhome.tar.gz
      • tar -zcvf myhome.tar.gz /home/
    • Case 3: Unzip pc.tar.gz to the current directory and switch to /opt/
      • tar -zxvf pc.tar.gz
    • Case 4: Extract myhome.tar.gz to the /opt/tmp2 directory
      • mkdir /opt/tmp2
      • tar -zxvf /home/myhome.tar.gz -C /opt/tmp2

 40. Basic introduction to Linux group

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

  1. owner
  2. Group
  3. other groups
  4. Change the user's group

41. Linux owners

  • Generally, the creator of the file, who created the file, automatically becomes the owner of the file.
  • View file owner

    • Command: ls -ahl
    • Applications
      • File owner is root
  • Change file owner

    • Command: chown username filename
    • Applications
      • Use root to create a file apple.txt, and then change its owner to tom
      • chown tom apple.txt file owner changed from root to tom

 Forty-two, group

  • basic instructions
    • groupadd group name
  • Applications
    • Create a group, monster
      • groupadd monster
    • Create a user fox and put it in the monster group
      • useradd -g monster fox
  • The group where the file/directory belongs

    • When a user creates a file, the group of the file refers to the group of the user
    • View the group to which the file/directory belongs

      • Basic instructions:
        • ls -ahl
      • Applications
        • Use fox to create a file to see which group the file belongs to
    • Modify the group the file belongs to

      • Basic instructions:
        • charp group name file name
      • Applications:
        • Use the root user to create the file orange.txt, see which group the file currently belongs to, and then modify the group where the file belongs to the fruit group.
          • groupadd fruit Create a fruit group
          • touch orange.txt creates orange.txt file
          • Check which group the current file belongs to -> root
          • chgrp fruit orange.txt

         

Forty-three, modify the group

  • other groups

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

  • Change the user's group

When adding a user, you can specify which group the user is added to. Similarly, you can use root administrative rights to change the group a user belongs to.

  •   Change the user's group
    • instruction:
      • usermod -g group name username
      • usermod -d directory name username changes the initial directory where the user logs in 
        • Note: The user needs to have permission to enter the new directory     
    • Applications
      • Change the user zwj from the original group to the wudan group 
        • id zwj first check which group zwj is in
        • Then cat /etc/group to see if there is a wudan group
        • If the file is large, check whether there are any used keywords and use the command cat /etc/group | grep wudan
        • usermod -g wudan zwj

         

         

 

 

Guess you like

Origin blog.csdn.net/z972065491/article/details/132182687