Shell command--file creation, search command--summary from "Linux Shell script Raiders"

Source: Internet
Author: User
Tags touch command cron script egrep




(a) file creation command 1.touch command

For example, the Touch ABC command creates an empty file named ABC in the local directory


2.CP command

The CP command allows us to copy the contents of a file to a file of the same name or name, and copy the resulting file to any directory. One risk of using the CP command is that it can easily overwrite files in different directories without prompting the user-R option supports recursive replication. For example: cp-ar/usr/share/doc/. /doc/copies all subdirectories and related files in the source directory


3.MV command

The MV command essentially labels the file with a different label, for example: MV file1 file2 command is to change file1 name to FILE2MV command can also be used in the directory


4.LN command

Linked files allow users to edit the same file in a different directory hard links contain a copy of the file. As long as the hard-link files are in the same partition, their index numbers are the same ln/etc/samba/smb.conf smb.conf soft joins play a directional role. When we open a file created with a soft link, the link redirects us to the original file. Ln-s/etc/samba/smb.conf smb.conf


5.RM command

The-r switch option can override any security measures by recursively making the-f switch Option rm-rf/root/a/b


6. Directory Creation and deletion

The mkdir and RmDir commands are used to create and delete directories-p No error if existing, make parent directories as needed such as Mkdir-p/test1/test2/test3 rmdir-p/ Test1/test2/test3


7.alias command

Can be used to simplify several commands. The default alias provides a bit of security for the root user


(ii) File Search 1.find command

Find/-name name.conf This command starts searching from the root directory find/usr-name name.conf starting from the/usr directory


2.locate command

Rhel allows users to create a database that holds all of the installation files and directories. The disadvantage of the locate command is that this database is typically updated only once a day, which is documented in the/etc/cron.daily/mlocate.cron script file that can be executed directly by the root user from the command line interface. Just enter the full directory of this file.


(iii) Management of text files 1.cat commands

The simplest text file Read command is cat. The cat filename command lets you page through the contents of the filename file.


2.less and more commands

With the more filename command, you can page through the contents of a text file, and each time a screen is displayed with the less filename command, we can use page up and page Down key page forward or backward view the text of a copper piece the less command has several features that are not available for both the more and the cat commands. It can read text files compressed in gzip format, usually with a. gz extension.


3.head and Tail commands







Head command to read the file header



Syntax: $ head–n N File Prints the first n rows $ head–n-n file prints all rows except the last n rows


The tail command always reads the end of the file



Syntax: $ tail–n n file prints after n lines $ taill–n +n file prints all rows except the first n rows


(iv) command to process text flow







Text flow is the flow of data. For example, the cat filename command outputs a stream of data from filename to the screen. When these files become larger, it is best to first use the filter commands to process these streams.


1.sort command

There are several ways to sort the contents of a file. By default, the sort command sorts the contents of the file alphabetically by character at the beginning of each line.


2.grep command

