[Linux] Detailed explanation of common basic instructions

1. What is Linux

For students who don't know Linux, they may not understand what Linux is at all. Next, I will briefly introduce Linux. You may not know much about operating systems, so you should have heard of the Windows system in our commonly used computers. Windows systems and Linux systems are both operating systems .

Linux is a free and open source UNIX-like operating system. The kernel of the operating system was first released by Linus Torvalds in 1991. Later, after adding user space applications, it became the Linux operating system system. Strictly speaking, Linux is just the operating system kernel itself, but the Linux kernel is usually used to express this meaning.

We only need to know that Linux and Windows are both operating systems. They are both software for software and hardware resource management. We will continue to explore more knowledge in the future.

2. Common commands in Linux

1. ls command

Syntax: ls [options][directory or file]
Function: For a directory, this command lists all subdirectories and files in the directory. For files, the file name is listed along with other information.

First of all, we need to know that a file is divided into content and attributes . The operation of the file is nothing more than the operation of the content and the operation of the attributes, and our ls command is an operation to view the supporting attributes of the file.

Common options for the ls command are as follows:

  • -a: List all files in the directory, including hidden files starting with .; in Linux, files starting with . are called hidden files. Use: ls -a.
  • -l: List file details. Among them, ls -l can be abbreviated as ll. The options can be used in combination, such as ls -al.
  • -i Outputs the index information of the i node of the file. For example, ls –ai specifies the file.
  • -r sorts the directory in reverse order.
  • -t Sort by time.

The following demonstrates two commonly used command options. Suppose I create a test directory and three files: test.cpp, test.c, test.txt; use the ls command:

Insert image description here
Use the ls -l command, which is ll:

Insert image description here
Everything in front of the file name or directory is the corresponding attribute.

2. pwd command

Syntax: pwd
function: Display the directory where the user is currently located

Use as follows:

Insert image description here

3. cd command

Syntax: cd directory name
Function: Change the working directory. Change the current working directory to the specified directory.

(1) When we execute ls -la, we can see two hidden files:

Insert image description here
So what do .and ..represent respectively?

In fact ., it represents the current directory ..and the upper-level directory, so cd .it can be understood as entering the current directory, that is, it is still in the current directory; and cd ..means going back to the upper-level directory; and here, assuming that we need to enter the test directory, we only need to cd test.

(2) When we perform pwd, we observe that there are many /symbols that separate different directories, as shown in the figure below, which we call path separators .

Insert image description here

So we can find a file by using the path separator . In the Linux system, the files and directories on the disk are composed of a directory tree, and each node is a directory or file. Take our above directories and files as an example, as shown below:

Insert image description here
Among them, /is the root directory. Why do we use paths to find files? Because there must be many files in the root directory, and to find files, path identification is the only way to identify files.

How to identify the path of a file under Linux:

  • relative path
  • absolute path

Relative path: Suppose we need to find the test directory now, and we are currently in the code1 directory. Relative to the code1 directory, we need to identify the path of test as: ../Blog/practice1/test, that is, first we need to go back to the lmy directory, and then enter the Blog directory. , then enter the practice1 directory, and finally mark test under practice1 . Example demo:

Currently in the code1 directory:
Insert image description here
We follow the relative path after cd to enter the test directory:
Insert image description here

Absolute path : Suppose we also need to identify the test directory. The absolute path means that we start from the root directory, and the path is: /home/lmy/Blog/practice1/test.

Example demo:

I'm currently in the / root directory:

Insert image description here
Follow cd with the absolute path to enter the test directory:

Insert image description here

(3) When we write code in Linux, we usually generate an executable program. The executable program is the program that needs to run the code. For example, in the figure below, a.out is an executable program:

Insert image description here

When we need to run this executable program, the command to run is: ./a.out, for example, as shown below:

Insert image description here
To run an executable program, it needs to be loaded into memory, and before that, we need to find it first, and in ./a.outis /the path separator in Linux. We know that .it means the current directory, and ./the meaning is in Executable programs are found in the current directory.

Other commands for cd :

  • cd ~: Enter the user’s home directory
  • cd - : return to the most recently visited directory

4. touch command

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

Our commonly used touch command is to create a file. For example, we want to create a mytest.c file:

Insert image description here

5. mkdir command

Syntax: mkdir [option] dirname...
Function: Create a directory named "dirname" in the current directory

The mkdir instruction is an instruction to create a directory. For example, I want to create a mytest directory:

Insert image description here
As shown above, the directory is created.

