Efficient command line skills for Linux users

Source: Internet
Author: User
Tags html to text
I recently saw a Q & A question on Quora about saving time Tips for Linux users in high efficiency. Summarize the answers to this question and add your own

I recently saw a Q & A question on Quora about saving time Tips for Linux users in high efficiency.
Summarize the answers to this question and add some of your own experiences. record them as follows for your reference.


The following describes some command line tools that are useful in the daily work of several respondents.
For any command that you do not know, use "man" "View, or use Google.
Some commands must be installed with the yum, apt-get install command.

Table of Contents
  • 1. basic commands
  • 2. some expressions
  • 3. routine commands
  • 4. Data processing
  • 5. system debugging
  • 6. Reference
1. basic commands understand the basic bash by reading the entire bash man page. learn about VIM in Linux. although you have Emacs and Eclipse, VIM is still a powerful tool to the right. Understand SSH and the basic password-free authentication method. For example, use ssh-agent or ssh-add. Lingxi Zhiqui usually uses the following script to complete password-free verification, saving time and effort.
Execution method sh nopasswd USER REMOTE_HOST
Before executing this script, confirm:


  • Id_dsa.pub already exists on the local machine. If no. Run the ssh-keygen-t dsa command to obtain the result.
  • Log on to the user's home directory on the remote machine. the. ssh folder already exists. if No. ssh folder is created.
$ cat  nopasswd #!/bin/sh  scp ~/.ssh/id_dsa.pub  $1@$2:~/ssh $1@$2 " touch ~/.ssh/authorized_keys ; cat ~/id_dsa.pub  >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys; exit"
Familiar with common task management commands &, Ctrl-Z, Ctrl-C, jobs, fg, bg, and 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,-, -B software installation command to understand apt-get and yumcat-n can help display the row number. 2. some expressions !! Run the previous command again! $ Last Word of the previous command {.. b} a list of {a, B, c} words a, B, c} in the order from a to B. you can use touch/tmp/{foo, bar, baz} {$1-$9} command line parameter $0 command name being executed during shell script execution $ # number of parameters passed in the currently started command $? The execution return value of the previous command. $ Process ID of the shell. $ * Start from $1 and start all parameters of the shell script. 3. run Ctrl-R. in bash, Ctrl-R is used to search for Ctrl-W, Ctrl-U, and Alt-BackSpacebash in History commands, ctrl-W delete the last word, Ctrl-U delete the last line, Alt-BackSpace delete the word man readline before the cursor contains a large number of default hotkey bindings in bash. cd-returns a very powerful command from the previous working path xargs. If you are not sure whether the task can be correctly executed, you can check it with xargs echo. The following is an example of using this function:
find . -name *.py | xargs grep some_functioncat hosts | xargs -l {} ssh root@{} hostname
Pstree-p uses the powerful tool pgrep in the process tree, and pkill uses the name to find the process, or directly sends a signal to the process with the specified name.


  • Understand the signal that a user can send to a process. For example, kill-STOP [pid] to suspend the pid process.
