Today I’m going to share with you a relatively basic topic: The basic operation of the terminal, I believe that many students have a negative view of the terminal, think oh, what terminal is easy to use, there are so many difficult commands to remember, it is not fragrant with beautiful GUI application, so this article will lead you to understand the terminal, the article is divided into three parts: In the first part, THE author will introduce some common commands under the terminal. In the second part, the author will show how to solve some problems with commands with several scenarios. In the last part, the author will share some plug-ins to improve efficiency. I hope that after reading this article, we can use the terminal to complete the work in our daily work. I believe that this can greatly improve our work efficiency.

The author takes the MAC system as an example, the terminal uses iterm (beyond the MAC terminal), and the shell uses ZSH (of course, it does not mean that ZSH is the best shell, this depends on personal habits, you can also make your own terminal bash, fish and other familiar shell environment). Without further ado, let’s begin

I. Common commands:

1. Man command can be understood as a command instruction manual. When you encounter a command that you do not know or cannot remember how to use the command, you can use man command, and the detailed description of the command will be printed in the terminal. Here I recommend a command called TLDR (Too Long,don’t read.), which is more concise than the MAN command. The man command and TLDR command can be used to query the usage of a command.

2, grep command

Grep is a powerful text search tool. It can use Regular Expression to search text and Print out matched lines. Its full name is Global Regular Expression Print and it is very flexible to use. It is very helpful for students who often check their logs. You can run the grep ABC xxx.log command to display the lines containing ABC keywords in the xxx.log file. Grep can also contain some parameters: -v indicates that the log file is not included, -i indicates that case is ignored, and regular expression search is supported. For example, each line in a log file has a timestamp. Use the regular expression to search for the line in a specified period. Of course, there are many other uses of grep, and it is recommended that you use this command more often.

3, tail and head;

Tail -f file is used to monitor file changes. Tail -n XXX file is used to output the XXX line from the last to the last line of a file. Head -n XXX file is used to output the XXX line of a positive file. Tail -f file | grep monitor file contains the XXX XXX content changes. Ll – t | head -n 10 to output the current directory 10 files of the latest changes.

4, awk command, usage:

Awk provides extremely powerful capabilities: it is used to process data and generate reports (Excel). The data processed can be one or more files, either from standard input or piped from standard input. This command is mostly used to split a line of text into multiple columns using custom delimiters, and then filter out the desired columns for output or statistics. The common way is to collect statistics on the interface call usage of online logs. Directly awK the interface URL path column and use the sort command and uniq command to collect statistics. Awk ‘BEGIN{commands} pattern{commands}END{commands} ‘file awK ‘BEGIN{commands} pattern{commands}END{commands}’ file AWK ‘BEGIN{commands} pattern{commands}END{commands} ‘file

5, the top command

The top command is a commonly used performance analysis tool in Linux. It displays the resource usage of each process in the system in real time, similar to the Task manager in Windows. Running the top command will enter the top model, inside there are many indicators, a detailed explanation may refer to www.cnblogs.com/peida/archi… When entering the top mode, there will be many columns. The CPU column and MEM column are often checked. Press P for CPU sorting and M for MEM sorting. To view a process, run the ps command to find the process id and run the top -p process ID.

6, lsof command:

The lsof command is a tool that lists open files on the current system. In The Linux environment, everything exists in the form of files. Through files, you can access not only regular data, but also network connections and hardware, such as transmission Control Protocol (TCP) and user Datagram Protocol (UDP) sockets. The system assigns a file descriptor to the application in the background. Regardless of the nature of the file, the file descriptor provides a common interface for the application to interact with the underlying operating system. Because the list of descriptors for an application’s open files provides a great deal of information about the application itself, being able to view this list through the LSOF tool can be very helpful for system monitoring and troubleshooting. I often use lsof -i:port to check whether a port is occupied. I use lsof -p process number to check the file descriptor of a process and grep LISTEN to check the port information of the process.

7, ansible command github.com/ansible/ans… Usage:

Ansible allows you to execute remote commands and download files in batches, which is a great tool for operation and maintenance students. Detailed use you can refer to the relevant documents.

8, nc command, usage:

Nc-vz IP port: scanning IP :port TCP connection, nc-uz IP port: Scan the IP: port udp connections, such as whether a service hangs up the can use this command to determine, nc could also be used to transfer files blog.csdn.net/u012486730/… .

Netstat = netstat = netstat

