Linux——2Linux basic commands

Table of contents

2.1 Linux directory structure

2.2 Getting started with Linux commands

ls command

Home directory and working directory

2.3 Directory switching related commands

cd changes working directory

pwd View current working directory

2.4 Relative paths, absolute paths and special path characters

Relative paths and absolute paths

special path character

2.5 Create directory command mkdir

2.6 File operation commands

touch, cat, more commands

cp, mv, rm commands

2.7 Search command

which

find - Find files by file name

find command - wildcard

find command - Find files by file size

2.8 grep, wc, and pipe characters

grep

wc command to do quantity statistics

pipe character |

2.9 Redirection characters of echo and tail

echo command

backtick'' 

Redirector

tail command

2.10 vim editor

Three working modes of vi\vim editor

 2.11 View command help and manuals

2.1 Linux directory structure

The directory structure of Linux is a tree structure;

Windows systems can have multiple drive letters, such as C drive, D drive, and E drive;

Linux does not have the concept of a drive letter, there is only one root directory /, and all files are under it.

In the Linux system, the hierarchical relationship between paths is represented by: / ; for example: /usr/local/hello.txt

In Windows systems, the hierarchical relationship between paths is represented by: \ . For example: D:\data\work\hello.txt

2.2 Getting started with Linux commands

No matter what the command is and what it is used for, in Linux, the command has its universal format:

 

  •  command: the command itself
  • -options: [Optional, not required] Some options for the command. You can control the behavior details of the command through the options.
  • parameter: [optional, not required] The parameters of the command, most of which are used for the target of the command, etc.
  • [] in grammar means optional

ls command

 

  •  -a -l -h are optional options
  • The Linux path is an optional parameter for this command

When options and parameters are not used, the ls command body is used directly, which means: in a tiled form , the contents of the current working directory, that is, the user's HOME directory, are listed .

When using parameters, the parameters of the ls command mean: specify a Linux path and list the contents of the specified path.

  • -a means: all means listing all files (including hidden files/folders)

ls -a lists more content than ls. Those starting with . indicate hidden files/folders in the Linux system (as long as they start with ., they can be automatically hidden). These hidden files/folders can only be seen through the -a option.

  • -l option: means: display the content in the form of a list (vertically arranged) and display more information

The -l option actually has the same meaning as the graphical arrangement of folders in a list.

  • -h means to list the file sizes in an easy-to-read form, such as K, M, G

The -h option must be used with -l.

Notice:

The options in the syntax can be used in combination. For example, the -a and -l learned can be used in combination. Writing method:

  1. ls -l -a
  2. ls - her
  3. ls -al

The above three ways of writing are all the same, indicating that the functions of -l and -a are applied at the same time.

Home directory and working directory

Directly input the ls command to list the contents of the current working directory. What is the current working directory?

The command line terminal of the Linux system will be loaded by default when starting:

  1. The HOME directory of the currently logged in user is used as the current working directory, so the ls command lists the contents of the HOME directory;
  2. HOME directory: The personal account directory of each Linux operating user in the Linux system, the path is: /home/username;

Windows systems and Linux systems both have user HOME directories, as shown in the figure:

 

2.3 Directory switching related commands

cd changes working directory

When the Linux terminal (command line) is opened, the user's HOME directory will be used as the current working directory by default.

We can change the current working directory through the cd command .

The cd command comes from English: Change Directory

grammar:

The cd command requires no options, only parameters, indicating which directory to switch to;

The cd command is executed directly without writing parameters, which means returning to the user's HOME directory.

pwd View current working directory

It is actually inappropriate to verify the current working directory through ls.

We can use the pwd command to view the current working directory .

The pwd command comes from : P rint Work Directory

Syntax: pwd

pwd command, no options, no parameters, just enter pwd directly.

2.4 Relative paths, absolute paths and special path characters

Relative paths and absolute paths

Through pwd, I know that the current location is the HOME directory: /home/itheima. Now I want to use the cd command to switch the working directory to the Desktop folder. So, how to write the parameters (Linux path) of the cd command?

  • cd /home/itheima/Desktop
  • cd Desktop

