[Linux] Linux background && common basic instructions

1. Linux background

Linux is a free and open source UNIX-like operating system whose kernel was first released by Linus Torvalds in 1991, and later, with the addition of user-space applications, became the Linux operating system system. Strictly speaking, Linux is just the operating system kernel itself, but the term "Linux kernel" is often used to mean that. Linux, on the other hand, is often used to refer to a complete operating system based on the Linux kernel, which includes GUI components and many other utilities

The GNU General Public License (GNU GPL or GPL for short), is a widely used free software license agreement, originally written by Richard Stallman for the GNU project, GPL gives computer programs free software The definition of any GPL-based software development derived products must adopt the GPL license when released, and the source code must be made public

Linux is the most famous example of the development of free software and open source software. As long as the GNU General Public License is followed, any individual or institution can freely use all the underlying source code of Linux, and can also freely modify and redistribute it. With the rapid development of the Linux operating system, various open source software and practical tools integrated on Linux have also been applied and popularized. Therefore, Linux has also become synonymous with open source software.

2. Basic instructions under Linux

ls command

Syntax : ls [options] [directory or file]

Function : For a directory, this command lists all subdirectories and files under the directory. For files, the filename is listed along with other information

Common options

-a lists all files in the directory, including hidden files starting with .

-d Display directories as files instead of the files under them. Such as: ls -d specifies the directory

-i Outputs index information for the i-node of the file. Such as ls -ai specified file

-k Indicates the size of the file in k bytes. ls –alk specifies the file

-l list detailed information of the file ls -l is equivalent to ll

-n use numeric UID, GID instead of name

-F appends a character to each file name to indicate the type of the file, "*" means executable ordinary file; "/" means directory; "@" means symbolic link; "|" means FIFOs; "= "Indicates sockets (sockets) (directory type identification)

-r reverse sort directory

-t sort by time

-s outputs the size of the file after the l file name (sort by size, how to find the largest file in the directory)

-R List files in all subdirectories. (recursive)

-1 output only one file per line

command demo

ls ls -l and ll : We can see that ls just lists the name of the file, while ls -l will display other information of the file. At the same time, under linux, the ls -l and ll commands have the same effect

ls: Display the name of the file or directory under the current path, hidden files are not displayed by default

ll: Display more detailed attribute information of files or directories under the current path, including hidden files

insert image description here

ls -al (ll -a) : When we add the -a option, we will find that three more files beginning with . are displayed, which are the current directory and the parent directory

insert image description here

If the file we created starts with ., it will not be displayed if we do not include -a. This type of file is called a hidden file

insert image description here

ls -d : When we bring the -d option, the system will display the directory information

-d: will not enter the directory, but display the directory itself

insert image description here

ls -R : When we bring the -R option, the system will directly display the information of the files in the directory

insert image description here

knowledge development

1. What do you do when writing instructions in Linux? Writing instructions under linux is equivalent to learning basic operations under windows. The former is a command line, while the latter is a graphical interface. There is no difference between the two at the operating system level

2.Linux is an operating system, and the operating system is a management software. It provides good (safe, Stable, efficient) purpose of the operating environment

3. A file is composed of content data and attribute data. The content data is the data we write into the file, and the attribute data refers to the size, type, permission, creation space, etc. of the file. Therefore, an empty file is also Occupies disk space, although there is no content data, but it also stores the attribute data of the file

4. In Linux, ls -l is equivalent to ll. In addition, the compound options of other commands and -l can also be reduced. For example, ls -al can be abbreviated as ll -a

5. The files starting with . in linux are called hidden files, a single . is the current directory, and ... is the parent directory

6. By default in linux, black is for ordinary files, blue is for directories, linux directories are equivalent to windows folders

pwd command

Syntax : pwd

Function : Display the directory where the user is currently located

command demo

insert image description here

cd command

Syntax : cd directoryname

Function : Change the working directory. Change the current working directory to the specified directory

Common options

cd … : return to parent directory

cd ~: enter the user's home directory

cd -: return to the most recently visited directory

command demo

insert image description here

/home/XXX: The working path called XXX, or the home directory----when a specific user logs in, the default path is located, and the home directory of the root user is called /root