The grep command is used for text search, which reads all lines of a file by default $ grep match_pattern filename or grep "match_pattern" filename a grep command can also be searched for multiple files $grep " Match_text "File1 file2 file3 ... the grep command usually treats Match_pattern as a wildcard character. If you want to use regular expressions, you need to add the-e option-this means using an extended (extended) regular expression, or using the EGREP command that allows regular expressions by default. For example: $ GREP-E "[a-z]+" or $egrep "[A-z]" matches the regular expression of the URL $ egrep-o "http://[a-za-z0-9." +\. [A-za-z] {2,3} "index.html--color=atutoOptions: You can highlight the matching words in the output line- oOptions: Outputs only the text portions of the file that match. Example: Output Each word echo this is a test file | Egrep-o "\b[[:alpha:]]+\b"- vOption: Print all rows except those that contain Match_pattern- COptions: Number of rows in a statistic file or text containing matching strings- bOptions: Print style matches the character or byte offset in which the option-B is always used with-O For example: $echo GNU is not Unix | Grep-b-O "not" other options: 1)Search Files recursively$ grep "text". -R-N2)ignore case in style$ echo Hello World | Grep-i "HELLO" 3)match multiple styles with grep$ GREP-E "Pattern1"-E "pattern2" or write down a style in the style file that needs to be matched, then execute grep with option-F. $echo Hello this is cool | Grep-f pat_file4)include or exclude files in grep searchOnly recursively searches the directory for all. C and. cpp files: $ grep "main ()". -R--include *. {C,cpp} Note: Some{string1,string2} will be extended to somestring1 somestring2 exclude all Readme files in the search: $ grep "main ()". -R--exclude "README" If you want to exclude directories, you can use the--EXCLUDE-DIR option if you need to read the list of files you want to exclude from the file, using--exclude-from FILE5)grep and Xargs using a 0-value byte suffixIn the following sequence of commands, the grep output takes 0 value bytes as a terminator file. This can be specified with the-Z option of grep. xargs-0 read input and split file name with 0 value byte Terminator: $ grep "test" file*-lz | Xargs-0Rm.-Z usually combined with-L 6)The silent output of grep: In silent mode (quiet mode), the grep command does not print any output to the standard output. It only runs the command and then returns the return status depending on whether the command executes successfully or not. Using-Q7)print a line before or after matching textTo print 3 rows after matching a result, use the-a option-A NUM,--after-context=num. For example $seq 10 | Grep-A3 5 to print 3 lines before matching a result, use the-B option-B NUM,--before-context=num. For example $seq 10 | Grep- b3 5to print a match to a resultBefore andafter the 3 lines, use-Coption-C NUM,-num,--context=num. For example $seq 10 | grep- C3 5If there are multiple matches, a line "--" is used as the delimiter between each match. For example $ ECHO-E "A\NB\NC\NA\NB\NC" | grep a-a 1







3.diff command







The diff command can find the difference between two files. Diff/root/ifcfg-eth0/etc/sysconfig/network-scripts/ifcfg-eth0


4.WC command







WC is a tool for statistics. It is the abbreviation of Word count (word count).


1) Count rows $ wc-l file

2) Count words $ wc-w file3) Statistics characters $ wc-c file4) when not available with any option to perform WC: $ WC File It only prints the number of lines, words, and characters of the file, separated by tabs from each other



5.sed command







the SED command is an abbreviation for stream editor that searches for and modifies a specified word or even text flow in a file.  If you want to replace everything, you need to add the parameter G at the end of the command with the following method: The $ sed ' s/pattern/replace_string/g ' file suffix/g means that sed replaces each match. But sometimes we don't need to replace the first n matches, but instead we need to replace the rest of the matches. You can use/ng for example: $echo This thisthisthisthis if you need to start replacing from Nth match | Sed ' s/this/this/4g 'sed common combination command 1) Remove blank line 2) Matched string tag &  $ echo This was an example | sed The ' s/\w\+/[&]/g ' regular expression \w\+ matches each word, and then we replace it with [&],& corresponds to the previous match to the word 3) substring match tag \1 & represents a string that matches a given style  4) combine multiple expressions  5) Reference








SED can replace strings in the given text. $ sed ' s/pattern/replace_string/' file

-I option : Applies the replacement structure to the original file.   For example: $ Sed-i ' s/text/replace/' file equivalent to sed ' s/text/replace/' file > newfile; $MV newfile File



$ sed '/^$/d ' file


In sed, a string that matches a style with & is able to use the matched content when replacing a string


$ echo Seven Eignt | Sed ' s/\ ([a-z]\+\] \ ([a-z]\+\)/\2 \1/' ([a-z]\+\] matches the first word ([a-z]\+\) matches the second word \1 and, 2 is used to refer to them. This medical use is referred to as a backward reference (back referencing). In the replacement section, their order is changed to \2 \1 so the results appear in reverse form.


$ sed ' expression ' | Sed "expression" is equivalent to sed ' expression; Expression


Sed expressions are usually quoted in single quotes. However, you can also use double quotation marks. Double quotes extend the expression by evaluating it. Double quotes are useful when we want to use some variable strings in an SED expression. For example: $ text=hello $echo Hello World | Sed "s/$text/hello/" result Hello Word, $text evaluation result is Hello


6.awk Command