Both of the above writing methods can correctly switch directories to the specified Desktop.

cd /home/itheima/Desktop absolute path writing

cd Desktopcd Desktop relative path writing

Absolute path: A way of describing the path starting from the root directory . The path description starts with /.

Relative path: A way of writing a path using the current directory as the starting point . The path description does not need to start with /.

special path character

The current working directory is: /home/itheima/Desktop. Now I want to go back one level and switch the directory to /home/itheima. How to do it? You can return to the HOME directory directly through cd or through special path characters.

Special path characters:

  • . represents the current directory, for example, cd ./Desktop represents switching to the Desktop directory under the current directory, which has the same effect as cd Desktop.
  • .. represents the upper level directory, for example: cd .. can switch to the upper level directory, cd ../.. can switch to the upper level directory.
  • ~ represents the HOME directory, for example: cd ~ to switch to the HOME directory or cd ~/Desktop to switch to the Desktop directory in HOME

2.5 Create directory command mkdir

New directories (folders) can be created through the mkdir command

mkdir from English : Make Directory

grammar:

 The parameter is required and represents the Linux path, that is, the path of the folder to be created, either a relative path or an absolute path;

The -p option is optional , indicating that non-existing parent directories are automatically created , which is suitable for creating continuous multi-level directories.

-p option explanation:

If you want to create multiple levels of directories at once, as shown below

 An error will be reported because the upper-level directories itcast and good do not exist, so the 666 directory cannot be created.

An entire chain can be created using the -p option.

Note: Creating a folder requires modifying permissions. Please ensure that all operations are within the HOME directory. Do not operate outside HOME because permission issues will arise. It will not succeed outside HOME. We will explain the knowledge of permission control later.

2.6 File operation commands

touch, cat, more commands

touch create file

Files can be created through the touch command

grammar:

The touch command has no options, and the parameter is required , indicating the path of the file to be created. Relative, absolute, and special path characters can be used.

cat command to view file contents

Once we have the file, we can view the contents of the file through the cat command. However, we have not yet learned the vim editor and cannot edit content into the file. Therefore, for the time being, we will first manually add content to the file through graphics to test the cat command.

 

After preparing the file contents, you can view the contents through cat.

grammar:

Cat test.txt can view the file contents.

Cat also has no options, only required parameters . The parameters indicate: the file path to be viewed. Relative, absolute, and special path characters can be used.

more command to view file contents

The more command can also view the file content. The difference from cat is that cat directly displays all the content . more supports page turning. If the file content is too much, it can be displayed page by page.

grammar:

There are also no options, only required parameters. The parameters represent: the file path to be viewed. Relative, absolute, and special path characters can be used.

During the viewing process, use the space to turn the page and use q to exit the viewing process.

cp, mv, rm commands

cp command to copy files folder

The cp command can be used to copy files\folders. The cp command comes from the English word: copy

grammar:

 The -r option, optional, is used to copy folders , indicating recursion; when copying folders, the -r option must be used, otherwise it will not take effect.

  1. Parameter 1, Linux path, represents the copied file or folder;
  2. Parameter 2, Linux path, indicates the place to be copied;

mv move files or folders

The mv command can be used to move files\folders. The mv command comes from the English word: move

grammar:

  1. Parameter 1, Linux path, indicates the file or folder to be moved
  2. Parameter 2, Linux path, indicates the place to be moved. If the target does not exist, rename it to ensure that the target exists.

rm delete files and folders

The rm command can be used to delete files and folders.

The rm command comes from the English word: remove

grammar:

  1. Like the cp command, the -r option is used to delete a folder (to delete a folder, you must use the -r option)
  2. f means force, forced deletion ( no confirmation message will pop up). Ordinary users will not be prompted when deleting content. Only root administrator users will be prompted when deleting content, so ordinary users generally do not use the -f option;
  3. Parameter 1, parameter 2,..., parameter N represent the paths of files or folders to be deleted, separated by spaces, so remove can delete one file/or folder, or multiple files or folders. .