If we continue to use the command to return to the upper level directory (cd ...), we cannot return to the root directory. Under Windows, we cannot return to the display disk interface, otherwise we can only exit. The two are similar

insert image description here

knowledge development

1. Whether it is windows or Linux, the file and directory structure of their disk is a tree structure (multi-fork tree), because in the tree structure, the path we find a file or directory is unique, then we You can use a unique identifier to mark a file or directory. In addition, through the characteristics of files/directories and tree structures, we can know that the leaf node of the tree may be a file or an empty directory, and the non-leaf node of the tree must be a directory

insert image description here

2. There are two ways for us to identify a file/directory or enter a file/directory. One is the absolute path, which starts from the root directory and identifies/finds one level at a time until the file/directory is found. Directory, the other is the relative path, which starts from the current path and keeps searching backwards. The absolute path is safe to use but it is troublesome to write and compare. The relative path is convenient but not so safe. After the file/directory is transferred, it may not be found. To the file/directory we want to find, so official documents generally use absolute paths, and we generally use relative paths in our daily life, such as ./a.out is the most common example, because relative paths are relatively simple

3. In windows, use \ as the path separator, but this is not particularly standardized, thinking that it may conflict with escape characters, while Linux uses / as the path separator

tree directive

Syntax : tree[options][directory]

Function : List the contents of the directory in a tree structure format

Common options

  • -a list all files, including hidden files, directories
  • -C adds color printing to the list of files and directories, which is convenient for distinguishing file types
  • -d list only directory names, not contents
  • -D list only file or directory change times
  • -L The maximum display depth of the directory tree
  • -p Print file permissions while printing structure
  • -l Follow symlinks of directories as if they were directories, avoiding links that cause recursive loops
  • -F Add a file type indicator after each entry (if the directory is /)

command demo

insert image description here

touch command

Syntax : touch [option]... file...

Function : The touch command parameter can change the date and time of the document or directory, including access time and change time, or create a new file that does not exist

Common options

-a or --time=atime or --time=access or --time=use only change the access time

-c or --no-create do not create any documentation

-d use the specified datetime instead of the current time

-f This parameter will be ignored and not processed, it is only responsible for solving the compatibility problem of the BSD version touch command

-m or --time=mtime or --time=modify only change the change time

-r Set the date and time of the specified document or directory to be the same as the date and time of the reference document or directory

-t use the specified datetime instead of the current time

knowledge development

1. There are 3 types of file time:

  • Recent access time (access): After viewing files such as cat less, the time will change
  • Last modification time (Modify): After using a compiler such as nano/vim to modify the content of the file, all three times will change
  • Last modification time (Change): After chmod modifies file permissions or attributes, this time will change

2. The stat command can display the status of files or file systems

3. touch can only be used to create ordinary files, not directories

command demo

insert image description here

mkdir command

Syntax : mkdir [options] dirname...

Function : Create a directory named "dirname" under the current directory

Common options :

-p, --parents can be a path name. At this time, if some directories in the path do not exist yet, after adding this option, the system will automatically create those directories that do not exist, that is, multiple directories can be created at one time;

command demo

insert image description here

rmdir command

rmdir is a command corresponding to mkdir. mkdir is to create a directory, and rmdir is to delete the command

Syntax : rmdir [-p] [dirName]

Applicable objects : all users with current directory operation authority

Function : delete empty directory

Common options

-p When the subdirectory is deleted, if the parent directory also becomes an empty directory, delete the parent directory together

command demo

insert image description here

rm command

The rm command can delete files or directories at the same time

Syntax : rm [-firv] [dirName/dir]

Applicable object : all users

Function : delete file or directory

Common options

-f Even if the file attribute is read-only (that is, write-protected), delete it directly

-i Ask for confirmation one by one before deleting

-r delete the directory and all files under it

command demo

rm deletes normal files by default

insert image description here

rm removes directories recursively

insert image description here

man command

Linux commands have many parameters, we can't remember all of them, we can get help by checking the online manual. The command to access the Linux man page is man

Syntax : man [options] command

Common options

-k search manpage by keyword

num is only found in chapter num

-a will display all the chapters, such as man printf, it will start searching from the first chapter by default, stop when you know it, use the a option, when you press q to exit, it will continue to search until all chapters are searched

The manual is divided into 8 chapters:

