Ten thousand words to explain the common commands of Linux

Table of contents

Foreword:

1. Linux interface problems

2. What is an operating system

3. Why learn Linux basic instructions

Four, Linux basic instructions

pwd command

ls command

Meet ls -a:

Meet ls -d:

understand the file

cd command

4. touch command

5. mkdir command

6. rmdir command and rm command

7.man command

8. cp command

9. mv command

10.more command

11.less instruction

12.head and tail instructions

Combination of head and tail

13. date command

Timestamp:

14.cal command

15. find command

16. grep command

17.uname command

18. Several important hotkeys


Foreword:

In addition to talking about some commonly used instructions in this article, there are many more important knowledge supplements than instruction operations. Knowing more will broaden the horizons of programmers.

1. Linux interface problems

Linux actually has an interface, including the command line we use is one of the forms:

But there is also a graphical interface for laymen to use. We use the command line to simulate an enterprise environment . If you want to marry your cloud server to others, you can also reprint the virtual machine on the Linux system.

Tip: XShell can use alt+enter to enter full screen

2. What is an operating system

Several configurations are essential for one of our computers to be used: application software, operating system, device driver, and hardware. Here we come to a brief understanding. We usually use computers to scan Douyin and the like. Our computers cannot directly call hardware through software (you need to use a display screen, keyboard, mouse, etc. to use Douyin). The brief said: He needs to use the operating system , Then call the device driver through the operating system, and the device driver calls the hardware.

Many students don’t know what a driver is. In fact, we have experienced it before. When the mouse is plugged into the computer, there will be a pop-up window: the mouse driver has been installed. This is our most intuitive feeling about the driver at present.

To give an example: In a company, you are a new employee (hardware), the company will assign you a mentor (device driver), the board of directors issued an order, and this order was passed from the middle leadership (operating system) to your mentor That, and then the mentor asks you to perform the task.

The role of the operating system here is to connect the past and the future . The user performs corresponding operations through the software, and then the operating system needs to call the underlying hardware to perform the corresponding operations. But the operating system does not know how to use the keyboard, mouse, etc., so special talents such as device drivers are needed to specifically operate our hardware.

The role of the operating system:

Manage hardware resources well (this is the means)

To provide a good, stable, safe and efficient operating environment (this is the purpose)

In the future, I will describe the relationship between hardware and software in more detail in the process section.

3. Why learn Linux basic instructions

What is the write command doing? In fact, it is equivalent to a series of operations we perform on the Windows graphical interface, such as double-clicking to open a folder and create a folder. We can also perform such operations through corresponding commands on the command line. In fact, whether it is the command line or the graphical interface, it looks the same to the operating system. In fact, Windows also comes with its own command line operation.

Students think about it, did you invent the keyboard or the mouse first?

At that time, we could only operate through the command line of the keyboard, and the mouse was produced because of the emergence of a graphical interface to cater to the majority of customers. This actually shows that the graphical interface and the command line are actually a relationship that leads to the same goal through different routes.

But what we are learning is the command line under the Linux system.

Why learn the basic commands of the command line? Because there is generally no need for a graphical interface on the server, we use the command line as a means of learning Linux and operating systems, as well as the ability to work in the enterprise in the future.

Four, Linux basic instructions

pwd command

Function: Display the directory where the current user is located.

Example:

In fact, this is equivalent to displaying the path of the current directory, which is the same in windows:

path:

The path form under Windows: D:\XXX\YYY\ZZZZ (the path separator is \)

The path form under Linux: /home/lzf/test2 (the path separator is /)

ls command

Function: Display the name of the file or directory under the current path

Example: This will display all the files in our current directory.

But there is only the file name here, what if we want to see the detailed information of the file, just like Windows:

We can add "options" to the command, such as -l to display detailed information about the file. The specific operation is to separate a space from the command and add options.

There are other options here, but the most commonly used is -l