Nohup, disown, screen, and tmux are useful when you need to keep the process running in the background. Lsof, netstat-lntp queries the current process listening port. In the bash script, set-x is used to obtain the debug output and set-e is used to obtain the error output .; The semicolon (;) is used to enable a sub-shell and close it after running. For example:
# Execute some commands (cd/some/other/dir; other-command) in the current path # The working path is still the current directory
Learn about multiple parameter expressions in shell $ {name :? Error message} checks whether a variable exists. If no error message exists. $ {Var % suffix}, $ {var # prefix} outputs the part of the var variable except the prefix or suffix. For example, the output code is foo.txt.
var = foo.pdf echo ${var%pdf}.txt
<,> Input/output redirection. Some_command> logfile 2> & 1 output both the standard output and the standard error output during the running of some_command to the logfile. Use man ascii to obtain a handy ASCII table that contains 10-and 16-digit values. Screen and dtach in remote ssh painting, use these two commands to save your session and avoid interruption due to network problems. Curl, curl-l, and wget are useful in web page debugging. These commands can help you download webpage code. This command is useful when lynx-dump-stdin converts HTML to text xmlstarlet to process XML. When you need to use a remote server to access a webpage, this command can help you establish an ssh tunnel between the remote server and your machine. The following configurations of ssh connection optimization can help you avoid link loss. you do not need to enter yes to confirm the link with the remote server every time, and enable compression in the link. We recommend that you put it in. ssh/config.
TCPKeepAlive=yes ServerAliveInterval=15 ServerAliveCountMax=6 StrictHostKeyChecking=no Compression=yes ForwardAgent=yes
Add # in front of the command that is being entered, half of the command has been entered, and suddenly change your mind to reduce the running time,
You can use Alt-# to add '#' before the command to change the entire command into a comment. In this way, you can find the command later in the command history. Cron helps you develop scheduled tasks. When Ctrl-S Ctrl-C accidentally needs to output a large amount of text, input these two operations in sequence,
The program can be terminated more quickly than simply pressing Ctrl-C. 4. Data processing sort, uniq, uniq-u, and uniq-d understand the command cut, paste, and join to understand the maintenance tools for these text files. After using cut, many people forget to join sort/uniq for the intersection, addition, and population of the set. suppose a and B are two text files, and the rows are unique.
The following commands can quickly implement some set operations.
cat a b | sort | uniq > c # c is a union b cat a b | sort | uniq -d > c # c is a intersect b cat a b b | sort | uniq -u > c # c is set difference a - b 
Using locale settings in LC_ALL = CLinux affects a large number of command line tools, including sorting tools.
In most installed Linux systems, LANG or other locale are set to US English by default.
However, this may lead to several times slower sorting and other commands.
Therefore, export LCALL = C can avoid using i18n to process data and improve performance. Awk and sed tools can replace and modify complex data.
For example, the following command calculates the sum of the data in the middle and lower columns of a text file.
Using shell to perform this operation is three times faster than using Python.
awk '{ x += $3 } END { print x }'
Shuf: this command can be used to mix the lines in a file or randomly select some lines from the command. Sort understands how sort's common options (-t,-k,-s) work. Note:-k1, 1 will only sort the first column,
-K1 is sorted by the entire row. -S can achieve stable sorting.
For example, you can use this command to sort the second domain and then sort the domain.
cat INPUT_FILE | sort -k1,1  | sort -s -k2,2
Tab input is in the bash command line. if you need to enter a tab, you can use Ctrl-V Or $ 'T' implements hd and bvi implements hexadecimal extraction and binary editing for binary files respectively. Strings and grep can help search for text in binary files. Iconv and uconv can help convert text encoding to split. csplit can separate files by size and by specific mode. 5. system debugging iostat, netstat, top, atop, htop, and dstat can help you understand the hard disk, CPU, memory, and network status.
This helps you first understand what is happening in the system. If you want to know the memory status, these two commands are very important.
Cached indicates the file cache size in the Linux kernel. Kill-3 When debugging a Java program, you can use this command to find the complete stack trace in stderr/logs,
Heap information (including garbage collection details). mtr and traceroute can help find network problems, the former is better than traceroute. Iftop and nethogs commands can be used to find out which port or process occupies the network bandwidth. The Apache tool AB and siege can help you quickly check the performance of web servers. Wireshark and tshark are powerful tools for more advanced network debugging. The strace and ltrace commands can help you provide some clues about program running failures, false positives, crashes, and other issues without knowing anything about them.
In addition, they can help find some performance problems. For example, The-c option can be used for profiling;
The-p option can be mounted to a specified process. Ldd checks shared libraries. gdb understands how to use GDB to connect to a running process and obtain its stack trace. /Proc/is useful for on-site debugging. For example,/proc/cpuinfo,/proc/XXX/cwd,/proc/XXX/exe,/proc/XXX/fd /, /proc/XXX/smapssar this command displays the CPU, memory, and network history when you need to determine why a system error occurred at a certain time. Stap and perf are useful when you need a deeper analysis system and performance. Dmesg is useful when the system is vulnerable, such as hardware or driver problems. 6. Reference
  • Http://unixhelp.ed.ac.uk/scrpt/scrpt2.2.2.html
  • Http://www.quora.com/Linux/What-are-some-time-saving-tips-that-every-Linux-user-should-know
7. thank you
  • Yang Qing: increase Alt-BackSpace
  • Dikar: modify string-> strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.