The netstat command displays statistics related to IP, TCP, UDP, and ICMP protocols and is generally used to check the network connection status of each port on the local host. Common parameter -t or -tcp Displays the connection status of TCP. -u or – udp Displays the connection status of udp transport protocol. -p or -programs displays the identifiers and names of the programs using the Socket. -n or -numeric uses the IP address directly, not through the DNS. -l or – listening Displays the Socket of the monitored server. So basically use netstat -tunlp directly to check the current machine is listening to the TCP UDP situation.

Two, practice:

Wow, something wrong with the line, check the log:

1, if you can locate a machine error, we will generally perform the following steps:

  • SSH to a machine first, SSH requires a password, if you need to enter a password every time is a bit too cumbersome (iterm supports a key to send a text), you can send the local certificate to the machine to achieve a secret free login. Can also write a expect for avoid close to www.cnblogs.com/Shoudler/p/… .
  • SSH to the machine, CD to the specific directory and grep error code, awk filter the desired information, tail + grep to see if there are any errors, such as this, basically can locate the error log.

2, because the user traffic is generally in the way of load balancing to hit the back of many machines, so it is not able to locate a machine, at this time if we want to SSH each machine is too undesirable, can be solved by the following steps:

  • Run the grep + tail + awk command to see which machine is running the log file. Ansible has a weakness: the log file must be a pair path. If the directory level is deep, I need to write a very long path (and there is no auto-complete ~). Plus ansible commands are inherently long, which can be inefficient and embarrassing if you forget the path.
  • For this reason, I wrote remote-tail and remote-grep by myself, which actually simulate the process of executing multiple SSH commands. The convenience is that the requirements of multi-machine grep and multi-machine tail can be realized only after configuration, including log file address and machine IP address, etc. The project is simple and is written in GO, so you can fork it out to supplement the scenes you need.

There is a puzzling process I need to look into:

With ps aux | grep XXX to locate the process, after some pid is to say, we can see with top -p pid under the process of resource utilization, use lsof -p pid to view the process takes up the file descriptor, Ll /proc/{pid}/exe to check where the process started.

How the disk is almost full again:

Using the df command to view the current disk usage, the implementation of the du – sh * | sort – nr will print the current directory in the directory size, so that we see which is the larger directory takes measure to space. If it is found that after deleted some files disk or occupancy ratio is higher, then you can use lsof | grep delete can view the current system have delete unreleased file descriptors, these can also be occupied the main culprit of a disk.

Three, improve efficiency

Ohmyzsh contains a variety of themes and can be used to dress your little black box with a pretty skirt.

2. For auto completion: ZSH is case insensitive, which is comfortable, rather than tabbing at uppercase directories. You can also install zsh-AutoSuggestions, which displays recently written commands in light-colored formats at the back of commands. Pressing → completes the command.

3, alias can greatly improve the efficiency of command typing, it can replace a long command or a command with many parameters into a custom command, so people prefer LL rather than ls -L.

CD /etc goes to the system configuration directory and CD ~ goes to the home directory. If you reach a directory with a long path, you need to type a lot of paths. This directory is also a common office directory. You can use the alias command to enter the office directory to abbreviate it to a short command such as cdwork; If there are more than one such directory that is more troublesome, here recommend a command called autojump, it can be recently visited by the weight of the directory to a place, and then fuzzy search tojump to the directory, just type J + part of the directory name tojump, very convenient.

The Ranger command is recommended. Running this command will enter the File manager under Linux. By default, the shortcut keys of VIm will be used for file operation without the mouse. The biggest advantage of this command is that it is highly configurable. It is very convenient to write some complex operation scripts to integrate into Ranger, such as decompressing files and deleting in batches. I suggest you try it, and I believe you will be very fond of it.

6, FZF command github.com/junegunn/fz… “Is a fuzzy search command for files. When a CD goes to a directory, you want to edit a file in the directory. You know the file name but don’t know where it is. You can use the find command to do this, but find is somewhat inefficient for simply looking up a file. After entering FZF mode, you only need to write a few keywords to list the files you want to search, and FZF features more than that. It can be integrated into VIM for common searches: buffer files, recently used commands, and so on. It can be integrated into Ranger to search fuzzy files; It can be integrated into the kill command to search for the process you want to shut down. You can replace the system’s CTRL + R to search for recently executed commands. These configurations are available at github.com/junegunn/fz… The find.

The end:

Thank you can read here, that is to bring the terminal of some of the basic operation, hope that we can apply the black box, can use the command to complete the job with the command to complete, believe me this is definitely much more efficient than the little point with the mouse, and finally with a matrix of figure after ~ I hope you can like the people in the matrix, Tap a few lines of command and the screen will scroll in a cool way