Suppose we need to create multiple directories recursively, that is, create a dir1 directory, create a dir2 directory in dir1 , and create a dir3 directory in dir2 . Here we need to add the -p option and the path to create the directory; here we need to introduce a command tree , which displays the specified directory in a tree structure, for example:

Create a recursive directory:

Insert image description here
Show recursive directories:

Insert image description here
As shown in the figure, multiple directories are created recursively.

6. rmdir command && rm command

rmdir is the opposite of mkdir . rmdir is an instruction to delete an empty directory.

However, we use the rm command more often for deletion in our daily life. The rm command can delete files or directories at the same time.

When we need to use the rm command to delete a directory, we need to bring the -r option. Commonly used options are as follows:

  • -f Even if the file attribute is read-only (i.e. write-protected), it will be deleted directly; that is, forced deletion, and no confirmation will be asked one by one before deletion.
  • -i Ask for confirmation one by one before deleting
  • -r deletes the directory and all files under it; it means recursive deletion

For example, we created multiple recursive directories dir1 above . We need to delete dir1 as follows:

Insert image description here

As shown in the figure, dir1 has been deleted. Note that the option can be placed at the end or after rm .

If we need to delete a file test.cpp , just use rm test.cppthe command directly.

In addition, if we want to delete all files and directories in the current directory, the command to execute is rm * -rf, * means all, and -rf means recursion and force.

7. man command

Linux commands have many parameters, and it is impossible for us to remember them all. We can get help by checking the online manual. The command to access the Linux man page is: man

Syntax: man [options] command

When we use it, just follow man with the command we need to find.

8. cp command

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

Description: The cp command is used to copy files or directories. If two or more 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 copies a file or directory, regardless of whether the destination file or directory already exists.
  • -r recursively processes files and subdirectories in the specified directory together. If the form of the source file or directory does not belong to a directory or symbolic link, it will be treated as an ordinary file.

For example, under our current path, there are two directories and one file:

Insert image description here

There are three files under mytest and test respectively, and their tree structure is as follows:

Insert image description here

Insert image description here

We first copy the mytest.c file to the mytest directory and use cp mytest.c mytestthe command:

Insert image description here

You can see that the copy has been successful. Then we copy the test directory and all the files in it to the mytest directory, using cp -rf test mytestthe command:

Insert image description here
As shown in the tree structure above, the directory test is also copied to the mytest directory.

9. mv command

