Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A summary of some of my commonly used linux commands

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

After two years of operation and maintenance, I have used a lot of orders and have a deep understanding of how much efficiency can be improved after mastering some linux commands. To take a simple example, after doing research and development, we often run some data. For the processing of the result data, our product students are generally accustomed to using excel to do statistics, copy the data to excel, and then sort the data. Finally, I come to some simple conclusions. I only need the commands cat, sort, uniq, awk, and grep to do the same thing between the waves.

Here I would like to summarize some of the commands I have used in my work over the past few years. Of course, I will not mention those simple commands such as vim cd ls mv cp. If you do not know these commands, I suggest you learn them first. There are a lot of commands here, and I'll just briefly list a few of my commonly used parameters. In fact, I do not use a lot of orders. In this article, I just hope to let you know that there is such a tool, but if you want to know more about it, I suggest you to check the manual. I have also listed some references for some comparison orders.

Commands related to server running status

Ps

Looking at the system process threads, I usually use this command to view the process pid, and then use pid to do more in-depth troubleshooting.

Basic usage

Ps-aux to view all processes

Ps-T-p ${pid} View the threads of a process

references

10 important Linux ps commands in actual combat

Pstree

Looking at the system process tree, he can identify the relationship between the various processes with a tree structure.

Basic usage

Pstree

Top

Check the running of system process threads, the use of package resources, system load and so on. My usage is to see if the load on the server is high, and then see which process and which thread takes up more cpu.

Basic usage

Top lists all thread load information

Top-H lists load information for all threads

Top-H-p ${pid} lists the load information of all threads under a pid

Free

Check memory and usage

Basic usage

Free

File operation related

Cat

I always use this command to view configuration files, or log files, but I need to note that the cat command will output the entire file to the terminal. If the file content is very large, it is recommended to use grep to filter, or directly use the less or more command.

Basic usage

Cat file.txt

Tail

Look at the end of a file, or at the end of the standard entry and exit, the default value shows 10 lines, and you can use the-n parameter to specify how many lines to output.

Basic usage

Tail-n 100 lines at the end of file.txt output

Tail-f file.txt continuously outputs new content as new files are added, which is generally used to view real-time logs.

Head

Very similar to the tail command, but head is the output of high-quality content, I feel that head is far less used than the tail command.

Basic usage

Head-n 100 file.txt output first 100 lines

More

It is also used to view files, but the more command loads only one screen of content and can be flipped down, because it loads less content, so it is much faster than cat.

Basic usage

More file.txt

Less

Very similar to more, but can be flipped up and down, feel that less and more only need less, and you can completely remove more.

Basic usage

Less file.txt

Grep

This is a command I use very often, especially when troubleshooting problems. I need to use grep to filter out some of the things I want from a large amount of data. Grep also supports regular expression matching.

Basic usage

The grep "abc" file filters out rows that contain abc from the file.

Awk

As I said at the beginning, this command is one of my most commonly used commands. For example, when a file has multiple columns, I can use awk to output specific columns, or do some simple statistical summation, average, or do some simple data formatting.

Basic usage

Cat data | awk'{print $1, 3, 7, 5} 'output column 135. Note that the subscript starts at 1.

Cat data | awk'{sum + = $1} END {print sum} 'sums the first column

Cat data | awk-F'\ t'{print $1 recording 3} 'classifies each row of data by tab and outputs 1 / 3 columns

references

Introduction of Ruan Yifeng awk

Sort

Sort the standard content

Basic usage

Cat file | sort sorts the data in file. Note that they are sorted in dictionary order. If you want to sort them by numerical value, you need to be able to add the-n parameter.

Cat file | sort-K2-n-r is sorted in reverse order by the second column,-k specifies which column, and-r means to flip reverse

Uniq

De-weight the sorted content, note that it is only the adjacent and the same de-weight, so if you want to remove the weight globally, you need to first use sort sorting.

Basic usage

Cat file | sort | uniq sorts and removes duplicates of files in file

Cat file | sort | uniq-c sorts and deduplicates the files in file, and outputs the number of occurrences per line

Wc