-a List all files in the directory, including hidden files starting with .
-d Display directories as files instead of the files under them. For example: ls -d specifies the directory
-i outputs the index information of the i node of the file. For example, ls -ai specifies the file
-k to express the size of the file in the form of k bytes. ls –alk specifies the file
-l lists the detailed information of the file.
-n Use numeric UID, GID instead of name. (Introduce UID, GID)
-F Append a character after each file name to describe the type of the file, "*" means an executable ordinary file; "/" means a directory; "@"
means |" means FIFOs; "=" means sockets. (directory type identification)
-r Reverse sorting on directories.
-t Sort by time.
-s Output the size of the file after the l filename. (Sort by size, how to find the largest file in the directory)
-R List the files in all subdirectories. (recursively)
-1 Output only one file per line.

Meet ls -a:

We normally display the current path file of the folder like this

But when we add an option -a, we will find a few more files:

Here is a small detail: The files starting with '.' in Linux are hidden files , which cannot be displayed only with -l, but if you use the -a option, all files in the current path will be displayed , including Hidden files, this a option can be understood as all.

So what are .files and ..files, and why do they use such strange names.

.file refers to the current directory, and ..file refers to the parent directory.

Let's display the current path:

We are now in the linux directory, how to go back to the previous directory?

The operation of cd+path is to enter the target path. (Let’s talk a little bit for experiments first, and I will talk about it in detail later)

We can enter the parent directory through cd+..:

We found that we entered the lzf directory from the linux directory.

Meet ls -d:

After ls, you can directly connect to a file, which means that only the information of the file is displayed, but if you follow it with a directory, Linux will open the directory and display it:

It is equivalent to displaying its internal files, but the -d option will not open the directory :

Only information about the directory itself is displayed.

Files are also a very important concept for us to learn the operating system:

understand the file

a. If we create an empty file, will this file take up space on disk?
The answer is yes: because although the content of a file is empty, is there only memory occupying space in your file and disk? No, you also need to store the name, size, modification date, etc. of the file. within the file size.

So we come to a conclusion: file = file content data + file attribute data (also known as metadata).

cd command

Function : change your current path

Example : The simplest is cd+ plus path

Can a directory be stored under a Linux directory? The answer is yes, which leads to the fact that the file storage form of Linux is stored in a tree form.

Just like the picture above, in such a tree structure, there are leaf nodes and road nodes. Leaf nodes can be ordinary files or directories, but the files on the path must be directories.

We can also use the command tree+"path" to view files in a tree form

If you have not installed it, enter: yum install -y tree

In this way, the installation is complete, because my ing is not a root account, you need to use sudo to upgrade your permissions (the permissions will be discussed later)

Why do we like to use a path to represent a file? Because in the tree structure, the path is unique. For example, if we want to enter a file: hello_world, we need

: cd /home/lzf/linux/hello_world, how to operate like this instead of directly using the file name? Because we all know that the same file name cannot appear in a folder, but it is possible in different directories. So we can't simply identify it by the file name, but use the path. The above form of /home/lzf/linux/hello_world starting from the root directory is called an absolute path. As we mentioned before, the . and .. files are convenient to use the relative path opposite to the absolute path. Relative paths are simpler and generally use relative paths.

When we use the cd operation many times, we will find that cd .. cannot be cd to a certain directory

As above, at most it is under the / directory. This directory is the root directory. It can be understood in combination with the above figure, and it plays the role of the root node. So what is the home directory for? It is used to store all users of the server, such as Zhang San, Li Si, and Wang Mazi. This is actually consistent with Windows, which also allows multiple users to share an operating system . Home is the directory of different users, which is used to store temporary files of different users. A path like /home/lzf is the home directory. The default path where a specific user is logged in.

This is the relevant operation of cd:

cd .. : Return to the parent directory
cd /home/litao/linux/ : Absolute path
cd ../day02/ : Relative path
cd ~: Enter the user’s home directory
cd -: Return to the most recently accessed directory

4. touch command

Syntax : touch [options] filename

Function : create a file

This command also has a small function to modify the time.

Using ls -l, we will find that there is a string of time in the detailed information, this time is the file creation time, re-touching an existing file will update the file time.

Common options:

-a or --time=atime or --time=access or --time=use only changes 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.

5. mkdir command

Syntax : mkdir [options] directory name

Function : Create a directory under the current directory.

Example : mkdir dirmane

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

6. rmdir command and rm command

