Command line saving skills that every Linux user should understand

Source: Internet
Author: User
Tags html to text
Some netizens asked questions on the Q & A website Quora: what time-saving tips should every Linux user know? JoshuaLevy usually works on the Linux platform, and he has accumulated a lot of practical command line skills, he selects a part in the response. For technical users, these Linux command Linux skills

Some netizens asked on the Q & A website Quora: "What time-saving tips should every Linux user know ?" Joshua Levy usually works on the Linux platform, and he has accumulated a lot of practical command line skills, he selects a part in the response. For technical users, these skills are very important or practical, but not many people know about them. The next article is a little long. Generally, users do not need to understand all the content. however, to save time and convenience, Joshua Levy has spared no efforts to proofread the content, to ensure that each of the listed items is worth reading, the premise is that you are a heavy user of Linux.

To obtain more information about a command mentioned in this article, first try "man" <命令名称> ", In some cases, to make this command run properly, you must install the corresponding package, you can use aptitude or yum. If it fails, ask Google.

Basics
  • Learn the basic Bash. In fact, read the entire bash help manual. it is easy to understand and not long enough. Some other optional shell may look more beautiful, but bash is powerful and always usable (you may be limited by zsh or tcsh in many cases ).
  • To learn vim, there are almost no tools available for random editing in Linux (even if you are using Emacs or Eclipse most of the time ).
  • Understand ssh and how to skip password verification during each login. use commands such as ssh-agent and ssh-add.
  • Familiar with bash job management: &, Ctrl-Z, Ctrl-C, jobs, fg, bg, kill, and so on.
  • Basic file management: ls and ls-l (in particular, learn the meaning of each field listed in "ls-l"), less, head, tail, tail-f, ln, ln-s (learning the differences between soft links and hard links), chown, chmod, du (quick understanding of the overall disk usage), df, and mount.
  • Basic network management commands: ip, ifconfig, and dig.
  • Learn about regular expressions and different command options of grep and egrep,-0,-A, and-B.
  • Learn to use apt-get or yum (depending on your release package) to find and install the package you need.
Daily use
  • When bash is used, Ctrl-R is used to search for command history.
  • When bash is used, Ctrl-W is used to clear the last word and Ctrl-U is used to clear the entire line. You can view man readline to get the binding settings of the default key in bash. A lot of content. For example, Alt-. (Note: point) traverses the parameters used in the previous command. Alt-* extends the parameter matching mode.
  • Go back to the last working directory: cd -.
  • If you change your mind when typing half of your command, you can use Alt-# to add A # in front of the command to make it A line of comment (or use Ctrl-A to return to the beginning of the command, then type #). You can then search for the historical records.
  • Use xargs (or parallel ). It is very powerful. Note that you can control the number of items executed in each row (-L) and the concurrency (-P ). If you are not sure that it will work as you wish, use xargs first. Furthermore,-l {} is useful. For example:
find . -name \*.py | xargs grep some_functioncat hosts | xargs -l{} ssh root@{} hostname
  • Pstree-p can easily display the entire process tree.
  • Use pgrep and pkill to find a process by name or send a signal to the process (the-f option will be useful ).
  • Learn about the types of signals you can send to processes. For example, to suspend a process, use kill-STOP [process ID]. For more information about the entire list, see man 7 signal.
  • If you want to keep a background process running, use nohup or disown.
  • Netstat-lntp is used to detect which processes are listening. You can also use lsof.
  • In the bash script, use set-x to debug the output. Use set-e to terminate execution when an error occurs. To strictly output errors, consider using set-o pipefail (although this topic is somewhat complicated ). You can also use trap for more complex scripts.
  • In bash scripts, a sub-shell (written in parentheses) is a convenient method for organizing commands. A common example is to temporarily move to another working directory, for example:
# Do something in the current directory (cd/some/other/directories; execute other operations) # continue to execute in the original directory
  • Note that bash has many variable expressions. Check whether a variable exists: $ {name :? Error message }. For example, if a bash script requires a single variable, you only need to write input_file =$ {1 :? Usage: $0 inpute_file }. Numeric extension: I =$ ({(I + 1) % 5 }). Sequence: {1 .. 10 }. String sorting: $ {var % suffix} and $ {var # prefix }. For example:
    If varnames contain foodies, then echo returns invalid varnames .w.20..txt # "foo.txt" is printed ".
  • With <(other commands), the output of a command can be treated as the content of a file. For example, to compare local and remote/etc/hosts files, you can use diff/etc/hosts <(ssh [remote host] cat/etc/hosts ).
  • Learn about "here documents" in bash, such as cat <
  • In bash, use other commands> log file 2> & 1 to redirect standard output and standard errors. To ensure that an instruction does not leave an open file descriptor for the standard input, output it to your current terminal and add
  • Man ascii can be used to obtain a complete ASCII table with the corresponding hexadecimal and 10-hexadecimal values.
  • When you connect to a remote terminal through ssh, use screen or dtach to maintain your session to prevent interruption. In ssh, it is useful to know how to use the-L or-D option (sometimes using-R). For example, if you access a webpage from a remote server.
  • Optimizing Your SSH options may also work. For example, the following. the ssh/config content can prevent connection disconnection in some network environments. when connecting to a new host, you do not need to confirm it again, compression is also used (it will be helpful when scp is used in some low-bandwidth connection environments ).
TCPKeepAlive=yesServerAliveInterval=15ServerAliveCountMax=6StrictHostKeyChecking=noCompression=yesForwardAgent=yes
Data processing
  • Convert HTML to text: standard lynx-dump input
  • Xmlstarlet is great if you want to process XML.
  • For Amazon S3, s3cmd is very convenient (although not quite mature, there may be some bad features ).
  • Understand sort and uniq (including uniq-u and-d options ).
  • Understand cut, paste, and join operations on text files. Many people use cut, but forget to have join.
  • It is very convenient to use sort/uniq when you want to add, subtract, and perform difference operations between files. If a and B are two de-duplicated text files, the calculation will be very fast, and operations can be performed between files of any size, or even to the size of GB. (Sort is not limited by memory, but if/tmp is in a small root partition, you may need to use the-T option)
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   # c is set difference a - b
  • Understanding localization affects the work of many command lines, including sorting order and performance. In most linux installation packages, LANG or other local variables are set as a local setting similar to that in American English. This slows down sort and other commands. (Note that even if you are using UTF-8-encoded text, you can still rest assured to sort by ASCII code order, which is a lot of use) to avoid i18n dragging down everyday work, use the traditional byte-based sorting order, and use export LC_ALL = C (in fact, consider in your. bashrc ).
  • Understand basic AWK and sed commands for simple data processing. For example, sum the number in the third column of a text file: awk '{x + = $3} END {print x }'. This is about three times faster than the same python, and the code length is also three times shorter.
  • Replace a string in all the places where it appears in all files.
perl -pi.bak -e 's/old-string/new-string/g' my-files-*.txt
  • Use shuf to randomly disrupt the rows in a file or select a random row.
  • Understand various sort options. Know how the key value works. In particular, when you want to use-k1, pay special attention: 1 only sorts the first field,-k1 means sorting by the entire row.
  • Stable sorting (sort-s) may be useful. For example, you can use sort-k1, 1 | sort-s-k2, 2
  • If you need to write the literal value of a tab key in the bash command line, press Ctrl + V, Or $ '\ t' (the latter is better, because you can copy and paste it ).
  • For binary files, use hd for simple export of hexadecimal representation or use bvi for binary editing.
  • For binary files, strings (and grep) allows you to find the file's byte (0101 ). to edit a file, you can try iconv, or if you want to use more advanced usage, try uconv, which supports some advanced Unicode tasks. For example, this command can be used to lower-case the stress and remove it (through expansion and loss ):
uconv -f utf-8 -t utf-8 -x '::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ' < input.txt > output.txt
  • To slice a file, try split (split by size) or csplit (split by mode ).
System debugging
  • For web Debugging, curl and curl-l are useful, and the same features as wget.
  • If you want to know the disk/cpu/network status, you can use iostat, netstat, top (better, use htop), and (especially) dstat, it is very convenient to quickly understand what is happening in the system. If you want to know the current memory status, you can use free and vmstat and understand the meaning of each output. It is particularly worth noting that the value of "cached" is the size of the space reserved by the Linux kernel for file caching, so the real available valid memory is the corresponding value of the "free" item.
  • Java system debugging is completely another thing, but there is a simple technique on Sun and other JVMs, that is, you can run kill-3 To get a complete stack call track and overall usage of the stack (including the garbage collection details, which contains a lot of information), will be directed to standard errors or logs.
  • Use mtr for better network tracking and identify network problems.
  • To check whether a disk is full, ncdu is faster than the commonly used "du-sk.
  • To check which sockets or processes are using the bandwidth, try iftop or netlogs.
  • The AB tool (released along with the apache installation package) is helpful for detecting the performance of network servers. for more complex stress tests, try siege. For debugging of more serious network problems, try wireshark or tshark. Learn about strace and ltrace. This is helpful when a program suddenly fails, crashes, or crashes, but you are overwhelmed, or you want to know the overall performance of the program. Pay attention to the-c and-p options.
  • Measure the test taker's knowledge about how to use ldd to check shared library functions.
  • Learn how to use gdb to connect to a running program and obtain its call stack.
  • Use/proc. it will be helpful for on-site debugging problems. For example:/proc/cpuinfo,/proc/xxx/cwd,/proc/xxx/exe,/proc/xxx/fd/,/proc/xxx/smaps.
  • Sar is useful when debugging problems that occurred in the past period of time. it can display statistics on CPU, memory, and network in the past period of time.
  • For deeper system performance optimization, you can focus on stap (systemtap) or perf.
  • When there are some very strange problems, you can try dmesg (such as hardware or driver problems ).

Original article link: Joshua Levy translation: Bole online-Gao Lei

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.