command-line skills needed for efficient Linux users

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

command-line skills needed for efficient Linux users

This article introduces some command-line tools that are useful in your daily work. Time-saving tips can be saved in high-efficiency Linux users. Some commands need to be installed first with yum, apt-get Install command.

Recently saw a question and answer question on Quora, about saving time tips for efficient Linux users. The answer to the topic to learn the summary, together with some of their own experience, recorded as follows, convenient for themselves and everyone's reference. Here are some command-line tools that are useful in the daily work of several respondents.

For any commands you don't know, use "man <COMMANDNAME>" to view or use Google.

Some commands need to be installed first with yum, apt-get Install command.

First, the basic command

1. Learn about the basic bash: read through the Bash man page.

2, Learning vim: on Linux systems, although you have Emacs and Eclipse, but Vim is still a weapon of unparalleled.

3, understand SSH, basic no Password authentication method. For example, through Ssh-agent, Ssh-add and so on. "Empathy" usually use the following script to complete no password verification, save effort.

Execution mode sh nopasswd USER remote_host

Before you execute this script, verify that:

    • There are already id_dsa.pub on this machine, if none. Use the command ssh-keygen-t DSA to obtain.
    • On the remote machine login user home directory, there is already an. SSH folder, if not created.
    1. $ cat NOPASSWD
    2. #!/bin/sh
    3. SCP ~/.ssh/id_dsa.pub [Email protected]$2:~/
    4. SSH [email protected]$2 "Touch ~/.ssh/authorized_keys; Cat ~/id_dsa.pub >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys; exit "

4. Familiar with the task management commands commonly used in bash: &,ctrl-z,ctrl-c,jobs,fg,bg,kill, etc.

5. Basic file management commands: LS, ls-l, less, head, tail, tail-f, LN, ln-s, chmod, Chown, Du, Du-sk *, DF, Mount

6, basic Network Management command: ipconfig, ifconfig, dig

7. Familiar with regular expressions, and Grep,egrep options:-O,-A,-B

8. Software Installation Command learn: Apt-get and Yum

Cat-n: can help display line numbers.

Ii. some expressions

!! : Executes the previous command again

!$: Last word of previous command

{A.. b}: A list of numbers in order from A to B

{A,b,c}: Three words a,b,c. You can use Touch/tmp/{foo,bar,baz}

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

$: The name of the command being executed

$#: Number of arguments passed in the currently started command

$?: The execution return value of the previous command.

$$: The shell's process number.

$*: Start all parameters of the shell script starting with $.

Iii. Daily use of commands

Ctrl-r: In bash, Ctrl-r is used to search in the history command

Ctrl-w, Ctrl-u, Alt-backspace:bash, ctrl-w delete the last word, ctrl-u delete the last line, alt-backspace delete a word before the cursor man ReadLine Contains a large number 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:

    1. Find. -name \*.py | Xargs grep some_function
    2. 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 a name to find a process, or send a signal directly to a process that specifies a name.

Learn some of the signals that users can send to processes. such as Kill-stop [PID], let the PID process hangs.

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 Set-x to get the debug output and use SET-E to get the error output.

;: A semicolon is used to open a child shell and to close after it has finished running. For example:

    1. #在当前路径下执行一些命令
    2. (Cd/some/other/dir; other-command)
    3. # The work 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}: Outputs the var variable in addition to the prefix or the suffix part. As the following code output is foo.txt.

    1. var = Foo.pdf
    2. Echo ${var%pdf}.txt

<,>: Input and output redirection operation.

Some_command > LogFile 2>&1: Outputs both the standard output and the standard error output during the Some_command run 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: Converting HTML to Text

Xmlstarlet: This command is useful when you need to work with 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 the loss of links, do not need to enter the Yes confirmation and the remote server link, as well as the link to enable compression. It is recommended to put it in the. Ssh/config.

    1. Tcpkeepalive=Yes
    2. Serveraliveinterval=
    3. serveralivecountmax=6
    4. stricthostkeychecking=No
    5. compression=Yes
    6. forwardagent=Yes

Add # to the command you are typing: The command has been entered half, and suddenly change your mind when you want to reduce the run, you can use alt-# to add ' # ' to the command before the command becomes 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: Will inadvertently need a large amount of output text, enter these two operations, in turn, more than simply press ctrl-c more quickly to let the program terminate.

Iv. 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.

The intersection, and, and complement operations of a collection using 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.

    1. Cat A B | Sort | Uniq > C # c is a union b
    2. Cat A B | Sort | Uniq-d > C # c is a intersect B
    3. Cat A b b | Sort | Uniq-u > C # c is set difference A-B

Using the locale setting in Lc_all=c:linux affects a 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.

    1. awk ' {x + = $ $} END {print x} '

Shuf: This 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-k1,1 will only sort the first column, and-k1 will sort by 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 (tab input);

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 you 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.

Five, System debugging

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, use this command to find the complete stack trace, heap information (containing the details of garbage collection) in Stderr/logs.

Mtr,traceroute: can help to find network problems, the former is 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, the-C option can do the PROFILING;-P option to hang onto a specified process.

LDD: Checking for shared libraries

GDB: Learn how to use GDB to connect to a running process and get its stack trace.

/proc/: It is very useful when doing on-site commissioning. such as/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.

Vi. references

Http://unixhelp.ed.ac.uk/scrpt/scrpt2.2.2.html

Http://www.quora.com/Linux/What-are-some-time-saving-tips-that-every-Linux-user-should-know

Vii. Acknowledgements

    • Yang Qing: Increase Alt-backspace
    • Dikar: Modifying string--strings
    • ZSC: Added GNU parallel command
    • Robberphex: Modify the –> abnormality of the turn sing

command-line skills needed for efficient Linux users

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.