Common Command Mastery on Linux

Source: Internet
Author: User
Tags ftp file

Http://coolshell.cn/articles/8883.html

This article comes from a question and answer by Quroa "What is some time-saving tips that every Linux user should know?"--linux users have the skills to be aware of how to improve efficiency. I feel very good, summed up better, turn it over, and add some of their own understanding. First of all, I want to tell you that under Unix/linux, the most efficient technique is not manipulating the graphical interface, but command-line operations, because the command line means Automation . If you've seen the shell you probably don't know and the 28 unix/linux command-line artifact, you'll know how powerful Linux is, and it's all coming from the command line, so even if you don't know how to be an environmentalist programmer, at least they can make you stay a little less night, This is beneficial to your physical health and sexual life. Here is a somewhat long list, as the author says, you don't need to know all of these things, but if you're still heavily using Linux, these things are worth looking at. (Note: If you want to know more about the use of the commands involved below, you must have a man point.) For some commands, you may need to install Yum or Apt-get first, and if you have any questions, don't forget about Google. If you want to Baidu, I only represent all the creatures on this planet, including microbes and even bacteria viruses and cockroaches BS you to the Cosmic destruction)

Basis
    • Learn Bash . You can see Bash's stuff in man bash, not complicated or long. You can do it with another shell, but bash is powerful and system default. (Learning zsh or Tsch will only allow you to be limited in many cases)
    • learn vim . Under Linux, there's basically nothing to compete with (even if you're a heavy user of Emacs or Eclipse). You can look at the concise Vim Guide and Vim's adventure game, as well as the Vim card for programmers, and the "turn vim into a programming IDE" and so on.
    • learn about SSH. Understand user authentication without password (through ssh-agent, Ssh-add), learn to use SSHFQ, use SCP instead of FTP file, etc. Do you know? At the far end of the SCP, you can press the TAB key to view the remote directory and files (and, of course, user authentication without a password), which is a credit to bash.

    • familiar with Bash's job management , such as: &, Ctrl-z, ctrl-c, Jobs, FG, BG, kill, etc. Of course, you also need to know the difference between ctrl+\ (sigquit) and CTRL + C (SIGINT).
    • Simple File Management : LS and ls-l (you'd better know what each column of "ls-l" means), less, head, tail and tail-f, ln and ln-s (you know the difference between hard link and soft link and pros and cons), Chown, chmod, Du (if you want to see the size of the disk Du-sk *), DF, Mount. Of course, the original author forgot the Find command.
    • Basic Network Management : IP or ifconfig, dig. Of course, the original author also forgot such as netstat, ping, traceroute, etc.
    • Understanding Regular Expressions , there are various options for GREP/EGREP. For example:-O,-A, and-B these options are well worth knowing.
    • Learn to use Apt-get and Yum to find and install software (the former classic bundle is Ubuntu, the latter's classic bundle is Redhat), and I recommend that you try to install the software from the source code.

Daily

    • In bash, use Ctrl-r instead of up and down cursor keys to find historical commands.
    • In bash, use ctrl-w to delete the last word, and use ctrl-u to delete a row. Ask man bash to find the ReadLine key bindings section to see the default hotkey for bash, such as: alt-. The last parameter of the previous command is typed, and alt-* lists the commands you can enter.
    • Go back to the last working directory: cd– (back home is CD ~)
    • Use Xargs. This is a very powerful command. You can use-l to limit how many commands you have, or you can specify the number of concurrent processes with-p. If you don't know what your order will become, you can use xargs echo to see what it's like. Of course,-i{} is also very useful. Example:
123 find. -name \*.py | xargs grep some_function cat hosts | xargs -I{} ssh[email protected]{} hostname
    • Pstree-p can help you show the process tree. (The person who read my article "a fork in the face question" should be not unfamiliar)
    • Use Pgrep and pkill to find or kill a process of a name. (The-f option is useful).
    • Understand the signals that can be sent to the process. For example: To suspend a process, use Kill-stop [PID]. Use the man 7 signal to view a variety of signals and use kill-l to view the numbers and signals corresponding tables
    • Use Nohup or disown if you want to let a process run in the background.
    • Use NETSTAT-LNTP to see if there is a process listening on a port on the network. Of course, you can also use lsof.
    • In bash scripts, you can use Set-x to debug the output. Use SET-E to abort execution when an error occurs. Consider using set-o pipefail to limit errors. Traps can also be used to intercept signals (such as intercepting CTRL + C).
    • In bash scripts, Subshells (written in parentheses) is a handy way to combine commands. A common example is to temporarily go to another directory, for example:
123 # do something in current dir(cd/some/other/dir; other-command)# continue in original dir
    • In bash, be aware that there are a lot of variables unfolding there. For example: Check if a variable exists: ${name:?error message}. If a bash script requires a parameter, perhaps it is an expression input_file=${1:?usage: $ input_file}. A calculation expression: i=$ (((i + 1)% 5)). A sequence: {1..10}. Truncate a string: ${var%suffix} and ${var#prefix}. Example: If var=foo.pdf, then echo ${var%.pdf}.txt prints "foo.txt".
    • By < Some command can be used as a file. Example: Compare a local file with a remote file/etc/hosts:diff/etc/hosts < (ssh somehost cat/etc/hosts)
    • Learn what "here documents" is, something like cat <<eof.
    • In bash, use redirection to standard output and standard errors. such as: Some-command >logfile 2>&1. In addition, to confirm that a command does not redirect an open file handle to standard input, the best practice is to add "</dev/null" and redirect/dev/null to standard input.
    • Use man ASCII to view the ASCII table.
    • In the remote SSH session, use screen or Dtach to save your session. (See the command-line artifact of the 28 unix/linux)
    • To debug the Web, try Curl and curl-i or wget. I think the debug Web is Firebug,curl and wget is used to catch the Web, hehe.
    • Turn HTML into text: Lynx-dump-stdin
    • If you want to work with XML, use Xmlstarlet
    • For Amazon S3, S3cmd is a handy command (and a little immature)
    • In SSH, know how to use the SSH tunnel. Through-l or-d (and-R), FQ artifact.
    • You can also do some optimizations for your SSH. For example,. Ssh/config contains some configurations: avoid links being discarded, link to new host without confirmation, forward authentication, previously using compression (if you want to use SCP to pass files):
123456 TCPKeepAlive=yesServerAliveInterval=15ServerAliveCountMax=6StrictHostKeyChecking=noCompression=yesForwardAgent=yes
    • If you lose a command line, but you change your mind, but you don't want to delete it, because you want to find it in the history command, but you don't want to do it. So, you can press alt-#, so this command is added with a # character, so it is commented out.

Data processing

    • Learn about the sort and Uniq commands (including the-U and-D options for Uniq).
    • Learn to manipulate text files with cut, paste, and join commands. Many people forget to use join before cut.
    • If you know how to use Sort/uniq to do set intersection, set, and difference set can greatly promote your work efficiency. Assuming that two text files A and B have been uniq, then using Sort/uniq will be the quickest way, no matter how large the two files are (sort will not be limited by memory, you can even use the-t option if your/tmp directory is small)
12345 cat a B | sort | uniq > c   # C is a union B and set  cat a B | sort | uniq -d > c   # C is a intersect B intersection  cat a b b | sort | uniq -u > c   # c is set difference A-b difference set
    • Learn about the command-line tools associated with character sets, including sorting and performance. Many Linux installers will set lang or other environment variables related to character sets. These things can make some commands (such as sort) perform slower than n times (note: Even if you encode text files with UTF-8, you can use ASCII to sort them very safely). If you want to disable that i18n and use the traditional byte-based sorting method, set the export lc_all=c (in fact, you can put it in. bashrc). If this variable is set, your sort command is likely to be wrong.
    • Learn about awk and SED and use them to do some simple data modification operations. For example: the sum of the numbers in the third column: awk ' {x + = $ +} END {print x} '. This could be 3 times times faster than Python and three times times less than Python's code.
    • Use Shuf to disrupt a row in a file or select a random line in a file.
    • Learn about the options for the sort command. Learn what key is (-T and-K). Specifically, you can use-k1,1 to sort the first column,-k1 to sort the whole row.
    • Stable sort (sort-s) can be useful. For example: If you want to sort two cases, first in the second column, then in the first column, then you can: sort-k1,1 | Sort-s-k2,2
    • We know that under the Bash command line, the TAB key is used to do the automatic completion of the directory file. But if you want to enter a tab character (for example: you want to enter the <tab> character after the SORT-T option), you can enter the <tab> character by pressing CTRL-V and then pressing the TAB key. Of course, you can also use $ ' t '.
    • If you want to view binary files, you can use the HD command (under CentOS is the Hexdump command), if you want to compile binaries, you can use the BVI command (http://bvi.sourceforge.net/Wall)
    • In addition, for binary files, you can use strings (with grep, etc.) to view the text in the binary.
    • For text file transcoding, you can try Iconv. Or try a stronger uconv command (this command supports more advanced Unicode encoding)
    • If you want to separate a large file, you can use the split command (split by size) and the Csplit command (split by a pattern).

System Commissioning

    • If you want to know the disk, CPU, or network status, you can use Iostat, netstat, top (or better htop), and Dstat commands. You can quickly know what's going on with your system. On this order, there are iftop, iotop, etc. (see the 28 unix/linux command-line artifact)
    • To understand the state of the memory, you can use the free and Vmstat commands. Specifically, you need to be aware of the "cached" value, which is the memory used by the Linux kernel. There is also a value for free.
    • Java system Monitoring There is a small trick is that you can use kill-3 <pid> send a sigquit signal to the JVM, you can dump the stack information (including garbage collected information) to Stderr/logs.
    • Using MTR will make it easier to locate a network problem than using Traceroute.
    • If you want to find out which socket or process is using network bandwidth, you can use Iftop or nethogs.
    • An Apache called AB tool is a useful and quick-and-dirty way to test the performance load of a Web server. If you need more complex tests, you can try siege.
    • If you want to grab a network bag, try Wireshark or Tshark.
    • Learn about Strace and Ltrace. These two commands allow you to view the system calls of the process, which helps you to analyze where the hang of the process is, how crash and failed. You can also use it for performance profile, using the-C option, you can use the-P option to attach any process.
    • Learn to check related dynamic link libraries with the LDD command. Note: Security issues with LDD
    • Use GDB to debug a running process or analyze a core dump file. See my written "several debugging methods that should be known in GDB"
    • Learn to view information in the/proc directory. This is a running statistic and information for the entire operating system recorded by the Linux kernel runtime, such as:/proc/cpuinfo,/PROC/XXX/CWD,/proc/xxx/exe,/proc/xxx/fd/,/proc/xxx/smaps.
    • The SAR command is useful if you are debugging something and why it is wrong. It allows you to look at the statistics of CPU, memory, network, etc.
    • Use DMESG to view information or problems with some hardware or drivers.

The author finally added a disclaimer: disclaimer:just because you can do something in bash, doesn ' t necessarily mean for you should. ;) (End of full text)

Common Command Mastery on Linux

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.