1 is the normal command

2 is a system call, such as open, write and the like (through this, at least you can easily find out what header files need to be added to call this function)

3 is a library function, such as printf, fread4 is a special file, that is, various device files under /dev

5 refers to the format of the file, such as passwd, which will explain the meaning of each field in this file

6 is reserved for the game, defined by each game itself

7 is the attachment and some variables, such as global variables like environ are explained here

8 is the command for system management, these commands can only be used by root, such as ifconfig

knowledge development

1. The man command needs to be installed before it can be used. The installation command is "yum install -y man-pages", and this command can only be installed by the root user or use sudo

2. If man does not specify a specific chapter, it will start searching from manual No. 1 by default until it is found. If a chapter is specified, it will search in the specified chapter

3. Since Linux is written in C language, man contains the manual of C language functions by default, specifically in manual No. 3. In addition, if we need to view other languages, such as C++/java/python, we need to install them in Linux now. You can also check directly on their official website

4. After we use the man command to enter the document, press the q key to exit the document

command demo

insert image description here

cp command

Syntax : cp [options] source file or directory target file or directory

Function : Copy files or directories

Explanation : The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, it will copy all the previously specified files or directories to this directory. . If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear

Common options

-f or --force Forcibly copy files or directories, regardless of whether the destination file or directory already exists

-i or --interactive ask user before overwriting files

-r Recursive processing, processing the files and subdirectories under the specified directory together. If the form of the source file or directory does not belong to a directory or a symbolic link, it will be treated as an ordinary file

-R or --recursive recursive processing, the files and subdirectories in the specified directory will be processed together

The cp command is used to copy files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing file or directory, it will copy all the previously specified files or directories to the directory. , if the final destination is not an existing file or directory, an error message will appear

command demo

Copy ordinary files with no duplicate names

insert image description here

Copy the file with the same name. When copying the file with the same name, if we do not want the operating system to confirm to us, we can use the -f option to force the copy

Copy the entire directory and all files under the directory

insert image description here

mv command

The mv command is the abbreviation of move, which can be used to move files or rename files (move (rename) fifiles). It is a commonly used command in the Linux system and is often used to back up files or directories.

Syntax : mv [options] source file or directory target file or directory

function :

1. Depending on the type of the second parameter in the mv command (whether it is a target file or a target directory), the mv command will rename the file or move it to a new one

in the directory.

2. When the second parameter type is a file, the mv command completes the file renaming. At this time, there can only be one source file (it can also be the source directory name), it

Renames the given source file or directory to the given destination filename.

3. When the second parameter is an existing directory name, there can be more than one source file or directory parameter, and the mv command will move the source files specified by each parameter to

in the target directory.

Common options

-f : force means that if the target file already exists, it will be overwritten without asking

-i : If the target file (destination) already exists, it will ask whether to overwrite

command demo

insert image description here

rename directory or file

insert image description here

cat command

Syntax : cat [options] [file]

Function : View the content of the target file, that is, display the content of the entire file to the terminal (screen) from top to bottom

Common options

-b number non-empty output lines

-n number all lines of output

-s Do not output multiple blank lines

There is also an instruction corresponding to cat – tac, tac will display the contents of the file to the terminal from bottom to top, but tac cannot display the line number

command demo

In order to facilitate the demonstration of instructions, we use a shell script to generate a simple text file with 10,000 lines, and the script instructions are as follows:

cnt=0; while [ $cnt -le 10000 ] ; do echo "hello linux [$cnt]"; let cnt++; done >test.txt

display without line numbers

insert image description here

Display with line numbers

insert image description here

more command

Syntax : more [options] [file]

Function : the more command, similar to cat, displays the content of the text on the terminal, but different from cat, more will display the content of the text file in full screen, when we press the space key (space) To go to the next page, when you press the b key, you will go back to the next page, and more also has the function of searching for strings. At the same time, since the more command reads files from front to back, the entire file is loaded at startup. document

Common options

-n defines the screen size as n lines

q exit more

+n start displaying from the nth line

/string, function to search down "string"

-c clears the screen from the top, then displays

-p paginate the file by clearing the window instead of scrolling, similar to -c

-u Remove underscores from file content

knowledge development

1. We can use /[target string] to find the string, but more can only find the string behind the text, not the string in front of the text

