Linux basic instructions

Keywords: C++ Linux leetcode linked list

Command format

command [options] [arguments]

Basic instructions: Directory related instructions, ordinary file instructions, matching search instructions, permission related instructions
Instruction usage rules: instruction name [operation option] [operation object]

Instruction Name: the name of a command, indicating the command to be executed 
Operation options: used to formulate sub item functions to complete an instruction
 Operand: Specifies the object to manipulate

Directory structure under Linux
In the computer, files are actually stored on disk (hard disk)
The disk has at least two partitions: swap partition and file system partition
Swap partition: swap partition, which is used when there is not enough memory
File system partition: file system partition, which is used for file management. A hard disk can have multiple file system partitions
Windows will assign a drive letter to each disk partition, that is, a large folder. The storage space occupied by the files in this folder comes from this partition. The number of partitions can have multiple drive letters. The file system management of window is similar to assigning a directory structure to a partition.
Different from Linux, the Linux directory structure is unique, but you can mount partitions separately for a specified directory, but at least one partition must be hung on the root directory. The directory structure has nothing to do with the number of disk partitions. The directory structure under Linux is unique. The implementation is to mount the specified partition to a directory, that is, to allocate space to a folder separately. The space occupied by the sub files under this folder comes from this partition. The directory structure under Linux is unique, a tree structure, / is the root directory, Is the final parent directory of all files
Linux is a multi-user operating system (a system can log in to multiple users for operation at the same time). Each user has his own home directory

Absolute path: the representation of a path, starting with the root directory, is called an absolute path
Relative path: the representation of a path, starting with the current directory, is called relative directory (it will vary with the directory)
Two special directories:

. Used to represent the directory itself
.. Used to represent the last directory of a directory (the parent directory of a directory)

Simple command:
Who: lists the relevant information of all users who are using the system
echo: display the parameters in the command line on the screen; It is often used as an output prompt in shell scripts.

echo hello
echo $SHELL

Date: displays or sets the date and time of the system

date
date -s 11/03/2003
date -s 18:33:23

cal: display calendar

cal
cal 2020
cal 10 2020

Clear: clear the information on the screen

File processing command:
ls: browse directory (directory is what we usually call folder) - display the file information inside a folder
View the contents of the current folder by default. Open a terminal and it will be in a folder (home directory) by default
-l: Display file contents in long format (view details)
-a: View all files, including hidden files (files whose file names begin with the. Symbol)
-d display directory properties
-R displays the directory and subordinate subdirectory structure
-i inode number of the output file
ls -a -l workspace == ls -al workspace

 ls -al
 Operation options can be connected together. An operation option usually represents only a fine molecular function without sequence requirements

touch: if the file does not exist, a new file is created, or if the file exists, it is the time attribute used to refresh the file

Format: touch [parameter]

passwd: user changes password
pwd: displays the path of the current folder
mkdir: create a directory

Format: mkdir [option] Directory name
-m Set access permissions for the new directory
-p Create a directory at multiple levels. If that layer does not exist, create that layer to the bottom

Example:

mkdir ./test/aaa    In the current file directory test Create a directory aaa catalogue
mkdir -p ./test3/bbb 

rmdir: delete empty directory

Format: rmdir [option] Directory name
-p Recursively delete directory

rm: delete a file or directory

Format: rm [option] File or directory
-i Interactive delete file
-f Forced deletion of files without prompt
-r Delete directory (delete yourself after recursively deleting all files in a file)

cp: copy a directory or file to the specified location (there are two operation objects, one is the source object and the other is the target location)

Format: cp [option] Source file or directory destination directory
-r The copy directory includes all children in the directory
-f Overwrite the existing target file without prompting
-i Overwrite the existing target file and give a prompt

Example:

cp -r learn/workspace

mv cut, or move a file or directory to the specified location and rename the file (there are two operation objects, one is the source object and the other is the target location)

Format: mv [Source file or directory] [Target directory]
mv learn/workspace/

cd changes the current directory, or enters a directory

cd workspace/
cd ~ Quickly return to the current user's home directory

General document related instructions
Note: files under Linux are not distinguished by file suffixes, which are just convenient for users to distinguish

View file:
cat: (short file) print the file content to the terminal, connect or display the file content

Format: cat [option] file name
-b Number all non blank lines
-n Number all lines

more page displays the contents of the file

Format: more [option] file name
    -num How many lines are displayed at a time
    +num From the first num Line start display
    Carriage return: next line, space or CTRL+F: Page down, CTRL+B: Page up, Q: Exit the display, q/Q sign out mor Orders, h display help information

