Practical Linux command line tips and tricks

The following are some of the command line tools that are useful in the daily work of several respondents. For any commands you don't understand, use "man" "View, or use Google. Some commands need to be installed first with the yum, apt-get install command.

1, the basic order

Learn the basic bash

Read through the entire bash man page.

Learning VIM

On Linux systems, although you have Emacs and Eclipse, VIM is still a great tool.

Learn about SSH, the basic no password authentication method.

For example, through ssh-agent, ssh-add, etc. "Spiritual and Interest" usually uses the following script to complete the passwordless verification, saving effort and effort.

Execution mode sh nopasswd USER REMOTE_HOST

Before executing this script, please confirm:

- There is already id_dsa.pub on this machine, if not. Obtained using the command ssh-keygen -t dsa.

- The .ssh folder already exists in the login user's home directory on the remote machine, if not created.

$ cat nopasswd #!/bin/shscp ~/.ssh/id_dsa.pub $1@$2:~/ssh $1@$2 " touch ~/.ssh/authorized_keys ; cat ~/id_dsa.pub >> ~/.ssh/authorized_keys ; chmod 644 ~/.ssh/authorized_keys; exit"

Familiar with the task management commands commonly used in Bash

&, Ctrl-Z, Ctrl-C, jobs, fg, bg, kill, etc.

Basic file management commands

Ls, ls-l, less, head, tail, tail -f, ln, ln -s, chmod, chown, du, du -sk *, df, mount

Basic network management commands

Ipconfig, ifconfig, dig

Familiar with regular expressions, and the options used by grep, egrep

-o, -A, -B

Software installation command to understand

Apt-get and yum

Cat -n: can help display the line number.

2, some expressions

!!

Execute the previous command again

!$

The last word of the previous command

{a..b}

Follow a list of numbers from a to b

{a,b,c}

The three words a, b, c. can be used like touch /tmp/{foo,bar,baz}

{$1-$9}

Command line arguments when executing shell scripts

$0

The name of the command being executed

$#

The number of parameters passed in the currently started command

$?

The execution return value of the previous command.

$$

The process ID of the shell.

$*

Starting with $1, start all the parameters of the shell script.

3, daily use orders

Ctrl-R

In bash, Ctrl-R is used to search in history commands.

Ctrl-W, Ctrl-U, Alt-BackSpace

In bash, Ctrl-W deletes the last word, Ctrl-U deletes the last line, and Alt-BackSpace deletes a word before the cursor;

Man readline contains a lot of default hotkey bindings in bash;

Cd -

Return to the previous working path

Xargs

Very powerful command. If you are not sure if you can perform the task correctly, you can first use xargs echo to view it. Here's an example of using this feature:

Find . -name *.py | xargs grep some_functioncat hosts | xargs -l {} ssh root@{} hostname

并行

A more powerful command. It can perform parallel execution tasks, and can split input files, specify multiple nodes to run commands at the same time, etc.

Pstree -p

A powerful tool for using the process tree

Pgrep, pkill

Use the name to find the process, or send a signal directly to the process with the specified name.

- Understand some signals that users can send to the process . For example, kill -STOP [pid] causes the pid process to hang.

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 port is currently listening on which port.

Set

In the bash script, use set -x to get the debug output and set -e to get the error output.

The semicolon is used to open a subshell and run to close after closing. E.g:

#Execute some commands in the current path (cd /some/other/dir; other-command)# The working path is still the current directory

Understand the various parameter expressions in the shell

${name:?error message}

Check if a variable exists, if there is no output error message.

