Summary of Linux high-frequency common instructions

Table of contents

Know the Linux directory structure

Absolute path: starting with the root directory, called an absolute path

Relative path: It does not start with the root directory, it is called a relative path

ls

pwd 

cd

mkdir

touch

cat

echo

rm 

cp

mv

ps and netstat

1. Query the corresponding process id according to the process name

2. View the corresponding process according to the port number

 3. View the corresponding bound port number according to the process id

​edit

vim editor

1. Enter the file

2. Enter edit mode

3. Save and exit

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


Know the Linux directory structure

 special directory:

                        / is called the root directory
                        . is called the current directory
                        .. is called the parent directory of the current directory

Absolute path: starting with the root directory, called an absolute path

Like : /usr/share/tomcat/logs/

Relative path: It does not start with the root directory, it is called a relative path

Shapes like : ./logs starting with . or .. are called relative paths .


ls

View the content of the specified directory, similar to double-clicking the D drive in Windows system to view the content of the D drive

  • -a lists all files in the directory, including hidden files starting with .
  • -d Display the directory as a file instead of the files under it. Such as: ls -d specifies the directory
  • -k indicates the size of the file in k bytes. ls –alk specifies the file
  • -l lists detailed information about the file.
  • -r Sort directories in reverse.
  • -t Sort by time.
  • -R lists the files in all subdirectories. ( recursive )

example 

ls -l

ls -l can list all files in the current directory, ls -l can be simplified to ll (here l is a lowercase L)

pwd 

View your current directory

Example: In case we get lost and don’t know which path we are in, we can use pwd to check our current absolute path

cd

Switch to the specified directory, which can be understood as double-clicking a directory to enter

  •  cd .. Back to the previous directory
  • cd ~ : enter the user's home directory
  • cd - : return to the most recently visited directory

mkdir

Create new directory folder (i.e. create new folder)

  • mkdir -p xxx/yyy creates a multi-level directory

touch

Create a new file, for example, create a 123.txt file

Example: touch 123.txt

cat

View the contents of a file in a folder

 

echo

write file content

Example: echo "hello" > 1.txt

rm 

Delete a file or directory (folder)
  • -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 directory and all files under it recursively 

Example: rm -i test.txt

Important notes:
Do not run rm -rf /, especially on the company's production server (/ is the root directory, r is recursive, f is directly deleted as long as it is a file)

cp

Syntax : cp [ options ] source file or directory target file or directory
Copy a file or directory (folder)
  • -f Forcefully copy a file or directory, regardless of whether the destination file or directory already exists
  • -i ask user before overwriting files
  • -r Recursive processing, processing 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

Note: If cp is copying a directory (folder), the -r option is required to recursively copy all files in the directory, otherwise an error will be reported

Example: cp test1.txt test2.txt  

mv

rename or move

Syntax : mv [ options ]   source file or directory    target file or directory
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 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 be the source directory name), and it will rename the given source file or directory to the given target filename.
3. 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 the target directory.

Example: mv test1.txt test2.txt to rename

ps and netstat

Use the netstat command: The netstat command can display network connections, routing tables, and network interface information. You can use the netstat command to check which process a certain port is occupied by.
The specific command is: netstat -nap | grep port number, where the port number is the port number to be queried.

1. Query the corresponding process id according to the process name

ps -ef | grep 进程名  #根据进程名查进程pid


ps  -aux | grep 进程名或进程id     #可以查到该进程占了多少CPU和内存占用比

2. View the corresponding process according to the port number

netstat -nap | grep 端口号  //其中端口号为需要查询的端口号。

 3. View the corresponding binding port number according to the process id

netstat -nap|grep pid    //pid为进程

vim editor

vim is a well-known text editor . The cat, less, head, tail and other commands learned earlier can only view text , but cannot edit text. You can edit it with vim.

1. Enter the file

 Syntax: :vim filename

Example: vim 1.txt

 Enter the vim interface

2. Enter edit mode

        After vim opens the file, it defaults to the normal mode . The keys on the keyboard in normal mode represent shortcut keys for some special functions . ( For example, pressing j is not to enter the letter "j", but to move the cursor down one line ),
Need to enter insert mode for text editing.
Press the i key to enter the insert mode . ( Insert --INSERT-- in the lower left corner ) Then you can edit normally like Notepad

3. Save and exit

When we finish typing in the vim edit box, we want to save and exit. At this point you need to switch from insert mode to normal mode. Files cannot be saved in insert mode , you need to return to normal mode first  .

Steps: 1. Press the Esc key to return to normal mode .

          2. In normal mode, enter  : wq and press Enter to save the file and exit.    

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

1. [Tab] button --- has the functions of "command completion" and "file completion"
If there is only one matching item, press the tab key to complete it automatically. If there are multiple matching items, press the tab key twice to display all the matching items
2. [Ctrl]-c key --- let the current program "stop", terminate the current program running
3. [Ctrl]-d button --- usually 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_73471456/article/details/131626259