insert image description here

2. We can press the q key to exit and view

command demo

Specify the number of rows on the page to display

insert image description here

less instruction

The less tool is also a tool for displaying files or other output in pages. It should be said that it is an orthodox tool for viewing file content in Linux, and its functions are extremely powerful. The usage of less is more flexible than that of more. In more, we have no way to turn forward, we can only look backward, but if less is used, we can use [pageup][pagedown] and other key functions to look back and forth, which is easier to view Contents of a file! In addition, you can have more search functions in less, you can not only search downward, but also search upward

Syntax: less [parameters] file

function :

Less is similar to more, but you can browse files at will with less, while more can only move forward, but not backward, and less will not load the entire file before viewing, which is more efficient for displaying large files

Common options

-i ignore case when searching

-N display the line number of each line

/string: function to search down for "string"

?string: function to search upwards for "string"

n: repeat previous search (related to / or ?)

N: Repeat previous search in reverse (relative to / or ?)

q:quit

command demo

look forward

insert image description here

look up backwards

insert image description here

head directive

Head and tail are as easy to understand as their names. They are used to display a certain number of text blocks at the beginning or end. end

Syntax: head [parameters]... [file]...

Function : head is used to display the beginning of the file to the standard output, and the default head command prints the first 10 lines of the corresponding file.

Commonly used options : -n<number of lines> the number of lines displayed

command demo

insert image description here

tail command

The tail command writes the file to the standard output from the specified point. Use the -f option of the tail command to easily check the log file being changed. tail -f fifilename will display the last content in fifilename on the screen, and not only refresh , allowing you to see the latest file contents.

Syntax: tail[required parameters][option parameters][file]

Function: It is used to display the end content of the specified file. When no file is specified, it will be processed as input information. Commonly used to view log files.

Common options:

-f read in a loop

-n<number of lines> display the number of lines

command demo

insert image description here

knowledge development

1. The tail command writes the file to standard output from the specified point

2. Use the -f option of the tail command to easily check the log file being changed

3.tail -f filename will display the last content in filename on the screen, and it will automatically refresh so that you can see the latest file content

4. Two ways to display the data in the middle of the file:

reset

We can first use relocation> to report the data of the head command to a temporary file, and then use the tail command to take out the data at the end of the temporary file. These data are the intermediate data we need. We can take out 100 to 110 lines of data For example:

insert image description here

But we found that this method needs to copy data and generate temporary folders, which consumes a certain amount of time and space, so we use the pipeline method

pipeline

Our pipeline in linux is a means of transmitting data. It is divided into entry and exit. Both head and tail read data from the file and then output it to the terminal. We can use pipes for head and tail. connection, so the data read by the head from the file will flow directly into the pipeline, and then the tail will read the data from the pipeline and output it to the terminal

insert image description here

date directive

date display

date Display time in the specified format: date +%Y:%m:%d

date usage: date [OPTION]… [+FORMAT]

1. In terms of display, the user can set the format to be displayed. The format is set to a plus sign followed by several marks. The list of commonly used marks is as follows

%H : hours (00…23)

%M : minutes (00…59)

%S : seconds (00…61)

%X : Equivalent to %H:%M:%S

%d : day (01…31)

%m : month (01…12)

%Y : full year (0000…9999)

%F : Equivalent to %Y-%m-%d

2. In terms of setting time

date -s //Set the current time, only the root authority can be set, others can only be viewed.

date -s 20080523 //Set to 20080523, which will set the specific time to empty 00:00:00

date -s 01:01:01 //Set the specific time and will not change the date

date -s "01:01:01 2008-05-23″ //This can set all time

date -s "01:01:01 20080523″ //This can set all time

date -s “2008-05-23 01:01:01″ //This can set all time

date -s “20080523 01:01:01″ //This can set all time

3. Timestamp

Time -> Timestamp: date +%s

Timestamp -> time: date -d@timestamp value

Unix timestamp (English is Unix epoch, Unix time, POSIX time or Unix timestamp) is from January 1, 1970 (UTC/GMT

The number of seconds elapsed from the beginning of midnight), leap seconds are not considered. In addition, since our country is in the East Eighth District, the corresponding time at that time is 8:00:00 on January 1, 1970