An awk script typically consists of 3 parts: A BEGIN statement block, an end statement block, and a common statement block that can use pattern matching. For example Echo-e "Line1\nline2" | awk ' BEGIN {print ' Start '} {print} end{print "END"} 'There are two important things to remember about print: When the parameters of print are separated by commas, the parameters are printed with spaces as delimiters, and in Awk's print statement, the double quotes are used as concatenation operators (concatenation operator). For example:$ echo | awk ' {var1= "v1"; var2= "V2"; var3= "v3"; \ print var1,var2,var3;} ' output v1 v2 v3$ echo | awk ' {var1= "v1"; var2= "V2"; var3= "v3"; \ Print Var1 "-" var2 "-" VAR3;} ' Output V1-v2-v3Supplemental Content1) Special variablesNF: Indicates the number of fields in the field, corresponding to the current number of fields during execution. $: This variable contains the text content of the current line during execution$: This variable contains the text content of the first field$: This variable contains the text content of the second fieldFor example, we can print the last field in a row with print $NF, print the penultimate field with $ (NF-1), and the other fields in turn. to print all the text in this range from M line to N lines, syntax $ awk ' nr==m, nr==n ' filenameto print text between Start_pattern and End_pattern, Syntax $ awk '/start_pattern/,/end_pattern/' filename used for the style in awk as a regular expression2) Passing the value of the external variable to awkThere is another flexible way to pass multiple external variables to awk for example:$ var1= "Variable1"; var2= "Variable2" $ echo | awk ' {print v1,v2} ' v1= $var 1 v2= $var 2In the above method, the variables are separated by a space, in the form of key-value pairs (v1= $var 1 v2= $var 2) as Awk's command-line arguments immediately after the begin, {}, and end statement blocks. 3) Read line with GetlineFor example: Seq 5| awk ' BEGIN {getline;print $0}{print} '







4) Filter The rows that awk handles with a style$ Awk ' NR < 5 ' # lines with line numbers less than 5$ awk ' nr==1,nr==4 ' #行号在1到5之间的行$ awk '/linux/' # contains lines of Style Linux (you can use regular expressions to specify styles)$ awk '!/linux/' # does not contain lines of style Linux5) Set the field delimiterin the BEGIN statement block, you can set the delimiter for the output field with fs= "delimiter" $ Awk ' BEGIN {fs= ":"} {print $NF} '/etc/passwd6) Read command output from awkawk supports associative arrays with text as an index7) using loops in awkAwk has a number of built-in string control functions:Length (String)index (string,search_string)Split (String,array,delimiter)substr (string,start-position,end-position)The sub (regex, REPLACEMENT_STR, String) replaces the first content of the regular expression with the Replacement_strgsub (Regex, REPLACEMENT_STR, String) replaces all the contents of a regular expression with Replacement_strMatch (regex, string) checks to see if the regular expression matches the string. Returns a value other than 0 if it can match; otherwise, 0 is returned. Match () has two related special variables, Rstart contains the starting position of the content that the regular expression matches, and Rlength contains the length of the content matched by the regular expression.
































AWK is designed for data flow. It's interesting because it can manipulate columns and rows.

The structure of the awk script is basically as follows: awk ' BEGIN {print ' start '} pattern {commands} end {print "End"} file



NR: Represents the Record count (number of records), which corresponds to the current line number during execution.


。。。


$N: This variable contains the text content of the nth field


Example: $var =10000 $ echo | Awk-v variable= $var ' {print VARIABLE} '


Typically, grep reads all the rows of a file by default. If you only want to read a row, you can use the Geiline function. Sometimes we need to read the first line from the BEGIN statement block.


We can specify some conditions for the rows that need to be processed, such as:


The default field delimiter is a space. We can explicitly specify a delimiter with-F "delimiter": $ awk-f: ' {print $NF} '/etc/passwd


$echo | awk ' {' grep root/etc/password | getline cmdout; print Cmdout} ' can read the output of an external shell command into a variable by using Getline cmdout


In awk you can use a for loop in the format: for (i=0;i<10;i++) {print $i;} or for (i in array) {print Array[i]}











Shell command--file creation, search command--summary from "Linux Shell script Raiders"




Related Article

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.