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

  • 相关阅读:
    【反射】Java反射机制
    Composer教程之常用命令
    Composer教程之基础用法
    Composer教程之初识Composer
    Composer 的结构详解
    现代 PHP 新特性系列(七) —— 内置的 HTTP 服务器
    现代 PHP 新特性系列(一) —— 命名空间
    现代 PHP 新特性系列(二) —— 善用接口
    现代 PHP 新特性系列(三) —— Trait 概览
    现代 PHP 新特性系列(四) —— 生成器的创建和使用
  • 原文地址:https://www.cnblogs.com/zk-blog/p/13595113.html
Copyright © 2011-2022 走看看