command demo

1. Format to display the current time

insert image description here

Any form of separator can be added in the middle, but spaces cannot be added. Adding spaces will be considered as multiple instructions

2. Timestamp generation and conversion

insert image description here

cal command

The cal command can be used to display the Gregorian (Gregorian) calendar. The Gregorian calendar is the current international calendar, also known as the Gregorian calendar, commonly known as the Gregorian calendar. "Gregorian calendar" also known as "the sun"

The "Western calendar" is based on the fact that the earth revolves around the sun for one year, which is commonly used by Western countries, so it is also called "Western calendar".

Command format : cal [parameter][month][year]

Function : Used to view calendar and other time information, if there is only one parameter, it means the year (1-9999), if there are two parameters, it means the month and year

Common options :

-3 Display the calendar of the previous month, the current month, and the next month of the system

-j Displays the day of the year (the date of the year is counted by day, starting from January 1st, and the number of days in the current month in the year is displayed by default)

-y displays the calendar for the current year

command demo

insert image description here

insert image description here

find command

Syntax: fifind pathname -options

Function: used to find files in the file tree and make corresponding processing (may access disk)

Common options: -name Find files by filename

knowledge development

1. The fifind command under Linux searches for files in the directory structure and performs specified operations.

2. The fifind command under Linux provides quite a lot of search conditions, and the function is very powerful. Because fifind has powerful functions, it also has many options, most of which are worth our time to understand.

3. Even if the system contains a network file system (NFS), the fifind command is also valid in this file system, only you have the corresponding permissions.

4. When running a very resource-consuming find command, many people tend to put it in the background, because traversing a large file system may take a long time (here refers to the file system above 30G bytes ). When we do a find search, we may need to access the disk and cause inefficiency

command demo

insert image description here

Other commands related to find

1.which – for searching commands

insert image description here

2.alias -- Rename the command (alias)

insert image description here

It should be noted that we use alias to create aliases for other instructions is a temporary one, and the aliases will become invalid after we restart the system

insert image description here

3.whereis-Under a specific path, find files or instructions that closely match the specified file name

insert image description here

grep command

Detailed Explanation of Linux grep Command Line

Syntax: grep [options] search strings file

Function: Search for a string in a file and print out the found lines

Common options:

-i : Ignore the difference in case, so the case is treated as the same

-n : By the way, output the line number

-v: Reverse selection, that is, display the line without the content of the 'search string'

In order to facilitate our demonstration, we need to create two files

1.test.txt

cnt=0; while [ $cnt -le 10000 ] ; do echo "hello linux [$cnt]"; let cnt++; done >test.txt

We make the following changes

insert image description here

2.num.txt

insert image description here

command demo

1. Search for a string in a file

insert image description here

2. Ignore case search

insert image description here

3. Reverse filtering

insert image description here

Other commands related to grep

grep can cooperate with other commands and combine pipelines to achieve certain matching tasks

1.wc-total number of times

insert image description here

grep+wc-> count the number of rows that meet the filter conditions

insert image description here

2.sort to sort the text content

insert image description here

3. uniq – remove the repeated content adjacent to the text

insert image description here

sort+uniq+pipeline -> take out the repeated data in the text and sort

insert image description here

Note: grep, sort, and uniq filter the content of the text according to certain conditions and display it to the terminal, without changing the data in the text

zip command

Syntax: zip archive.zip directory or file

Function: Compress directories or files into zip format

Commonly used options: -r recursive processing, all files and subdirectories in the specified directory will be processed together

expand knowledge

1. Why do we pack and compress files

It is easy to transfer. There are many large and small files on the Internet. Packing all files into one file for transfer can avoid the loss of small files and achieve efficient transfer. For example, the class committee in college packs the files submitted by students

Easy to save, saving all files in one file is convenient for us to manage and save it

2. When zip packs and compresses a directory by default, it only packs and compresses the directory. When we use zip to pack and compress, we need to bring the -r option

command demo

1. Pack and compress directory files

insert image description here

2. Pack and compress ordinary files

insert image description here

unzip command

Syntax : unzip [compressed file/directory] [options]

Function : unpack and extract directory/file

Common options : -d unzip the file to the specified directory

expand knowledge

1.zip packs and compresses the file, unzip unpacks and decompresses the file

