Mastering some Linux Command lines and standard utilities can greatly improve the processing efficiency of daily tasks and serve as a powerful helper for development. This article will focus on useful commands and combinations, and record the problems and solutions I encountered in my daily life and work.
1. Linux powerful tools:
Find: Provides n multiple search options to find a collection of files that meet various conditions in the directory structure;
Grep: searches for text strings matching the pattern in a given file set based on powerful regular expressions;
Sed: Execute text search/replacement based on powerful regular expressions and editing commands.
Awk: especially suitable for processing records.
Sort: provides many options for sorting record files and non-record files.
MAN: detailed and readable help information, -- Help, Info
2. Common Linux commands and important shell elements:
|,>, <, >>, &,/Dev/null,
Echo, Cat, ls, Head, tail, WC, cut, join, printf, TR, uniq, Tee,
CD, CP, MV, RM, mkdir, locate, Which, whereis, WHO, chmod, touch, file, xargs, DF, md5sum,
Read, set, test,., sh, type, export, ENV, unset, readonly, shift, getopts, exit, exec, Eval,
FC, FG, trap, kill, jobs, PS, PWD,/proc/PID, crontab, AT, sleep, top,
$ Home, $ ENV, $ ifs, $ PWD, $ ps1, $ PS2, $ (expr), $ #, $ @, $1-$9, ${10 -...}, $ *, $ ?, $, [], & N, $ {cmd-replacement},/dev/null,/dev/stty
$ {Varname:-word}, $ {varname: = word}, $ {var # pattern}, $ {var # pattern}, $ {var % pattern }, $ {var % pattern },
For VaR in $ @ Do... done; case $ VaR in patt1-N) dosome; esac;
If cond then statement Elif cond2 then statement2... else statement3 fi;
3. Command combination:
(1) Sed-I's/@ override // G' $ (find.-Name '*. Java ')
Find.-Name '*. Java' | xargs sed-I's/@ override // G'
This command is used to remove the @ override annotation from all. java files in the current directory and Its subdirectories. $ (Find.-Name '*. Java') uses the command replacement function. Shell uses the result obtained by executing this command as the input parameter of sed. Sed is an important tool for text replacement.-I is local replacement, which is directly replaced in the edited file. If not, the original file will not be modified, only the changes are output to the standard screen. The 's/oldtextpatt/newtext/G' command is called the edit command of the SED command. You can perform global replacement to match all oldtextpatt
Replace the text with newtext. You can also write multiple editing commands in a file, such as pat_file, and then use-F pat_file to call it.
Because the length of the command line is limited, the files found by find will be used as command line parameters, which may exceed the limit. Therefore, a better solution is to use xargs, it reads pipeline Input and converts it to the following command parameter, so as not to worry about the length limit of the command line characters.
Extension :*. java is a file matching form, which can be replaced *. C ,*. H ,*. {C, Java, H, Deb, tar. {GZ, bz2}, JS, PHP, HTML} (Note: spaces are not allowed in the middle), etc;
Important tools: SED, find,
Important Concept: regular expression. It is used to describe the text mode. The most important and powerful UNIX tools are built on it, such as find, sed, awk, and grep. For example, a * is used to describe zero or multiple A; [1-9] * [0-9] + can match any integer. We recommend "regular expressions must know", a thin 100 pages, a good Regular Expression entry book.
(2) While ifs =: Read var1 var2... Varn do... done <inputfile;
CMD | while ifs =: Read var1var2... Varn do... Done
Use IFS to divide each row of data output from an input file or pipeline into multiple fields and assign the values to var1, var2 ,..., varn performs some processing until all rows output by the input file or pipeline are read.
(3) find.-Name '*. Java' | xargs awk' $0 ~ /Vm_monitor/{printf "% s: line % s: \ n % s \ n", filename, FNR, $0} '| SED's/^ [[: space:] \ {1 ,\}//'
Search for the matching mode vm_monitor string in all. java files in the current directory and subdirectory, and display the file name, row number, and matched row content.