Shell script implements file traversal and delete operations

Source: Internet
Author: User

The following features are required for this article: a folder that has a folder that is named by a numeric number requires the deletion of files other than the maximum encoding.

Specific implementation

General idea: Loop through all the files in the folder, match the maximum encoding file, and then loop the file to delete the file except the maximum encoding.

The implementation code is as follows:

#!/bin/BashfunctionGetdir () {max=0Datepattern="^[0-9]*$"     forElementinch`ls$1`     Do        if[["$element"=~$DATEPATTERN]]  Then            if[ `Expr$max-$element '-lt0 ]             ThenMax=$elementfi         fi     Done         forElementinch`ls$1`     Do        if[["$element"=~$DATEPATTERN]]  Then            if[$max! =$element] Then                RM-RF Elementfi         fi     Done}root_dir="/root/cloud/builds"getdir $root _dir

Achieve effect: folder:/root/cloud/builds

After executing the script:

Shell Basics 1. Variable

The variable declaration of a shell script is assigned by "=", unlike C + + or Java, where the variable name, value, and equal sign cannot have spaces , or the variable cannot be recognized. Such as

var=      var1="qwert"   var2='qwert  '

echo $var #输出 10
echo $var 1 #输出 qwert
echo $var 2 #输出 Qwert

Gets the value in the variable, in the format "$ variable name".

2. String

The declaration string can be in double or single quotes, but there are some differences

Single quotation mark: 1. The characters in the single quotation mark are output as is, and the variables in them do not work; 2. The escape character cannot be used in single quotation marks, it will be error;

Double quotes: 1. Can contain variables and take values; 2. Can contain escape characters

#!/bin/Basha=TenVal='Hello World $a'Echo "Single quotes:"$val Val='Hello'World'$a'Echo "single quote + single quote = stitching:"$val #val='Hello \ ' world\ ' $a'#Echo$val # Error:/usercode/file.SH: Line9: Unexpected EOF whileLooking forMatching '"'Val="hello ' world ' $a"Echo "double quotation mark + Single quote = output single quote:"$valval="Hello"World"$a"Echo "double quotes + double quotes = stitching:"$valval="Hello \ "world\" $a"Echo "double quotation mark + double quote escape character = Output double quote:"$valval="Hello"$a" World"Echo "double quotes + variables = stitching:"$val

Output Result:

single quote: Hello World $a single quote + single quote = stitching: Hello World $a double quotes 'World'     double quotes, double quotes , "World" , double quotes + variable = Stitching: Hello 10world

String Stitching problem

(1) string concatenation assignment to the variable : double quotation marks or single quotation marks when stitching, if the substring is completely pure string, there can be spaces between, if there is a variable, there can be no space between the variable and the string;

(2) String stitching echo output: can have spaces. Like echo "Hello" $a ' world ' output: Hello ten World

3. Passing parameters

The script function gets the format of the parameter: $n, n represents the nth parameter, such as $ = to get the first argument, and $ = to get the second argument .... $ A to get the execution pin name

4. Basic operations

Native bash cannot perform simple mathematical calculations, which can be accomplished by command, such as awk or expr.

The various arithmetic rules can refer to the Novice tutorial: http://www.runoob.com/linux/linux-shell-basic-operators.html

The calculations used in this article include: subtraction calculation, unequal judgment, less than judgment, such as [' Expr $max-$element '-lt 0], [$max! = $element]

5. Process Control

(1) Condition judgment:

if condition  Then     ...... elif  Then     ...... Else     ...... fi

(2) For loop

 for inch item1 item2 ... itemn  Do     command1    command2    ... CommandN Done

Specific reference: http://www.runoob.com/linux/linux-shell-process-control.html

6. Regular expressions

The regular expression used in this article is a positive integer, such as "^[0-9]*$", starting with ^, ending with $, [0-9] identifying any number between 0 and 9, * represented by 0 or more characters in front of the character. Specific, can refer to http://www.jb51.net/article/94354.htm or related books.

Determine if the target matches the regular expression, using both brackets and =~, such as [["$element" =~ $DATEPATTERN]]

7. #!/bin/bash

#! is a contract tag that tells the system what interpreter the script needs to execute, and bash is used by default under Linux to view bash files in the/bin directory, such as:

All shell scripts that need to be executed need to be written on the first line.

Summarize
    • It takes a little time to learn the basic syntax and commands of the shell, a beginner's tutorial, or "shell programming from beginner to mastery."
    • When using the RM command in a shell script, it is also necessary to be careful that careless operation may cause the system to hang up and to see the fatal myths of using the RM command in a bash script

Shell script implements file traversal and delete operations

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.