2. unzip decompresses the file/directory to the current path by default, and can be decompressed to the specified path by adding -d

command demo

1. Unzip to the current path

insert image description here

2. Unzip to the specified path

insert image description here

tar command

Syntax : tar [options] [file/directory]

Directory : Compress a directory or file into tgz mode

tar [-cxtzjvf] files and directories ... Parameters :

-c : Create a parameter command for a compressed file (meaning create);

-x : Unzip the parameter command of a compressed file!

-t : View the files inside the tarfifile! Do not open the compressed file, directly view the contents of the file in the compressed package

-z : Does it also have the gzip attribute? That is, does it need to be compressed with gzip?

-j: Does it also have the attributes of bzip2? That is, does it need to be compressed with bzip2?

-v : Show files during compression! This is commonly used, but not recommended for background execution! Synchronize compressed file list when decompressing/compressing

-f: Use the file name, please note that the file name must be followed immediately after f! Do not add parameters!

-C : Unzip to the specified directory

Knowledge expansion :

1. When we use the tar command, the -f option needs to be placed at the end, and the rest of the options can be swapped at will

2. tar will decompress the directory/file to the current path by default, and you need to add the -C option to decompress to other paths

3. The compressed file in tar format does not support decompression to view the file, just use the -t option

command demo

1. Unzip the file/directory

insert image description here

2. During the process of compressing files and displaying the file list

insert image description here

3. View files without decompression

insert image description here

4. Unzip the file/directory to the current path, and display the file during the decompression process

insert image description here

5. Unzip the file/directory to the specified path, and do not display the file during the decompression process

bc command

The bc command is an arbitrary precision calculator language. It is used as a calculator under Linux. We can do basic mathematical operations. Enter quit to exit bc

insert image description here

Combination of bc and pipeline

insert image description here

uname directive

Syntax : uname [options]

Function : uname is used to obtain information about computers and operating systems.

Supplementary note : uname can display basic information such as the version of the operating system used by the Linux host and the name of the hardware.

Common options:

-a or –all output all information in detail, followed by kernel name, host name, kernel version number, kernel version, hardware name, processor class

Type, hardware platform type, operating system name

-r can output some information, such as kernel version, hardware name, processor type (architecture/cpu framework)

command demo

insert image description here

other instructions

1.lscpu – view CPU information

insert image description here

2. lsmem – view content information

insert image description here

3.df -h – check disk status

insert image description here

4.who – view the current online users of linux

insert image description here

Several important hotkeys [Tab], [ctrl]-c, [ctrl]-d

1. [Tab] button—has the functions of "command completion" and "file completion". When we enter a part of a command, and then press [tab] once, and some need to be pressed twice, Linux will automatically This command completes

insert image description here

2. [Ctrl]-c button—let the current program "stop" to terminate the abnormal program in the foreground

Generally used in two aspects, one is to terminate a program, such as an infinite loop program

insert image description here

insert image description here

The second is to abolish the current command line

insert image description here

3. [ctrl] -d key – exit the current user (exit layer), can replace exit

insert image description here

4.[ctrl] -r–search history command, we input ctrl+r, and then enter the keyword system will pop up the corresponding history command, we can press <–> to select

insert image description here

shutdown

Syntax : shutdown [options]

common options

-h : Shut down immediately after stopping the system's services

-r: Restart after stopping the system service

-t sec : Add seconds after -t, which means "shut down after a few seconds"

We need to note that the virtual machine needs to be shut down, but the cloud server does not need to be shut down

3. Extended instructions

Installation and login commands : login, shutdown, halt, reboot, install, mount, umount, chsh, exit, last;

File processing commands : file, mkdir, grep, dd, fifind, mv, ls, diffff, cat, ln;

System management related commands : df, top, free, quota, at, lp, adduser, groupadd, kill, crontab;

Network operation commands : ifconfig, ip, ping, netstat, telnet, ftp, route, rlogin, rcp, fifinger, mailnslookup;

System security related commands : passwd, su, umask, chgrp, chmod, chown, chattr, sudo ps, who;

Other commands : tar, unzip, gunzip, unarj, mtools, man, unendcode, uudecode.

Guess you like

Origin blog.csdn.net/qq_67582098/article/details/130246925