rmdir can also be seen from the name, remove dir is a command relative to the mkdir command. Delete a directory, but the limitation of this command is that it cannot delete a directory with files in it.

What if we want to delete a directory with files?

rm deletes normal files by default.

The -r option means recursive deletion. Why is it called recursive deletion? The reason for recursive deletion is that files are stored in the form of a tree. To delete a directory, you must first delete the files at the next level. To delete a file at the next level, you must first delete the files at the next level, and so on. .

Therefore, the combination of rm -r + directory name is specially used to delete the directory with files.

Let's look at another phenomenon. When we create a file with superuser privileges, we have to ask when we delete it.

What if we want to delete multiple files at once. For example: rm root1.txt root2.txt root3.txt The result is that you will be asked one by one to confirm the deletion.

It will be very inconvenient. So we can use such an option: -f, which means force.

The function is: delete files directly without asking.

Why is it set up like this? Take a look at the following line of command: rm -rf /, -rf is the abbreviation of -r -f combined. Do not try this line of command, it will directly delete your root directory, which is equivalent to deleting your entire system.

So you know why I want to ask you why I deleted the file set by root, just to prevent you from making mistakes .

In the future, you need to be cautious in the company, and don't accidentally "delete the database and run away". If you accidentally delete it during your internship, you should first report it and see if there is any way to restore it. If you cause losses, don’t be afraid. You are the first person responsible but you should not take full responsibility. If you think about what company would allow an intern to have such high authority, this is actually a failure of their risk assessment.

-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

7.man command

Syntax : man + [options] + A syntax

Function : Find the detailed information of A syntax in the online manual

There are many instructions in Linux, and some instructions were normal in the past, so the system provides us with an online grammar manual. However, man can not only search for instructions, but also for system interfaces and C language interfaces.

man ls

This is the search result of ls. Try C language: man printf

We can even view man's: man man

In fact, here we can also see the catalog of man (like the catalog on the book)

1 stores the command line, 2 stores the system instructions, 3 stores the C language library, etc.

Man+[option] mentioned above, we can fill in the man directory in the place of this option, so that man will directly search in this directory,

After entering man, stand-alone q exits man, you can't use the mouse to turn the page, you can only use the up and down keys to turn the page. If you don't fill it in, you will start from the beginning and keep looking down until you find it.

-k Search the online help num according to the keyword
to find only chapter num
-a Display all chapters, such as man printf, it starts searching from the first chapter by default, stop when you know it, use a option, when you
press Exit, he will continue to search back until all chapters are searched.
Directory:
1 is a common 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
is a library function, such as printf, fread4 It is a special file, that is, various device files under /dev
5 It refers to the format of the file, such as passwd, it will explain the meaning of each field in this file
6 It is reserved for the game and is defined by each game itself
7 Is it an attachment or There are some variables, such as global variables such as environ, which are explained here.
8 are commands for system management. These commands can only be used by root, such as ifconfig

Many students are actually wondering why the library of c is saved, and why there is only c. Is it because they look down on other languages? Don't be surprised that you can even type the syntax of c directly on the command line and use:

Because Linux is written in C plus a small amount of assembly language. The background details of Linux can be seen at:

Linux background and development history

Some students may not be able to use man, it is because there is no installation, type: yum install -y man-pages

8. cp command

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

Function: 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,

then it will copy all the files or directories specified earlier into this directory. If multiple files or directories are specified at the same time, and the final destination is not a saved

In the directory, an error message will appear.

Example:

This will copy the set txt file to the test2 directory. This is actually equivalent to copying and pasting files under windows.

What happens if we execute the above command again?

I added a -i to ask whether to confirm the meaning of the search. We can see his inquiry and ask us whether to overwrite the corresponding file.

Then we come to a conclusion: if the source file is consistent with the target file, the target file will be directly overwritten.

Another option is -r, which means recursive copy, and copy the subfiles in your source directory. Because recursive copy is required, this option is needed to determine.

options:

-f or --force forcibly copy files or directories, regardless of whether the destination file or directory already exists
-i or --interactive ask the user before overwriting files
-r recursive processing, and process the files and subdirectories in the specified directory together. If the form of the source file or directory does not belong
to , it will be treated as an ordinary file
. -R or --recursive recursive processing, and the files and subdirectories in the specified directory will be processed together