The mv command is the abbreviation of move. It can be used to move files or rename files. It is a commonly used command in Linux systems 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 whether the second parameter type in the mv command is a file or a directory, the mv command will rename the file (file) or move it to a new directory (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 will rename the given source file or directory to the given one. Target file name.

For example, there are two directories and one file in my current path:

Insert image description here

We use the command mv mytest.c mytest.txtto observe what happens:

Insert image description here
We can observe that mytest.c is renamed to mytest.txt;

Note that when we use the mv command, we mv src dstassume that src is the source file/directory and dst is the target file/directory. src must exist no matter what; when dst is a file, dst must not exist. If it exists, src will overwrite dst. , causing dst to be lost.

  1. When the second parameter type is a directory , there can be multiple source file or directory parameters. The mv command moves all the source files specified by each parameter to the target directory. The function here is to cut .

For example, there are two directories and one file in my current path:

Insert image description here

We use the command mv test Testto observe what happens. Note that Test is a directory that does not exist:

Insert image description here

Note that this is not a renaming function, but to cut the test directory to the Test directory. Because the Test directory does not exist, the system will create one by itself and cut all directories or files under test into Test . .

We execute mv Test mytestthe command to cut Test into mytest:

Insert image description here
We can see through the tree structure of mytest that Test has been cut into mytest.

10. cat command

Syntax: cat [option][file]
Function: View the contents of the target file

Common options:

  • -b output line numbers for non-empty lines
  • -n numbers all lines of output
  • -s does not output multiple blank lines

Here, we introduce several instructions echo, >, >>, <;

Among them, echo is to write a string to the display, for example:

Insert image description here

And >, >>respectively are output redirection and append redirection ;

> : Output redirection, the content that should be written to the display file is rewritten to the specified file; for example, we write a string to a file:

Insert image description here
Here we can use the command cat learned above . Cat is to view the content of the target file. Use it as above;

Note that if you use input redirection, if you write to a file that does not exist, the system will automatically create the specified file; and input redirection must clear the contents of the original file each time before writing to the file, because it defaults to Open the file for writing only.

>> : Append redirection. The difference between append redirection and output redirection is that append redirection does not clear the contents of the original file before each file is written, because it is written in an append mode. Use as follows:

Insert image description here

< : Input redirection. Data should be read from the keyboard file instead of reading the corresponding data from the specified file, as follows:

Insert image description here
Regarding input redirection, output redirection and append redirection, we only have a preliminary understanding here, and we will introduce more learning later.

11. more command

The function is similar to cat , but when our file has a lot of content, it only displays a full page of content at a time. Press Enter to display the remaining unfinished content line by line. Press q to exit.

Since the function of our next command is similar to it, but it is easier to use and more commonly used than it, so the more command will not be introduced in detail. Let us introduce the next command in detail.

12. less command

Syntax: less [parameter] File
function: less is similar to more, but you can use less to browse files at will, while more can only move down, but not up, and less can move up and down when viewing.

Options:

  • -i ignore case when searching
  • -N displays the line number of each line
  • /string: function to drill down for "string"
  • q:quit
  1. The less tool is also a tool for paging display of files or other output. It should be said that it is an orthodox Linux tool for viewing file contents, and it is extremely powerful.
  2. 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.
  3. But if you use less, you can use the up and down keys to flip back and forth through files, making it easier to view the contents of a file.
  4. In addition, you can have more search functions in less, not only you can search down, but you can also search up.

For example, we use a method to directly write 1000 "hello world" to a test.txt file. This method is entered directly on the command line cnt=0; while [ $cnt -le 1000 ]; do echo "hello world, hello $cnt"; let cnt++; done > test.txt, such as the following figure:

Insert image description here

We only need to execute the cat command to view 1000 "hello world" , and we execute the command less -N test.txt:

Insert image description here

The current screen can only display 25 data. We can press the up and down keys to browse freely up and down. Press q to exit viewing.

13. wc command and uniq command

Here we introduce two instructions wc, wcuniq , which can check how many lines a file has and how big it is. Use it as follows:

Insert image description here

Among them, -lthe option is to view the number of rows.

uniq is to deduplicate the duplicate content in the file. For example, there is a test.c file with the following content:

Insert image description here

We deduplicate it and add option -c to display line numbers:

Insert image description here

14. head command

Syntax: head [parameter]... [file]...
Function: head is used to display the beginning of the file to the standard output. The default head command prints the first 10 lines of the corresponding file.

Options:

  • -Number of rows displayed

For example, in the 1000 "hello world" written above, we execute head -20 test.txtthe command to display the 20 lines from the beginning:

Insert image description here

15. tail command

Syntax: tail [required parameters] [selected parameters] [file]
Function: used to display the content at the end of the specified file. If the number of lines is not specified, the last 10 lines will be displayed by default; when the file is not specified, it will be processed as input information. Commonly used to view log files.

Options:

  • -f loop reading
  • -The number of rows shows the number of rows

For example, continue to use the above file as an example and view the last 20 lines:

Insert image description here

Now we can look at the head and tail, but what if we want to look at the middle?

Here we introduce another knowledge point: pipeline, the symbol of pipeline is |.

For example, we need to view lines 600~609. Our idea is to first use head to view line 609. Then we use the output of the previous head as the input of the pipeline, and then output the last 10 lines of line 609. For example, the following command:

Insert image description here

Let’s learn another command tac here. tac is the opposite of cat. tac checks the contents of the specified file backwards. We use the command we learned earlier to complete a command as follows:

Insert image description here

We used pipelines to complete a "pipeline". The above instructions are to extract the first 610 lines of data from the test.txt file being viewed, then extract the last 10 lines of data, and finally print it backwards.

16. Time-related commands

You can check the time directly by using date, such as:

Insert image description here
We date +%scan use the command to view the timestamp, such as:

Insert image description here

The Unix timestamp (Unix epoch, Unix time, POSIX time or Unix timestamp in English) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), regardless of leap seconds.

17. cal command

The cal command can be used to display the Gregorian (Solar) calendar. The Gregorian calendar is the calendar currently used internationally, also known as the Gregorian calendar and commonly known as the Gregorian calendar.

Syntax: cal [parameter][month][year]
Function: used to view time information such as calendar. If there is only one parameter, it represents the year (1-9999). If there are two parameters, it represents the month and year.

For example, use the command cal directly:

Insert image description here
Use the command to view the calendar for 2023:

Insert image description here

18. find command

Syntax: find -options pathname
Function: Used to find files in the file tree and process them accordingly (possibly accessing the disk)

Common options:

  • -name searches for files by file name

For example, there is a directory Test in mytest under my current path , and there is a file6 file in the Test directory , as shown below:

Insert image description here

