Shell scripts implement file traversal and deletion,

Source: Internet
Author: User

Shell scripts implement file traversal and deletion,

The functions to be implemented in this article are as follows: a folder has a folder named by number, and files except the maximum encoding need to be deleted.

Implementation

General idea: traverse all files in the folder cyclically, and obtain the maximum encoding file by regular expression. Then, loop through the file and delete the files except the maximum encoding.

The implementation code is as follows:

#!/bin/bashfunction getdir(){    max=0    DATEPATTERN="^[0-9]*$"    for element in `ls $1`    do        if [[ "$element" =~ $DATEPATTERN ]]        then            if [ `expr $max - $element` -lt 0 ]            then                max=$element            fi         fi    done        for element in `ls $1`    do        if [[ "$element" =~ $DATEPATTERN ]]        then            if [ $max != $element ]            then                rm -rf element            fi         fi    done}root_dir="/root/cloud/builds"getdir $root_dir

Effect: folder:/root/cloud/builds

After the script is executed:

Basic Shell knowledge 1. Variables

The shell script variable declaration is assigned a value through "=", which is different from C ++ or java,Variable names, values, and equal signs cannot have spacesOtherwise, the variable cannot be identified. For example

Var = 10 var1 = "qwert" var2 = 'qwert'

Echo $ var # output 10
Echo $ var1 # output qwert
Echo $ var2 # output qwert

Get the value in the variable, in the format of "$ variable name.

2. String

Double quotation marks or single quotation marks can be used to declare a string, but there are some differences between the two.

Single quotes: 1. The characters in single quotes are output as is, and the variables do not work. 2. escape characters cannot be used in single quotes and an error is returned;

Double quotation marks: 1. variables can be included and values can be taken; 2. escape characters can be included

#! /Bin/basha = 10val = 'Hello world $ a' echo "single quotes:" $ val = 'Hello 'World' $ a' echo "single quotes + single quotes = splicing: "$ val # val = 'Hello \ 'World \ '$ a' # echo $ val # error:/usercode/file. sh: line 9: unexpected EOF while looking for matching ''' val = "hello 'World' $ a" echo "Double quotation marks + single quotation marks = output single quotation marks: "$ valval =" hello "world" $ a "echo" Double quotation marks + double quotation marks = splicing: "$ valval =" hello \ "world \" $ a "echo" Double quotation marks + double quotation marks Escape Character = output double quotation marks: "$ valval =" hello "$ a" world "echo" Double quotation marks + variable = splicing: "$ val

Output result:

Single quotes: hello world $ a single quotes + single quotes = splicing: hello world $ a double quotes + single quotes = output single quotes: hello 'World' 10 double quotes + double quotes = splicing: hello world 10 double quotation marks + double quotation marks Escape Character = output double quotation marks: hello "world" 10 double quotation marks + variable = concatenation: hello 10 world

 String concatenation

(1) String concatenationAssign a value to a variable: When double quotation marks or single quotation marks are concatenated, if the substring is completely a pure string, there can be spaces between them. If there is a variable, there cannot be spaces between the variable and the string;

(2) String concatenation echo output: space is allowed. Echo "hello" $ a 'World' output: hello 10 world

3. PASS Parameters

The format of parameters obtained by the script function is $ n. n indicates the nth parameter. For example, $1 indicates obtaining the first parameter, and $2 indicates obtaining the second parameter .... $0 indicates obtaining the execution Script Name

4. Basic operations

Native bash cannot perform simple mathematical calculations, such as awk or expr.

Various calculation rules can refer to the rookie Tutorial: http://www.runoob.com/linux/linux-shell-basic-operators.html

Calculation used in this article includes: subtraction calculation, unequal judgment, less than judgment, such as ['expr $ max-$ element '-lt 0], [$ max! = $ Element]

5. Process Control

(1) condition judgment:

if conditionthen    ......elifthen    ......else    ......fi

(2) for Loop

for var in item1 item2 ... itemNdo    command1    command2    ...    commandNdone

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

6. Regular Expression

The regular expression used in this article is a positive integer, for example, "^ [0-9] * $", starting with ^ and ending with $, [0-9] Any number between 0 and 9. * indicates a number consisting of 0 or more front-side characters. For more information, see http://www.jb51.net/article/94354.htmor.

Determines whether the target matches a regular expression.Brackets and = ~For example, [["$ element" = ~ $ DATEPATTERN]

7 .#! /Bin/bash

#! It indicates the agreed tag and tells the system which interpreter is required to execute the script. bash is used by default in Linux. You can view the bash file in the/bin directory, for example:

All shell scripts to be executed must be written in the first line.

Summary
  • It takes some time to learn the basic syntax and commands of shell. You can refer to the cainiao tutorial or Shell programming from getting started to proficient.
  • When using the rm command in shell scripts, you also need to be careful. Accidental operations may cause the system to fail. You can refer to the fatal mistakes in the bash script when using the rm command.

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.