High Efficiency Tips for Linux users

Source: Internet
Author: User
Tags html to text
In UnixLinux, the most efficient technique is not to operate the graphic interface, but to operate on the command line, because the command line means automation. In bash, you can use Ctrl-R instead of the upper/lower cursor key to search for historical commands. In bash, use Ctrl-W to delete the last

In Unix/Linux, the most efficient technique is not to operate the graphic interface, but to operate on the command line, because the command line means automation.

Daily
  • In bash, use Ctrl-R instead of the upper/lower cursor key to search for historical commands.
    • In bash, Ctrl-W is used to delete the last word and Ctrl-U is used to delete a row. Please search for the Readline Key Bindings section after man bash to see the default hotkeys of bash, such as Alt -. name the last parameter of the last command, while Alt-* lists the commands you can enter.
      • Go back to the last working directory: cd? (Back to home is cd ~)
        • Use xargs. This is a very powerful command. You can use-L to limit the number of commands, or use-P to specify the number of parallel processes. If you don't know what your command will look like, you can use xargs echo to see what it will look like. Of course,-I {} is also very useful. Example:
          find. -name \*.py | xargsgrep some_function cathosts | xargs-I{} sshroot@{} hostname

          Pstree-p can help you display the process tree.
          • Use pgrep and pkill to locate or kill a process with a name. (The-f option is useful ).
            • Understand the signals that can be sent to processes. For example, to suspend a process, use kill-STOP [pid]. use man 7 signal to view various signals, and use kill-l to view corresponding tables of numbers and signals.
              • Use nohup or disown if you want to run a process in the background.
                • Use netstat-lntp to check the processes listening to a port on the network. You can also use lsof.
                  • In the bash script, you can use set-x to debug the output. Use set-e to execute abort when an error occurs. Use set-o pipefail to limit errors. You can also use trap to intercept signals (for example, ctrl + c ).
                    • In the bash script, subshells (written in parentheses) is a convenient way to combine some commands. A common example is to temporarily access another directory, for example:
                      (cd/some/other/dir; other-command)# continue in original dir
                      • In bash, note that there are many variables to expand. For example, check whether a variable exists: $ {name :? Error message }. If a bash script requires a parameter, it may be such an expression input_file =$ {1 :? Usage: $0 input_file }. A calculated expression: I =$ (I + 1) % 5 )). A sequence: {1... 10 }. Truncates a string: $ {var % suffix} and $ {var # prefix }. Example: if var=foo.txt, then echo has been var%.pdf}.txt prints has foo.txt ".
                        • You can use <(some command) to treat a command as a file. Example: compare a local file with a remote file/etc/hosts: diff/etc/hosts <(ssh somehost cat/etc/hosts)
                          • What is "here documents", such as cat <
                          • In bash, use redirection to standard output and standard errors. For example, some-command> logfile 2> & 1. In addition, to ensure that a command does not redirect A opened file handle to the standard input, the best practice is to add"
                          • Use man ascii to view ASCII tables.
                            • In a remote ssh session, use screen or dtach to save your session.
                              • To debug the Web, try curl, curl-I, or wget. I think the powerful tool of debug Web is firebug. curl and wget are used to capture webpages.
                                • Convert HTML to text: lynx-dump-stdin
                                  • If you want to process XML, use xmlstarlet
                                    • For Amazon S3, s3cmd is a convenient command (somewhat immature)
                                      • In ssh, you know how to use the ssh tunnel. Use-L or-D (and-R) to flip the walls.
                                        • You can also optimize your ssh. For example ,. ssh/config includes some configurations: to avoid link discarding, you do not need to confirm the connection to the new host, forward authentication, and use compression before (if you want to use scp to transfer files ):
                                          TCPKeepAlive=yesServerAliveInterval=15ServerAliveCountMax=6StrictHostKeyChecking=noCompression=yesForwardAgent=yes
                                          • If you have a command line, but you have changed your note, but you do not want to delete it, because you have to find it in the history command, but you do not want to execute it. Then, you can press Alt-#, so the command is shut down with a # character, so it is commented out. Data processing
                                            • Understand sort and uniq commands (including uniq's-u and-d options ).
                                              • Understand how to use cut, paste, and join commands to operate text files. Many people forget to use join before cut.
                                                • If you know how to use sort/uniq for set intersection, union, and difference set, it can greatly improve your work efficiency. Assume that two text files a and B have been decrypted by uniq, sort/uniq will be the fastest way, no matter how large these two files are (sort will not be limited by memory, you can even use the-T option, if your/tmp directory is small)
                                                  Cata B | sort | uniq> c # c is a union B union cata B | sort | uniq-d> c # c is a intersect B intersection cata B | sort | uniq- u> c # c is set difference a-B difference set

                                                  • Understand character set-related command line tools, including sorting and performance. Many Linux installers set LANG or other environment variables related to character sets. These things may slow the execution performance of some commands (such as: sort) N multiple times (note: even if you encode a text file with a UTF-8, you can also use ASCII to sort it safely ). If you want to Disable the i18n and use the traditional byte-based sorting method, set export LC_ALL = C (in fact, you can put it in. bashrc ). If this variable is set, your sort command may be wrong.
                                                    • Understand awk and sed and use them to perform some simple data modification operations. For example, calculate the sum of the numbers in the third column: awk '{x + = $3} END {print x }'. This may be three times faster than Python and three times less than Python code.
                                                      • Use shuf to disrupt the rows in a file or select a random row in the file.
                                                        • Understand the sort command options. Understand what the key is (-t and-k ). Specifically, you can use-k1 and 1 to sort the first column and-k1 to sort the entire row.
                                                          • Stable sort (sort-s) is useful. For example, if you want to sort the two cases first in the second column and then in the first column, you can do this: sort-k1, 1 | sort-s-k2, 2
                                                            • We know that in the bash command line, the Tab key is used to automatically complete directory files. However, if you want to enter a Tab character (for example, you want to enter it after the sort-t option) Character), you can press Ctrl-V first, and then press the Tab key, you can enter Character. You can also use $ '\ t '.
                                                              • If you want to view the binary file, you can use the hd command (hexdump command in CentOS). If you want to compile the binary file, you can use the bvi command
                                                                • In addition, for binary files, you can use strings (with grep and so on) to view the binary text.
                                                                  • 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 by size command and the csplit by a pattern command ). System debugging
                                                                      • 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 happened to your system. For such commands, see iftop and iotop.
                                                                        • To understand the memory status, you can use the free and vmstat commands. Specifically, pay attention to the value of "cached", which is the memory occupied by the Linux kernel. There are also free values.
                                                                          • One small technique in Java system monitoring is that you can use kill-3 Send a SIGQUIT signal to JVM to dump stack information (including garbage collection information) to stderr/logs.
                                                                            • Using mtr makes it easier to locate a network problem than using traceroute.
                                                                              • If you want to find the socket or process that is using network bandwidth, you can use iftop or nethogs.
                                                                                • An Apache tool called AB is very useful. it uses quick-and-dirty to test the performance load of website servers. If you need more complex tests, you can try siege.
                                                                                  • If you want to capture the network package, 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 analyze where the hang of the process is, how crash and failed. You can also use it for the performance profile. with The-c option, you can use the-p option to attach any process.
                                                                                      • Measure the test taker's knowledge about how to use the ldd command to check related dynamic link libraries.
                                                                                        • Use gdb to debug a running process or analyze the core dump file.
                                                                                          • Learn to view information in the/proc directory. This is the operating statistics and information of the entire operating system recorded during Linux kernel running, such as:/proc/cpuinfo,/proc/xxx/cwd,/proc/xxx/exe, /proc/xxx/fd/,/proc/xxx/smaps.
                                                                                            • The sar Command is useful if you debug why something fails. It allows you to view statistics about CPU, memory, network, and so on.
                                                                                              • Use dmesg to view some hardware or driver information or problems.

                                                                                              • Efficient commands

                                                                                                • 'Alt +. 'or' .'
                                                                                                  Hot alt +. or esc +. you can repeat the parameters of the last command line.
                                                                                                  • ^ Old ^ new
                                                                                                    Replace some strings in the previous command.
                                                                                                    Scenario: echo "wandreful", in fact, is to output echo "wonderful ". You only need ^ a ^ o, which is helpful for spelling long commands. (Chen Hao note: You can also use it !! : Gs/old/new)
                                                                                                    • Du-s * | sort-n | tail
                                                                                                      List the top 10 files in the current directory.
                                                                                                      • : W! Sudo tee %
                                                                                                        Save a file that can only be written by root in vi
                                                                                                        • Date-d @ 1234567890
                                                                                                          Time cut-off time
                                                                                                          • > File.txt
                                                                                                            Create an empty file, which is shorter than touch.
                                                                                                            • Mtr coolshell.cn
                                                                                                              Mtr commands are better than traceroute commands.
                                                                                                              • Add a space before the command line. The command will not enter history.
                                                                                                                • Echo "ls-l" | at midnight
                                                                                                                  Run a command at a certain time.
                                                                                                                  • Curl-u user: pass-d status = "Tweeting from the shell" http://twitter.com/statuses/update.xml
                                                                                                                    Use the command line method to update twitter.
                                                                                                                    • Curl-u username? Silent "https://mail.google.com/mail/feed/atom” | perl-ne 'print" \ t "if/ /; Print "$2 \ n" if/<(title | name)> (. *) <\/\ 1> /;'
                                                                                                                      Check your gmail unread emails
                                                                                                                      • Ps aux | sort-nk + 4 | tail
                                                                                                                        List the top 10 processes that consume the most memory
                                                                                                                        • Man ascii
                                                                                                                          Displays the ascii code table.
                                                                                                                          Scenario: Do I still need google when I forget the ascii code table? Especially when the Tianchao network is so "smooth", it is more troublesome to apply the rules once in GWF. use local man ascii.
                                                                                                                          • Ctrl-x e
                                                                                                                            Quickly start your default EDITOR (set by the variable $ EDITOR ).
                                                                                                                            • Netstat? Tlnp
                                                                                                                              Lists the port numbers listened to by local processes. (Chen Hao note: netstat-anop can display the process listening on this port number)
                                                                                                                              • Tail-f/path/to/file. log | sed '/^ Finished: SUCCESS $/q'
                                                                                                                                When Finished: SUCCESS occurs in file. log, tail is exited. This command is used to monitor and filter logs in real time.
                                                                                                                                • Ssh user @ server bash </path/to/local/script. sh
                                                                                                                                  Run a script on a remote machine. The biggest advantage of this command is that you do not need to copy the script to a remote machine.
                                                                                                                                  • Ssh user @ host cat/path/to/remotefile | diff/path/to/localfile-
                                                                                                                                    Compare a remote file with a local file
                                                                                                                                    • Net rpc shutdown-I ipAddressOfWindowsPC-U username % password
                                                                                                                                      Remotely shut down a Windows machine
                                                                                                                                      • Screen-d-m-S some_name ping my_router
                                                                                                                                        The background runs a program that is not terminated and can view its status at any time. The-d-m parameter starts the "separation" mode, and-S specifies the ID of a session. You can run the-R command to re-mount the session of an identifier. For more details, see screen usage man screen.
                                                                                                                                        • Wget -- random-wait-r-p-e robots = off-U mozilla http://www.example.com.
                                                                                                                                          Download the entire www.example.com website. (Note: do not go too far. most websites have anti-crawling functions :))
                                                                                                                                          • Curl ifconfig. me
                                                                                                                                            When your machine is on the intranet, you can use this command to view the internet IP address.
                                                                                                                                            • Convert input.png-gravity NorthWest-background transparent-extent 720x200 output.png
                                                                                                                                              Change the size of a piece
                                                                                                                                              • Lsof? I
                                                                                                                                                View the activity status of the local network service in real time.
                                                                                                                                                • Vim scp: // username @ host // path/to/somefile
                                                                                                                                                  Vim remote file
                                                                                                                                                  • Python-m SimpleHTTPServer
                                                                                                                                                    To implement an HTTP service in one sentence and set the current directory as the HTTP service directory, you can access it through http: // localhost: 8000. This may be the simplest HTTP server implementation on the planet.
                                                                                                                                                    • History | awk '{CMD [$2] ++; count ++;} END {for (a in CMD) print CMD [a] "" CMD [a]/count * 100 "%" a} '| grep-v ". /"| column-c3-s" "-t | sort-nr | nl | head-n10
                                                                                                                                                      (Chen Hao note: It's a bit complicated, history | awk '{print $2}' | awk 'BEGIN {FS = "|"} {print $1}' | sort | uniq-c | sort-rn | head- 10)
                                                                                                                                                      This line of instinct outputs the ten most commonly used commands, which can even gain insight into what type of programmer you are.
                                                                                                                                                      • Tr-c "[: digit:]" "</dev/urandom | dd cbs = $ COLUMNS conv = unblock | GREP_COLOR =" 1; 32 "grep? Color "[^]"
                                                                                                                                                        Want to see the screen effect of Marix?

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.