${var%suffix}, ${var#prefix}

Output the var variable except the prefix or suffix. The following code is output as foo.txt.

Var = foo.pdfecho ${var%pdf}.txt

<,>

Input and output redirection operations.

Some_command > logfile 2>&1

Both standard output and standard error output from some_command runs are output to the file logfile.

Man ascii

Get a nice ASCII table with decimal and hexadecimal 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 webpage code, which is very useful.

Lynx -dump -stdin

Convert HTML to text

Xmlstarlet

This command is useful when you need to process XML.

Ssh -L, ssh -D

When you need to access a web page with a remote server, this command can help you establish an ssh tunnel between the remote server and your machine.

Ssh connection optimization

The following configuration can help you avoid link loss, do not need to enter yes to confirm the link to the remote server each time, and enable compression in the link. It is recommended to put it in .ssh/config.

TCPKeepAlive=yesServerAliveInterval=15ServerAliveCountMax=6StrictHostKeyChecking=noCompression=yesForwardAgent=yes

Add # before the command being entered

If the command has been entered halfway, and suddenly change the mind to reduce the run time, you can use Alt-# to add '#' to the command to turn the entire command into a comment. This way you will find the command in the command history later.

Cron

Can help you make some scheduled tasks for scheduled execution.

Ctrl-S Ctrl-C

When you accidentally need a large amount of output text, enter these two operations one by one, and the program can be terminated faster than simply pressing Ctrl-C.

4, data processing

Sort,uniq, uniq -u, uniq -d

Understand these sorting commands

Cut,paste, join

Learn about the maintenance tools for these text files. Many people forget to join after using cut

Use sort/uniq to perform intersection, union, and complement operations =

Suppose a and b are two text files, and the lines are unique.

The following commands can quickly implement some collection operations.

Sort | uniq > c # c is a union bcat ab | sort | uniq -d > c # c is a intersect bcat abb | sort | uniq -u > c # c is set difference a - b

Use LC_ALL=C

The locale settings in Linux affect a large number of command line tools, including sorting tools. Most installed Linux systems set LANG or other locale to US English by default. But this can cause sorting and other commands to be several times slower. Therefore, export LCALL=C can avoid using i18n form to process data, which brings performance improvement.

Awk, sed

These two tools enable complex data replacement and modification.

For example, the following command implements summing the data in the lower three columns of the text file. Using the shell to do this is three times faster than using Python.

Awk '{ x += $3 } END { print x }'

Shuf

This command can be used to shuffle the lines in a file or randomly select some rows from it.

Sort

Learn how sort's common options (-t, -k, -s) work. Note that -k1,1 will only sort the first column, and -k1 will sort according to the entire row. -s can achieve stable sorting.

For example, first use the second domain sort, then sort by domain one, you can use this command to achieve:

Cat INPUT_FILE | sort -k1,1 | sort -s -k2,2

Tab input

In the bash command line, if you need to enter a tab, you can use Ctrl-V Or $' 'implementation

Hd, bvi

For binary files, these two commands implement hexadecimal decimation and binary editing operations, respectively.

Strings, grep

Can help find text in binary files .

Iconv,uconv

Can help convert text encoding

Split,csplit

Separate files by size and partition according to specific patterns.

5, system debugging

Iostat, netstat, top, atop, htop, dstat

Can help understand the state of the hard disk, CPU, memory, network. This can help you get a first look at what is happening in the system.

Free, vmstat

These two commands are important if you want to know the state of the memory. Where cached is the size of the file cache in the Linux kernel.

Kill -3

When debugging a Java program, use this command to find the complete stack trace, heap information (including garbage collection details) in stderr/logs.

Mtr, traceroute

Can help find network problems, the former is better than traceroute.

Iftop, nethogs

These two commands can just find out which port or process is taking up much network bandwidth.

Ab, siege

This Apache comes with tools that help you quickly check the performance of your web server.

Wireshark, tshark

It is a powerful tool for more advanced network debugging.

Strace, ltrace

These two commands can help you to bring some clues to problems such as program failure, suspended animation, crash, etc., without knowing anything.

In addition, they can help find some performance issues. For example, the -c option can be profiling; the -p option can be linked to a specified process.

Ldd

Check the status of shared libraries

Gdb

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

/proc/

Useful when doing on-site debugging. For example /proc/cpuinfo, /proc/XXX/cwd, /proc/XXX/exe, /proc/XXX/fd/, /proc/XXX/smaps

Sar

This command displays the history of CPU, memory, and network when it is necessary to determine why the system has failed at some point in the past.

Stap, perf

These two tools are useful when you need deeper analysis systems and performance.

Dmesg

This works well when there are some unusual phenomena in the system, such as hardware or driver problems.

1800 Puff

1800 Puffs Disposable ecig have a completely enclosed design, reducing the need for charging and replacing cartridges. The no-charge design also reduces the occurrence of faults. It is understood that with rechargeable e-cigarettes, each cartridge needs to be charged at least once and the battery efficiency is extremely low, while the design of disposable ecig can solve this problem very well.

1800 Puff Disposable Vape,1800 Puff Disposable Vape For Sale,Best Puff Disposable Vape

Shenzhen E-wisdom Network Technology Co., Ltd. , https://www.healthy-cigarettes.com