rm delete files and folders - wildcard *

The rm command supports the wildcard character *, which is used for fuzzy matching.

The symbol * represents a wildcard character, which matches any content (including empty), example:

  1. test* means matching anything starting with test ;
  2. *test means matching anything ending with test ;
  3. *test* means matching anything containing test .

Demo: Delete all files or folders starting with test

 Notice:

rm is a dangerous command, especially when you are the root (super administrator) user. Please use with caution.

Please do not execute the following commands under the root administrator user :

rm -rf /

rm -rf /*

The effect is equivalent to formatting C drive on Windows.

2.7 Search command

which

The Linux commands we learned earlier are actually binary executable programs. It has the same meaning as the .exe file in Windows system.

We can use the which command to see where the program files for the series of commands used are stored .

grammar:

 

find - Find files by file name

In graphics, we can easily search for specified files through the search function provided by the system.

Similarly, in Linux systems, we can search for specified files through the find command.

grammar:

 Find the file named: test, starting from the root directory:

Based on the syntax, there can be commands: find / -name "test"

find command - wildcard

The file name to be searched supports the use of wildcard character * for fuzzy query. The symbol * represents a wildcard character, which matches any content (including empty), example:

test* means matching anything starting with test

*test, means match anything ending with test

*test* means matching any content containing test

Based on the meaning of wildcards, you can use the find command to perform fuzzy query on files.

find command - Find files by file size

grammar:

  1.  +, - means greater than and less than;
  2. n represents the size number kMG represents the size unit;
  3. k (lowercase letter) represents kb, M represents MB, and G represents GB.

Example:

Find files smaller than 10KB: find / -size -10k

Find files larger than 100MB: find / -size +100M

Find files larger than 1GB: find / -size +1G

2.8 grep, wc, and pipe characters

grep

You can use the grep command to filter file lines by keywords from a file.

grammar:

  1. Option -n, optional, indicates to display the line number of the matching line in the results;
  2. Parameters, keywords, required , indicating filtering keywords, with spaces or other special symbols, it is recommended to use "" to surround the keywords;
  3. Parameter, file path, required , indicating the file path to filter the content, which can be used as the content input port . ( can be used as the input of the pipe character )

For example: grep "haha 666" 666.text.txt

wc command to do quantity statistics

You can count the number of lines, words, etc. in a file through the wc command.

grammar:

  • Option, -c, counts the number of bytes
  • Option, -m, counts the number of characters
  • Option, -l, counts the number of lines
  • Option, -w, counts the number of words
  • Parameters, file paths, files to be counted, can be used as content input ports (can be used as pipe character input)

pipe character |

After learning the grep command, we are going to learn a new special symbol, the pipe character: |

The meaning of the pipe character is: use the result of the command on the left side of the pipe character as the input of the command on the right side.

 As shown above: The output result of cat itheima.txt (file content) is used as the input of the grep command on the right (filtered file).

The pipe character has many applications:

  • 1. ls | grep Desktop, filter the results of ls 

 

  • 2. find / -name “test” | grep “/usr/lib64”, filter the results and only find results with /usr/lib64 in the path

 

  • 3. cat itheima.txt | grep itcast | grep itheima, you can use it in nested formats

             The result of cat itheima.txt is used by grep itcast;

             cat itheima.txt | grep The results of itcast are used by grep itheima.

example:

To perform statistics on the created test.txt, please use a combination of cat, grep, pipe character, and wc commands to perform statistics:

  • There are several lines with the itcast keyword in the statistics file.

cat test.txt | grep itcast | wc -l

  • Count how many words are in the results with the itheima keyword in the file

cat test.txt | grep itheima | wc -w

2.9 Redirection characters of echo and tail

echo command

You can use the echo command to output specified content on the command line.

grammar:

 No options are required, only one parameter indicating the content to be output. Complex content can be surrounded by ""

Demo: Display on terminal: Hello Linux

  •  With special symbols such as spaces or \, it is recommended to use double quotes.

Because if it is not surrounded, it will be easily recognized as parameter 2 after the space. Although echo will not be affected, you have to develop a habit.

backtick'' 

Take a look at the following command: echo pwd

 The original intention is to output the current working path, but pwd is output as an ordinary character.

We can enclose the command with backticks (commonly called floats) `. The content surrounded by ` will be executed as a command instead of ordinary characters.

Redirector

  1. >, overwrite and write the result of the command on the left to the file specified on the right side of the symbol.
  2. >>, append the result of the command on the left to the file specified on the right side of the symbol.

Demo:

  • echo “Hello Linux” > itheima.txt

  •  echo "Hello itheima" > itheima.txt, execute it again, overwriting the new content

Overwrite the content in the original file and output it as new content.

  •  echo “Hello itcast” >> itheima.txt, execute it again, use >> to append new content

 Add the newly entered content to the line below the original content.

tail command

 Using the tail command, you can view the tail content of the file and track the latest changes to the file. The syntax is as follows:

  1. Parameter, Linux path, represents the tracked file path;
  2. The option, -f, indicates continuous tracking;
  3. The option, -num, means to check how many lines at the end, if not filled in, the default is 10 lines .

Example:

  1. Check the last 10 lines of the /var/log/vmware-network.log file: tail /var/log/vmware-network.log
  2. Check the last three lines of the /var/log/vmware-network.log file: tail -3 /var/log/vmware-network.log

Using the -f option, file changes can be tracked continuously.

2.10 vim editor

vi\vim is the abbreviation of visual interface and is the most classic text editor in Linux .

Like the text editor in the graphical interface, vi is an excellent choice for editing text files under the command line.

Vim is an enhanced version of vi. It is compatible with all instructions of vi. It can not only edit text, but also has the function of shell program editing. It can identify the correctness of the grammar by using fonts of different colors, which greatly facilitates program design and editing.

Three working modes of vi\vim editor

Command mode

In command mode, the editor understands the keys you press as commands and executes different functions driven by commands. Under this model, text editing is not freely possible.

Insert mode

It is also called edit mode and insert mode. In this mode, the file content can be freely edited.

Last line mode

Starting with:, it is usually used for saving and exiting files.

If you need to edit the file through vi/vim editor, please use the following command:

vim is compatible with all vi functions, and all vim commands will be used in the future.

If the file represented by the file path does not exist, this command will be used to edit the new file.

If the file represented by the file path exists, this command is used to edit the existing file.

Editing a file through the vi/vim command will open a new window. At this time, this window is: command mode window. Command mode is the entrance and exit of the vi editor.

For example:

  1. Use: vim hello.txt to edit a new file. After execution, you will enter the command mode.
  2. In command mode, press i on the keyboard to enter input mode
  3. Enter in input mode: itheima and itcast.
  4. After the input is completed, press esc to return to the command mode.
  5. In the command mode, press: on the keyboard to enter the bottom line command mode
  6. Enter: wq in the bottom line command, save the file and exit the vi editor

Command mode shortcut keys:

 

 

 Bottom line command pattern:

There is nothing special about the editing mode. After entering the editing mode, any shortcut keys have no effect, just enter text normally. The only thing you need to remember is that you can return to command mode through esc.

In the command mode, enter : to enter the bottom line command mode, which supports the following commands:

 2.11 View command help and manuals

  • If you want to check other options of the command, you can do the following:

Any command supports: --help option, you can use this option to view the help of the command .

For example: ls --help will list the help documentation for the ls command.

  • If you want to view the detailed manual of the command , you can view it through the man (manual, manual) command

For example: man ls is to view the detailed manual of the ls command.

man cd is to view the detailed manual of the cd command.

  • Most manuals are in English. If you have difficulty reading, you can use the redirection character: man ls > ls-man.txt, output the manual to a file, and then use a translation software to translate the content and view it.

Guess you like

Origin blog.csdn.net/m0_49687898/article/details/131410366