9. mv command

Similar to cp, cp is copy and paste, mv is similar to windows cut. Copy the file and delete the source file

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

Function:

a. 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.

b. 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.

c. When the second parameter is the name of an existing directory, 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.

instruction:

-f: force means to force, if the target file already exists, it will not ask but directly overwrite
-i: if the target file (destination) already exists, it will ask whether to overwrite!

Why is there no -r command here, because the operation of mv is not practical for recursion.

10. cat command:

Syntax: cat [command] filename

Function: view the content of the target file

options:

-b number non-empty output lines
-n number all output lines
-s do not output multiple empty lines

Example: cat test.txt, test.txt is a file I wrote, only added line numbers in the last ten lines.

We can see that when cat views this, it jumps directly to the bottom of the file. Because it is too long, it can only display the bottom part, but we can still see the above content with the mouse. This is the limitation of cat, which is not suitable for reading large files .

10.more command

Syntax: more [directive] file

Function: view file content

Common options:

-n number all lines of output
q exit more

11.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. It has extremely powerful functions.

its 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 back

But if you use less, you can use the functions of [pageup][pagedown] and other buttons to browse files back and forth, which is easier to use

To view the 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 is before viewing

The entire file will not be loaded.

options:

-i ignore case when searching
-N show line number for each line
/ string: function to search down "string
"
? or ? related)
N: Repeat previous search in reverse (related to / or ?)
q:quit

12.head and tail instructions

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, and head is used to display files

The beginning goes to standard output, and tail takes it for granted that it looks at the end of the file.

Syntax: head (tail) [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. tail is to display the contents of the last ten lines.

options:

-n display the number of lines of content

Example:

Combination of head and tail

What should I do if I want to get the last three lines of the first ten lines of the file

head test.txt | tail -3

Here is a reference to a symbol - |, the pipe character, its function is to splice many instructions together to process tasks. Just like above, two instructions appear on the same line at the same time. The underlying logic of this symbol is to throw the file it acquires into the pipeline with head, of course only ten lines are acquired, and then the tail command directly extracts the last three lines in the pipeline and prints them out.

Here's a longer example:

head -20 test.txt |tail -10|tac |head -3

This is actually a bit similar to the assembly line in life, entering from the left and exiting from the right. The role of tac is to print out the contents of the file anyway.

13. date command

Syntax: date

Function: Display time.

But we can specify the time output format: date +%Y/%m/%d

The + sign cannot be omitted

Timestamp:

A Unix timestamp (Unix epoch, Unix time, POSIX time, or Unix timestamp in English) is the number of seconds since January 1, 1970 (midnight UTC/GMT), not taking leap seconds into account.

The role of timestamp For example, if I write a program, to calculate the operation time of the program from opening to ending, it is only necessary to subtract the timestamps of the two time points. The second usage is to use it when creating random numbers.

The method of displaying the timestamp: date +%s

14.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 Display the number of days in the year (the date of a year is calculated by day, starting from January 1, and the current month is displayed in a year by default number of days)
-y display the calendar for the current year

The effect is like a strong wall calendar

15. find command

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

The fifind command under Linux provides quite a few search conditions and is very powerful. Because fifind is so powerful, so are its options

Many, and most of these options are worth our time to explore.

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

When running a very resource-intensive fifind command, many people tend to put it in the background, because traversing a large file system

The system may take a long time (here refers to the file system above 30G bytes).

16. grep command

Syntax: grep [options] String filename to look for

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'

Here we find out the content of the line with 97

17.uname command

Syntax: uname [options]

Function: Get the information about the computer and the operating system

options:

-a or –all output all information in detail, followed by kernel name, host name, kernel version number, kernel version, hardware name, processor
type hardware platform type, operating system name

18. Several important hotkeys

[Tab] key --- has the function of "command completion" and "file completion"
[Ctrl]-c key --- makes the current program "stop"
[Ctrl]-d key --- usually represents : Means "keyboard input end (End Of File, EOF or End OfInput)"; in addition, it can also
be used to replace exit

Guess you like

Origin blog.csdn.net/qq_64484137/article/details/128687813