I always use wc to count the uplink. In fact, wc can count not only how many lines, but also how many words and characters.

Basic usage

Wc-l file count how many lines are in the file

Wc-w file how many words

Wc-c file how many bytes

Wc-m file how many characters

references

Https://www.jb51.net/LINUXjishu/62056.html

Sed

Parallel

Most linux commands are single-process, and this command allows other commands to be executed by multiple processes.

references

15-minute introduction to artifact gnu parallel

Scp

In the past, when there were a large number of machines for operation and maintenance, it was usually necessary to modify a configuration file in batches, which was changed on one machine and then distributed to other machines with scp scripts, which greatly improved the efficiency.

Basic usage

Scp aaa.txt test@192.168.1.3:/tmp/ puts the aaa.txt files in the current directory into the / tmp directory through the test account on 192.168.1.3.

Scp test@192.168.1.3:/tmp/aaa.txt. Contrary to the previous one.

Disk and IO

Du

View directory siz

Basic usage

Du-h-- max-depth=1 outputs the directory with the deepest layer, and then the file size is in a human-readable way, such as 1K 234m 2G

Df

View disk size and occupancy

Basic usage

Df-h to view the size and usage of each partition

Iostat

View the io status of the disk

Iotop

You can display the io status of each process in real time, similar to the top directory.

Find

Find files, search criteria can be file name, file date, file size, very powerful. Before we had a full disk on the server, we forced to delete files with a file name of * .log that were larger than 1g in a certain directory of the server and took more than 2 days, which was done with the command find plus xargs.

Basic usage

Find / home/test-iname "test.txt looks for a file named test.txt under / home/test/. Wildcards are also supported.

Find / home/test-isize + 100m find files larger than 100m under / home/test

references

Wikipedia unix find

Locate

The locate command is much faster than find-name to locate a specific file, because instead of searching for a specific directory, it searches for a database / var/lib/mlocate/mlocate.db, which is regularly updated through a cron, so it is possible that the newly created file will not be retrieved.

Basic usage

Locate a.txt locates the location of the a.txt, and if there are multiple a.txt in the system, all of them will be displayed.

Tree

You can see the tree directory structure.

Basic usage

Tree-L 2 shows only two layers of tree structure.

The network

Ping

Check to see if the network is connected

Basic usage

Ping www.baidu.com

Nc

Netcat, can be used to see whether a remote port is open, the function is very powerful, but I do not use much.

Basic usage

Nc-z xindoo.me 443 checks whether port 443 on my server is open (open, of course)

references

Introduction to linux nc command

Route

View and manipulate the native routing table

Basic usage

Route lists the local routing table

references

Route command

Netstat

If you look at the network status of this machine, you can see the port occupancy and network links.

Basic usage

Netstat-antp

Traceroute

View all the routing nodes through which a request goes to the target server, which is generally used to troubleshoot network problems.

Basic usage

Traceroute www.baidu.com

references

Netstat command

Iftop

View real-time network io

Lsof

View port occupancy

Dig

To check the information of a domain name, when doing OPS, you often need to verify whether a domain name resolution change is effective, because usually a domain name will be-A to multiple ip, and you can only see one ip with the ping command. At this time, I will use dig to view the domain name resolution information.

Basic usage

Dig www.baidu.com

references

Introduction to dig command

Curl

To initiate a http request, I usually use this command to verify whether the service can be accessed properly. It has the function of getting the html source code.

Basic usage

Curl www.baidu.com

Curl-I www.baidu.com get the request header of the request baidu.com

references

The usage of curl

Wget

Downloading a file on the network is basically a command-line download tool.

Basic usage

Wget xindoo.me/test.txt downloads the test.txt file from my server to local

Other

Yum | apt install

Many times we don't have the tools we want on the server, we can install it with this command. Yum is the Shell front-end package manager in Fedora and RedHat as well as CentOS, and apt is on the ubuntu platform.

Basic usage

Yum install curl

Apt install curl

Man

This command is used to view other command manuals, you can see the detailed role of a specific command, and specific parameters. This is a very important command, and it is usually much more detailed than the help that comes with each command.

Basic usage

Man curl views the manual of the curl command

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report