less pagination displays the contents of the file, which can turn the page up

Format: less [option] file name
CTRL+F/B: Page up and down, up and down arrows or J/K: Scroll by line,/string: Look down the specified string,?string: Look up the specified string, Q: sign out

head prints the first n lines of the file, 10 lines by default

Format: head [option] file name
-n Before display n Row, the default value is 10
-nc Show front n Bytes
head -n 1 passwd

tail prints the contents of n lines at the end of the file. The default is 10 lines

Format: tail [option] file name
-n Show last n Row, default 10 rows
-nc Show last n Bytes
    -n num Specifies the number of lines to print
tail -n 1 passwd
    -f Dynamically refresh the contents at the end of the file (after the command is run, it will not exit, but always wait for new data at the end of the file, then refresh the display)

wc counts the number of lines, words and bytes of the file

Format: wc [option] file name
    -l Count rows line
    -w Count words word((in string)
    -c Count bytes byte

File search:
which view the directory where the command is located

Format: which command
Example: which ls Displays alias information for the command

find finds the specified file or directory

Format: find Search path [option] Search keywords
-name Find by file name
-size Find by file size
-user Find by file owner
-ctime Find according to modification time
-type Find by file type

locate finds files or directories with search keywords

Format: locate [option] Search keywords

grep searches the file for lines that match the string

Format: grep [option] Specifies the string source file
-v Lists rows that do not match strings or regular expressions
-c Count matched rows
-l Displays only the file names that contain matching files
-i A case insensitive match is generated. The default state is case sensitive    
echo"nihao">>test.txt Append write data to the end of the specified file
echo "nihao"Print the specified string -- writes the data to the standard output file

Standard input file - keyboard device, standard output file - display, standard error file - display

Redirection: standard output redirection
    The data originally written to the display device is no longer written to the standard output, but to the specified file
>>: Append redirection to append data to the end of the file
>: Clear the redirection, clear the original contents of the file, and write the redirected data
<:input redirection 

test.txt standard output redirection, so that the standard output does not point to the display device, but to the specified file
Write the data originally written to the display device to the specified file instead of the display
test.txt

Pipe symbol:|
Connect the two commands and hand over the output results of the previous command to the subsequent command for processing
Example: print the third line of passwd file
head -n 3

Compression decompression instruction

zip/unzip:zip Compression and decompression of format
gzip/gunzip:gzip Compression and decompression of format
bzip2/bunzip2: bzip2 Compression and decompression of format
tar:Packaging tools
    -c pack
    -x Unpack
    -z While packing or unpacking gzip Format for compression and decompression
    -j Package or unpack with bzip2 Format for compression and decompression
    -v Display detailed information while packaging or unpacking
    -f Specifies the package name
 tar -czvf **.tar.gz  file1 file2 ... / tar -xzvf **.tar.gz

Match lookup instruction:
Find the file in the specified directory: find

-name Find by file name find ./ -name"*hello"
    -type Find by file type find ./ -type d [fdplcb] 
    	 f-Ordinary file  d-catalogue  p-The Conduit  l-Symbolic link  c-Character device  b-Block device
    -size Find by file size find ./ -mtime -1 --Find files within a day
    -[amc]time: In 24 hours find ./ -mtime -1
    -[amc]min: In minutes
    -exec Perform the specified operation on the found file
```bash

find ./ -name "*test*" -exec rm -rf{}\

Match content in specified file: grep

 -i ignore case
    -v Matches a line that does not contain the specified string
    -R Recursively match the files in the specified directory one by one
        -R Option is not a file but a directory
    grep -ivR "main" ./
    grep -i "main" ./a.txt
Shortcut operation
    ctrl+c Interrupt current operation
    ctrl+d Standard input completed or as of
    tab Key: command file name auto completion
    ↑Keys: find historical operation commands

The essence of a shell is software. It is a command-line interpreter that captures user input and executes the corresponding function program (shell command)
Type of shell:
bash
dash
...
In fact, the shell is also a software. Its function is to capture the user's input, understand what the user wants to do through the input information, and then execute the corresponding function program
The system kernel user cannot access directly because direct access is unsafe. The kernel provides an access interface to the outside. In order to facilitate the operation of the system, the common functions have been written directly. For example, the function of browsing the directory has directly written the ls program shell instruction

Operation authority:
Authority: limitation of power
Linux system is a multi-user operating system

User permissions:
In Linux, the system divides users into two categories: Administrator - root and ordinary users
Switch user: su root / su username
User authorization: sudo CP. / a.txt/bin/sudo is / root
**File permission: * * user's right control over the operation of the file

    Classification of users: file owner (primary user), file group, and other users
    Classification of operations: readable or not-r(read),Is it writable-w(write),Executable-x(execute)  Browse, create or delete, enter

Command operation:
Umask: view or set the mask of the default permission of the system default file, which affects the default creation permission of the file. The umask command works only for the current shell environment.
Meaning of umask mask
Note: the file created by default does not have executable permissions
If you want the file to have run permission, you can only set it by the chmod command

Format: umask mode
see: umask/umask -S    set up:umask 003

chmod: modify permissions for existing files
Two methods:
Symbol mode: setting permissions with strings

Format: chmod key file name

Absolute mode: use octal numbers to set permissions

Format: chmod mode file name
    chmod [ugoa][+/-][rwx] file
    chmod 777 b.c
    chmod o+x.a,c / chmod 777 a.c

chown change the owner of a file or directory (this command is only available to super users)

Format: chown [option] User files/catalogue
    -R Recursively change the primary user of the specified directory and all subdirectories and files

chgrp

  Format: chgrp [option] Group name file name/Directory name
    -R Recursively change the user group of all subdirectories and files under the specified directory machine

Link file
function
Create links between files
In fact, it is to assign another name to an existing file in the system that can be used to access it
advantage
For this new file name, you can specify different access rights to control the sharing and security of information
Connection type
Hard link

Format: ln Source file link name

Symbolic link

User management commands
user management
useradd: add a user (it can only be used by the root user. The newly created user cannot log in temporarily and needs the passwd command to set the password for it)

Format: useradd [option] user name
    -d Specify user home directory
    -m Specify user login directory
    -g Specifies the user group for the user
    -s Specifies the name of the user shell
    -D For display useradd The default value used by the command

userdel: delete user

Format: userdel [option] User account
    -r Delete files in the primary directory of the user's home directory

    usermod: Modify user information( ID Value 0-999 (reserved for system account)
    Format: usermod [option] user name
    -d Modify user login directory
    -g Modify the group to which the user belongs
    -l Modify user account name
    -s Modify the password used after the user logs in shell
    -u Modify user ID

User group management
Group add: Add User Group

Format: groupadd [option] Group name
    -g Specifies the name of the new user group ID

groupdel: delete user groups

 Format: groupdel Group name

groupmod: modify user group properties

Format: groupmod [option] [User group]
    -g Create a new group for the user group ID
    -n Modify the user's group name

User switching
su: switch between users

 Format: su -username

When switching users with the su command, you need to enter the password of the target user
When switching from root user to other users, you can not enter a password
When the default option is the same as the user name, switch to the root user
Enter "exit" on the command line to exit the target user

sudo: can be regarded as restricted su
The administrator can grant some ordinary users to perform some operations performed by root without knowing the root password
Before using the sudo command, you need to configure the permissions to be used for the current user by modifying the / etc/sudoers file
/The etc/sudoers file has certain syntax specifications. In order to avoid syntax errors after modification, you should use the visudo command to open the file for modification

Sticky bit: a special permission bit. The permissions for other users are to restrict other users from creating files in a directory (directory with sticky bit set), but they can only delete their own files, not others' files chmod_t dir

Basic instruction:
Directory related instructions: ls,pwd,mkdir,rm,cp,mv,cd
File related pointers: touch,cat,more,less,head,tail
Compression related instructions: zip/unzip,gzip/gunzip,bzip2/bunzip2,tar -cxzjvf
Matching lookup instruction: grep -ivR,find -name/-type/-size/-time
Shortcut keys: tab,ctrl+c,ctrl+d

Installation and login commands: login, shutdown, halt, reboot, install, mount, umount, chsh, exit, last;
File processing commands: file, mkdir, grep, dd, find, mv, ls, diff, cat, ln;
Commands related to system management: df, top, free, quota, at, lp, adduser, groupadd, kill, crontab;
Network operation commands: ifconfig, ip, ping, netstat, telnet, ftp, route, rlogin, rcp, finger, mail, nslookup;
System security related commands: passwd, su, umask, chgrp, chmod, chown, chattr, sudo ps, who;
Other commands: tar, unzip, gunzip, unarj, mtools, man, unendcode, uudecode.

Representation of file permission value

Character representation

Octal numeric representation

Posted by mikie46 on Fri, 19 Nov 2021 21:39:48 -0800