command-line skills needed for efficient Linux users

Source: Internet
Author: User
Tags garbage collection html to text locale stack trace

Excerpt from: 1, http://www.lingcc.com/2013/03/22/12282/

Content: 1, basic command:

Learn the basics of Bashand read through the Bash man page

Learn vim

Learn about SSH, basic password-free authentication method:

familiar with task management commands &,Ctrl-Z,Ctrl-C,jobs,fg,bg,kill commonly used in bash such as 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 -o, -A, –B used by Grep,egrep Software Installation Commands learn about Apt-get and Yum   cat -n to help display line numbers.

Experimental experience: The reference to command is the basic command, but also the flexibility to use the command. Especially in: LS grep

2. Some expressions:

· !! --Execute the last command again

· !$--The last word of the previous command

· {A.. b} {A,b,c} using effects in commands

· {$1-$9} command arguments when executing shell scripts

· $0--the name of the command being executed

· $#-Number of parameters

· $? --The execution return value of the previous command

· $$ the shell's process number

· $*– starts the shell script from $ start, so the parameters

Summary: Feel that these can only be used in the script, and now do not try to come back to learn to see

3. Daily use command:
      Ctrl-R
In bash, Ctrl-r is used to search in the history command
      Ctrl-W, Ctrl-U, Alt-BackSpace
Bash, Ctrl-w deletes the last word, Ctrl-u deletes the last line, alt-backspace a word in front of the delete cursor man readline contains a lot of default hotkey bindings in bash.
      cd -
Return to previous work path
      xargs
very powerful command. 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_function                  Cat hosts | xargs-l {} SSH [email protected]{} hostname
parallel
A more powerful command. Can be implemented in parallel tasks, and can split the input file, specify multiple nodes to run the command and other functions. Detailed functionality can be found in this link.
pstree -p
A powerful tool for playing with the process tree
pgrep,pkill
Use the name lookup process, or send a signal directly to a process that specifies a name.
  • Learn some of the signals that users can send to processes. For example kill -STOP [pid] , let the PID process hang.
nohup,disown,screen, tmux
These two commands are useful when you need to keep the process running in the background forever.
lsof, netstat -lntp
Query what process is currently listening on what port.
set
In the bash script, use the set -x get debug output to set -e get the error output.
;
a semicolon is used to open a child shell and to close after it has finished running. For example:
#在当前路径下执行一些命令 (Cd/some/other/dir; other-command) # The working path is still the current directory
Understanding multiple parameter expressions in the shell
${name:?error message}
Checks if a variable exists, if no output error message exists.
${var%suffix}, ${var#prefix}
output var variable except for prefix or suffix part. As the following code output is foo.txt.
var = foo.pdfecho ${var%pdf}.txt
<,>
Input output redirection operation.
some_command > LogFile 2>&1
will Some_command run the standard output during Output and standard error output to the file logfile.
man ASCII
get a good 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 Web page debugging, these commands can help you download the Web page code, which is useful.
lynx-dump-stdin
convert HTML to text
xmlstarlet
This command is useful when the
needs to process XML.
ssh-l, ssh-d
When you need to use a remote server to access a webpage, this command helps you establish an SSH tunnel between the remote server and your machine.
SSH connection optimization
The following configuration can help you avoid link loss, do not need to enter the Yes confirmation and link to the remote server each time, and enable compression in the link. It is recommended to put it in the. Ssh/config.
Tcpkeepalive=yesserveraliveinterval=15serveralivecountmax=6stricthostkeychecking=nocompression=yesforwardagent =yes
在正输入的命令前加#
The command has been entered in half, and suddenly changed his mind to run less,
You can use Alt-# the ' # ' in front of the command to turn the entire command into 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
when you accidentally need a large amount of output text, enter both actions 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
Learn about these sort commands
cut,paste, join
Learn about the maintenance tools for these text files. Many people forget to join after using cut
使用sort/uniq进行集合的交、并、补运算=
Suppose A and B are two text files, where the lines 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
使用LC_ALL=C
The settings for locale in Linux affect a large 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
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
制表符的输入
in Bash's 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 implement 16 binary extraction and binary editing operations.
strings,grep
can help find text in a binary file.
iconv,uconv
can help convert text encoding
split,csplit
you can separate files by size, and split them according to a specific pattern.
5 System Commissioning
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 (contains details of garbage collection).
mtr,traceroute
can help to find network problems, the former 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
Check for shared library conditions
gdb
Learn how to use GDB to connect to a running process and get its stack trace.
/proc/
It is very useful when doing the field debugging. Like /proc/cpuinfo, /proc/XXX/cwd, /proc/XXX/exe, /proc/XXX/fd/, /proc/XXX/smaps
sar
This command shows the history of CPU, memory, and network when it is necessary to determine why a system error occurred in the past.
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 unusual phenomena in the system, such as hardware or driver problems.

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.