• 自定义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

  • 相关阅读:
    一点一点学习GIt
    17-ajax向后端提交POST请求
    16-djongo中间件学习
    ES6快速入门
    15 Django组件——中间件
    导入Scrapy 项目报错:no module named scrapy
    slf4j + logback 输出日志:mybatis sql语句
    java实现链表
    Log4j2.xml的日志设置
    java多线程之生产者消费者经典问题
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13595113.html
Copyright © 2020-2023  润新知