Time-saving tips for every Linux user who should know about the command line

Source: Internet
Author: User
Tags garbage collection

Bole Online Guide : There are netizens in the question and answer site Quora asked: "What are the time-saving tips, is every Linux user should know?" "Joshua Levy usually works on the Linux platform, and he has accumulated a lot of practical command-line skills, and he picked out some of the answers." For technical users, these skills are important or practical, but not many people know. The following is a bit long, generally speaking, users do not need to understand all the content, but in order to achieve the time-saving convenience of the purpose, Joshua Levy still spared no effort to do proofreading, to ensure that each listed is worth reading, if you are a Linux heavy users.

In order to get more information about a command mentioned in this article, try "Man < command name >", in some cases, in order for the command to execute properly, you must install the appropriate package, either with aptitude or yum. If it fails, help Google.

Basic Article
    • Learn the basics of bash. In fact, reading the entire Bash Help Handbook is easy to understand and not too lengthy. Some of the other optional shell skins may be more beautiful, but bash is powerful and always available (learn zsh or tcsh in many cases you will be limited).
    • Learn vim, and for random edits under Linux, there are few tools to make it right (even if you spend most of your time using Emacs or Eclipse).
    • Learn about SSH through commands such as Ssh-agent,ssh-add, and skip the basics of password verification every time you log in.
    • Be familiar with the work management under bash: &,ctrl-z,ctrl-c,jobs,fg,bg,kill, etc.
    • Basic file management: LS and ls-l (specifically, learn the meaning of each column field listed in "Ls-l"), Less,head,tail,tail-f,ln,ln-s (Learn the difference between soft links and hard links), Chown,chmod, Du (Quick understanding of disk overall occupancy), Df,mount.
    • Basic Network Management commands: IP or Ifconfig,dig.
    • Learn about regular expressions, and the different command options for grep and Egrep, and -0,-a,-b are worth knowing.
    • Learn to use Apt-get or yum (depending on your release package) to find and install the packages you need.
Daily use of the article
    • use Ctrl-r to search the history of a command when using bash.
    • When using bash, use Ctrl-w to clear the last word and use ctrl-u to clear the entire line. You can view the man readline to get the binding settings for the default key in bash. A lot of content. Like alt-. (Note: The point) iterates over the parameters used in the previous command, alt-* expands the matching pattern of the parameters.
    • go back to the last working directory: CD-.
    • If you change your mind halfway through your command, you can use alt-#来在命令前面增加一个 # to make it a one-line comment (or use ctrl-a to go back to the beginning of the command and then type #). You can return it later by searching the history record.
    • uses Xargs (or parallel). It's very powerful. Note that you can control how many items are executed per row (-l) and how concurrency (-P) can be controlled. If you are not sure that it will work as you wish, use Xargs first. Furthermore,-l{} is useful. For example:
12 find -name \*.py | xargs grep some_function cat hosts | xargs -l{} ssh [email protected]{} hostname
    • The pstree-p can easily display the entire process tree.
    • Use Pgrep and Pkill to discover the process by name or to signal the process (the-f option is useful).
    • Learn about the kinds of signals you can send to a process. For example, to suspend a process, use kill-stop [process id]. Refer to man 7 signal for the entire list.
    • If you want a background process to run all the time, use Nohup or disown.
    • NETSTAT-LNTP to detect which processes are listening. You can also use lsof.
    • Bash script, use Set-x to debug the output. Use SET-E to terminate execution when there is an error. To make a strict output error, consider using Set-o pipefail (although this topic is somewhat complicated). For more complex scripts, you can also use a trap.
    • In a bash script, a child shell (written in parentheses) is a convenient way to organize commands. A very common example is moving to another working directory temporarily, for example:
