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

  • 相关阅读:
    IoC 中 car 对象的配置如下,现在要添加 user 对象,并且将 car 注入到 user 中,正确的配置是?
    说说 IoC 中的继承和 Java 继承的区别
    bean 的 scope 有几种类型?请详细列举。
    简单谈谈 IoC 容器的原理
    谈谈你对 Spring IoC 和 DI 的理解,它们有什么区别?
    day13-14 内置函数
    day12 生成器(重要)
    day11 闭包和迭代器
    day10 函数进阶
    day09 函数
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13595113.html
Copyright © 2011-2022 走看看