zoukankan      html  css  js  c++  java
  • 自定义linux系统文件搜索脚本

    1.  新建search.sh脚本,写入以下shell脚本:

    #!/bin/sh
    # lazy find
    # GNU All-Permissive License
    # Copying and distribution of this file, with or without modification,
    # are permitted in any medium without royalty provided the copyright
    # notice and this notice are preserved.  This file is offered as-is,
    # without any warranty.
    ## help function
    function helpu {
        echo " "
        echo "Fuzzy search for filename."
        echo "$0[--match-case|--path] filename"
        echo " "
        exit
    }
    ## set variables
    MATCH="-iname"
    SEARCH="."
    ## parse options
    while [ True ]; do
    if [ "$1" = "--help" -o "$1" = "-h" ]; then
        helpu
    elif [ "$1" = "--match-case" -o "$1" = "-m" ]; then
        MATCH="-name"
        shift 1
    elif [ "$1" = "--path" -o "$1" = "-p" ]; then
        SEARCH="${2}"
        shift 2
    else
        break
    fi
    done
    ## sanitize input filenames
    ## create array, retain spaces
    ARG=( "${@}" ) 
    set -e
    ## catch obvious input error
    if [ "X$ARG" = "X" ]; then
        helpu
    fi
    ## perform search
    for query in ${ARG[*]}; do
        /usr/bin/find "${SEARCH}" "${MATCH}" "*${ARG}*"
    done

    2.  赋予脚本可执行权限:

        chmod  +x  search.sh

    3.  使用

        sh  search.sh  hello                               ## 在当前目录下遍历搜索带hello关键字的所有文件

        sh search  --path   /usr/local    hello      ##在指定目录/usr/local下搜索带关键字hello的所有文件

    4.  添加到系统快捷命令

        vim ~/.bashrc                                        ##编辑系统命令

        添加:

        alias lf=/root/find_file.sh                       ##自定义文件搜索脚本

        保存生效:

        .  ~/.bashrc

    5.  使用快捷命令搜索文件

        lf  hello  或者  lf  --path   /usr/local   hello

  • 相关阅读:
    linux以下安装dnw
    【Spark】Spark容错机制
    Codeforces Round #273 (Div. 2)
    IOS开发之简单计算器
    Andorid使用WiFi 连接adb进行调试
    i2c_set_clientdata函数【转】
    内核添加dts后,device和device_driver的match匹配的变动:通过compatible属性进行匹配【转】
    devm_kzalloc【转】
    RK3288 make otapackage 出错的问题【转】
    RK3288-OTA编译失败解决办法【转】
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13595113.html
Copyright © 2011-2022 走看看