123 #在当前目录下做一些事情cd/一些/另外的/目录;执行别的操作)#继续在原来的目录下执行
  • Note that there are many kinds of variable expressions in bash. Check if a variable exists: ${name:? error message}. For example, if a bash script requires a single variable, just write Input_file=${1:?usage: $ inpute_file}. Numeric extension: i=$ ({(i+1)%5}). Sequence: {1..10}. Collation of Strings: ${var%suffix} and ${var#prefix}. For example:
    1 ifvar==foo.pdf, then echo${var%.pdf}.txt   #会打印"foo.txt"。
  • With < (other directives), the output of a command can be treated as if it were the contents 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 <<eof ...
  • In bash, standard output and standard errors are redirected through other directives > log file 2>&1. It is common practice to add "</dev/null" to your current terminal in order to ensure that an instruction does not leave an open file descriptor for the standard input.
  • With man ASCII, you get a complete ASCII table with the corresponding 16-and 10-binary values.
  • When connecting to a remote terminal via SSH, use screen or Dtach to maintain your session and prevent interruption. In SSH, it is useful to know how to use the-l or-D option (and sometimes the-R), for example, if you access a Web page from a remote server.
  • Optimizing your SSH options may also work. For example, the following. Ssh/config content in some network environments can prevent the connection off the line, when connected to the new host does not need to reconfirm, jump authentication, and also use compression (for some low-bandwidth connection environment in the use of SCP will be helpful).
123456 TCPKeepAlive=yesServerAliveInterval=15ServerAliveCountMax=6StrictHostKeyChecking=noCompression=yesForwardAgent=yes
Data Processing article
    • turn HTML into text: Lynx-dump standard input
    • if you want to work with Xml,xmlstarlet, it's great.
    • is handy for Amazon S3,s3cmd (though immature, there may be some less-than-good features).
    • Learn about sort and uniq (including the-U and-D options for Uniq).
    • understand Cut,paste,join to manipulate text files. Many people use cut but forget to join.
    • It is very convenient to use Sort/uniq when you want to do the addition, subtraction, and subtraction of collections between files. If A and B are two of the already-heavy text files, then the operations will be fast and can be performed between files of any size, even to gigabytes of bytes. (Sort is not limited by memory, but if/tmp is in a very small root partition, you may need to use the-t option)
123 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
    • Understanding localization affects the work of many command lines, including the order and performance of sorting. Most Linux installers set lang or some other localized variable to a local setting similar to American English. This makes sort and some other commands much slower to run. (Note that even if you use UTF-8 encoded text, you can still safely sort through the ASCII code order, which is a lot of use) to avoid i18n slowing down everyday work, using the traditional byte-based sort order, using the export lc_all=c (in fact, Consider adding in your. bashrc).
    • Learn the basic awk and SED commands to do simple data processing. For example, sum the numbers in the third column of a text file: awk ' {x + = $ $} END {print x} '. This is probably three times times faster than the equivalent Python speed and the code length is 3 times times shorter.
    • Replace a string in place in all occurrences of the file.
1 perl -pi.bak -e ‘s/old-string/new-string/g‘my-files-*.txt
    • Use Shuf to randomly shuffle a line in a file or select a random line.
    • Learn about the various options for sort. Know how the key value works. In particular, when you want to use-K1, pay extra attention to: 1 Sorts only the first field,-k1 means sort by the entire row.
    • Stable sorting (sort-s) may be useful. For example, if you sort by the second field, and then sort by the first field, you can use a sort-k1,1 | Sort-s-k2,2
    • If you need to write a tab key in the command line in bash, press CTRL + V, <tab> or $ ' t ' (the latter is better because you can copy, paste).
    • For binary files, use HD for simple export 16 binary notation or binary editing with BVI.
    • For binary files, strings (and grep, etc.) allows you to find the byte bits (0101) of the file. To make a file, you can try the iconv, or if you want to use a more advanced usage, try Uconv, which can support some advanced Unicode things. For example, this command can have the accent lowercase and removed (by extending and discarding):
1 uconv -f utf-8 -t utf-8 -x ‘::Any-Lower; ::Any-NFD; [:Nonspacing Mark:] >; ::Any-NFC; ‘< input.txt > output.txt
    • To slice a file, you can try split (by size) or csplit (slice by pattern).
System Debug Chapter
  • For Web debugging, curl and curl-l are useful, as well as the same part of wget.
  • If you want to understand the state of a disk/cpu/network, you can use Iostat,netstat,top (better, with Htop), and (in particular) Dstat, which is very convenient for you to quickly understand what is happening in your system. If you want to know the current state of the memory, you can use free and vmstat, as well as understand what each output means. In particular, you need to know that the value of "cached" is the size of the space that the Linux kernel retains to make the file cache, so the actual available memory is the corresponding value of the "free" item.
  • Java's system debugging is completely different, but there is a simple trick on sun and other JVMs that you can run kill-3 <pid> to get a complete stack call trajectory and the aggregate usage of the heap (including the resulting garbage collection details, The bread here contains a lot of information) and will be directed to standard errors or logs.
  • Use MTR as a better network trace to identify problems with the network.
  • To see if a disk is full, NCDU is faster than the general "Du-sk *".
  • To see which sockets or processes are taking up bandwidth, try Iftop or netlogs.
  • The AB tool (released with Apache's installation package) is useful for detecting the performance of a network server, and for more complex stress tests, siege can be tested. For more serious network problems debugging, try Wireshark or Tshark. Learn about Strace and Ltrace. This can be helpful when a program suddenly fails, hangs, or crashes, and you are overwhelmed, or if you want to know the overall performance of the program. You can note the-C and-P options.
  • Learn some questions about using LDD to check shared library functions.
  • Learn how to use GDB to connect to a running program and get its call stack.
  • Use/proc. It will be very helpful for on-site commissioning. For example:/proc/cpuinfo,/PROC/XXX/CWD,/proc/xxx/exe,/proc/xxx/fd/,/proc/xxx/smaps.
  • When you want to debug a problem that has occurred over a period of time, the SAR can be useful to display statistics about the CPU, memory, and network over time.
  • For deeper system performance optimizations, 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).

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.