Recently saw a question and answer question on Quora, about saving time tips for efficient Linux users.
The answer to the topic to learn the summary, together with some of their own experience, recorded as follows, convenient for themselves and everyone's reference.
Here are some command-line tools that are useful in the daily work of several respondents.
For any commands you don't know, use "man <COMMANDNAME>" to view or use Google.
Some commands need to be installed first with yum, apt-get Install command.
1 basic commands Learn basic bash read through the entire Bash man page. Learn vim on Linux systems, although you have Emacs and Eclipse, Vim is still the most powerful tool. Learn about SSH, the basic method of no password authentication. For example, through Ssh-agent, Ssh-add and so on. "Empathy" usually use the following script to complete no password verification, save effort.
Execution mode sh nopasswd USER remote_host
Before you execute this script, verify that:
There are already id_dsa.pub on this machine, if none. Use the command ssh-keygen-t DSA to obtain.
On the remote machine login user home directory, there is already an. SSH folder, if not created.
$ cat nopasswd #!/bin/shscp ~/.ssh/id_dsa.pub [email protected]$2:~/ssh [email protected]$2] Touch ~/.ssh/ Authorized_keys; Cat ~/id_dsa.pub >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys; Exit "
Familiar with the task management commands &,ctrl-z,ctrl-c,jobs,fg,bg,kill in bash. Basic file management commands ls, ls-l, less, head, tail, tail-f, LN, ln-s, chmod, Chown, Du, Du-sk *, DF, Mount basic network management commands ipconfig, ifconfig, Dig familiar with regular expressions, and the options used by Grep,egrep-O,-A,-B Software Installation commands understand Apt-get and yumcat-n can help display line numbers. 2 Some expressions!! Execute the last command on!$ again. {A] b} According to a number list {a,b,c} three words from A to B order a,b,c. You can do this by using touch/tmp/{foo,bar,baz}{$1-$9} command-line arguments when executing shell scripts-the name of the command that is being executed $# The number of arguments passed in the currently started command? The execution return value of the previous command. $$ the shell's process number. $* starts all parameters for the shell script starting with $. 3 daily use command ctrl-r in bash, Ctrl-r is used to search for ctrl-w in the history command, Ctrl-ubash, ctrl-w delete the last word, ctrl-u delete the last line. The man readline contains a lot of the default hotkey bindings in bash. CD-Returns the previous work path Xargs very powerful commands. If you are not sure if you can perform the task correctly, you can use xargs echo to view it first. Here is an example of this feature:
Find. -name \*.py | Xargs grep Some_functioncat Hosts | xargs-l {} SSH [email protected]{} hostname
Pstree-p the powerful tool of the process tree Pgrep,pkill uses the name lookup process, or sends a signal directly to the process that specifies the name.
Nohup,disown,screen, Tmux These two commands are useful when you need to keep the process running in the background forever. Lsof, NETSTAT-LNTP queries what process is currently listening on what port. Set in the Bash script, use Set-x to get the debug output and use SET-E to get the error output. The semicolon is used to open a child shell and run until the end is closed. For example:
#在当前路径下执行一些命令 (Cd/some/other/dir; other-command) # The working path is still the current directory
Understand the various parameter expressions in the shell ${name:?error message} to check for the existence of a variable if there is no output error message. ${var%suffix}, ${var#prefix} outputs the var variable in addition to the prefix or suffix part. As the following code output is foo.txt.
var = foo.pdfecho ${var%pdf}.txt
<,> input and output redirection operation. Some_command > LogFile 2>&1 outputs both the standard output and the standard error output during the Some_command run to the file logfile. Man ASCII obtains a usable ASCII table that contains 10 binary and 16 binary values. Screen,dtach in remote SSH painting, use these two commands to save your session and avoid interruptions due to network problems. Curl, Curl-l, wget in the Web page debugging, these commands can help you to download the Web page code, very useful. This command is useful when Lynx-dump-stdin converts HTML to text Xmlstarlet need to process XML. Ssh-l, ssh-d need to use a remote server to access the Web page, this command can help you establish an SSH tunnel between the remote server and your machine. SSH Connection Optimization configuration can help you avoid the loss of links, do not need to enter the Yes confirmation and the remote server link, as well as the link to enable compression. It is recommended to put it in the. Ssh/config.
Tcpkeepalive=yesserveraliveinterval=15serveralivecountmax=6stricthostkeychecking=nocompression=yesforwardagent =yes
Before the command is entered and the # command has been entered half, suddenly change the idea to do less to run,
You can use alt-# to add ' # ' to the command before the entire command becomes a comment. This will enable you to find the command in the command history later. Cron can help you set up some scheduled tasks for scheduled execution. Ctrl-s Ctrl-c will inadvertently require a large amount of output text, enter these two operations in turn,
More often than simply press CTRL-C to get the program to terminate faster.
4 data processing Sort,uniq, Uniq-u, uniq-d understand these sort commands cut,paste, join to understand the maintenance tools for these text files. Many people use the cut, forgetting that join uses Sort/uniq to set the intersection, and the complement operation = Assume that A and B are two text files, where the rows are unique.
The following commands allow you to implement some set operations quickly.
Cat A B | Sort | Uniq > C # C is a union bcat a B | sort | uniq-d > C # C is a intersect bcat a b b | sort | uniq-u > c< c2/># c is set difference A-b
Using the locale setting in Lc_all=clinux affects a number of command-line tools, including the Sort tool.
Most installed Linux systems set lang or other locale defaults to US 中文版.
But this can lead to several times slower ordering and other commands.
As a result, export lcall=c avoids processing data in the form of i18n, resulting in performance gains. Awk,sed These two tools enable complex data substitution and modification.
For example, the following command implements the sum of data in a lower three column in a text file.
Using the shell to do this is 3 times times faster than Python.
awk ' {x + = $ $} END {print x} '
Shuf the command can either mix rows from a file, or randomly select rows from it. Sort to learn how the common options for sort (-t,-k,-s) work. Note that-k1,1 will only sort the first column,
The-K1 will be sorted according to the entire row. -S to achieve stable sequencing.
For example, you can use this command to sort the second field first, and then sort by field.
Cat Input_file | sort-k1,1 | sort-s-k2,2
tab input in the bash command line, if you need to enter a tab, you can use ctrl-v <tab> or ' \ t ' to implement HD,BVI for binary files, the two commands respectively implement 16 binary extraction, binary editing operations. String,grep can help find text in a binary file. Iconv,uconv can help convert text encoding split,csplit separate files by size and split them according to specific patterns.
5 system Debug Iostat,netstat,top,atop,htop,dstat can help to understand the state of hard disk, CPU, memory, network.
This will help you have a first understanding of what is happening in the system. Free,vmstat These two commands are important if you want to understand the state of the memory.
Where cached is the size of the file cache in the Linux kernel. Kill-3 <pid> When debugging a Java program, using this command, you can find the complete stack trace in Stderr/logs.
Heap information (including garbage collection details). Mtr,traceroute can help find network problems, the former is better than traceroute. Iftop,nethogs These two commands can be done just to find out which port or process is consuming much network bandwidth. Ab,siege this Apache-brought tool can help you quickly check the performance of your Web server. Wireshark,tshark is a powerful tool for more advanced network debugging. Strace,ltrace these two commands can help you in the case of ignorance, the program failed to run, suspended animation, crashes and other problems brought some clues.
In addition, they can help to find some performance problems. For example,-c option can do profiling;
The-p option can be hung onto a specified process. LDD checking the shared library GDB knows how to use GDB to connect to a running process and get its stack trace. /proc/is useful when doing on-site commissioning. such as/proc/cpuinfo,/PROC/XXX/CWD,/proc/xxx/exe,/proc/xxx/fd/,/proc/xxx/smapssar in the need to determine why in the past some time the system will be wrong, this command can display the CPU, Memory and the history of the network. STAP, perf These two tools are useful when you need a deeper analysis system, as well as a performance situation. DMESG This works well when there are some very big things in the system, such as hardware or driver problems.
6 References
command-line skills needed for efficient Linux users