Let's say I need to find it but don't know where it is, we can use instructions find -name file6like:

Insert image description here
So we can find where it is.

19. grep command

Syntax: grep - option 'Search for string' File
function: Search for a string in the file and print out the found lines

Common options:

  • -i: Ignore differences in case and treat all cases as the same
  • -n: output line number
  • -v: reverse selection, that is, display the line without the 'search string' content

Suppose we find the line numbered 88 in the file above that printed 1000 "hello world" and display the line number. We can execute the grep -n "88" test.txtcommand, for example:

Insert image description here

As shown in the picture above, the green number in the front is the line number, and the red number in the back is the string we need to find.

20. zip/unzip command

Syntax: zip compressed file.zip directory or file
function: compress the directory or file into zip format

Common options:

  • -r recursively processes all files and subdirectories in the specified directory.

(1) For example, there are two directories under my current path, one is an empty directory, and the other has a directory and files. Their tree structure diagram is as follows:

Insert image description here

Insert image description here

Now I need to package and compress all the files and directories in the mytest directory into temp . First, we package and compress in the mytest directory:

Insert image description here
As shown above, a test.zip file is formed , and then we move this file to the external temp directory for decompression:

Insert image description here
Then we observe the tree structure in the current directory:

Insert image description here
We can observe that Test was originally a directory, and the files in it were not packaged and compressed, so something went wrong when we packaged and compressed it.

The command we execute when packaging and compressing zip test.zip ./*is: The meaning is to package and compress all the files and directories in the current directory into a test.zip file, but the files in the Test directory are not packaged and compressed, so we are packaging When compressing, add the -r option, which means recursive processing, processing all files and subdirectories in the specified directory together, so the correct command should be, for example, as shown below zip -r test.zip ./*:

Packaging and compression completed:

Insert image description here

Move the .zip file and extract it:

Insert image description here

Finally, observe whether everything has been packaged:

Insert image description here
We can see that the files in Test are also packaged, so this packaging and compression is correct.

(2) During the above packaging and compression, after we complete the packaging and compression, we need to move the .zip file to the specified directory and then decompress it. Can we directly decompress the .zip file to the specified directory? The answer is yes. For example, if we continue to follow the above example, we have already packaged and compressed a .zip file, as shown in the figure:

Insert image description here

We will directly extract the test.zip file to the temp directory of the upper-level directory and execute the command unzip test.zip -d ../temp, where -d is the specified path, as shown below:

Insert image description here
Then we go back to the temp directory and observe:

Insert image description here

We can see that it is no problem to specify the decompression path in this way.

21. tar command

tar command: pack/unpack, don’t open it, just read the content .

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

Options:

  • -c: Parameter command to create a compressed file (meaning create)
  • -z: Whether it also has the attributes of gzip, that is, whether to compress
  • -f: Use the file name. Note that the file name must be followed immediately after f! No more options!
  • -x : Unzip the parameter command of a compressed file
  • -C : Unzip to the specified directory
  • -v : Show files during compression! This is commonly used, but it is not recommended to be used in background execution processes.

For example, if we continue to use the above files and directories as an example, we will pack and compress all the files and directories in the mytesttar -czf test.tgz ./* directory. The command is , where the meaning of czf is the meaning of the above options, and the formed file name will be suffixed with .tgz , . / * are all files and directories under the current path;

Example demo:

Insert image description here

Then we extract it to the temp directory in the upper-level directory. If we do not specify a directory, it will be extracted to the current directory by default. For example, demo:

Insert image description here

As shown in the picture above, we also used the tar command to complete the process of packaging, compression, decompression and unpacking.

Finally, let’s briefly introduce the difference between zip/unzip and tar. The difference lies in the compression algorithms. Different compression algorithms are used in different scenarios, but for us, we can use either one.

22. bc command

The bc command is actually a calculator on Linux. It is generally used in combination with echo and pipes. It is used as follows:

Insert image description here

23. uname -r command

Syntax: uname [option]
Function: uname is used to obtain information about the computer and operating system.

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

24. Several important hotkeys

  • [Tab] button—has the functions of "command completion" and "file completion"
  • [Ctrl]+c key - "Stop" the current program
  • [Ctrl]+d key - usually means: "end of keyboard input"; in addition, it can also be used to replace exit. Generally, you can use [Ctrl]+d to log out.

After learning this, we have almost learned the basic instructions, which is enough for everyone to get started with. We will also introduce more instructions in the future study~ If you find it helpful, please give it a thumbs up~

Guess you like

Origin blog.csdn.net/YoungMLet/article/details/131886563