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

 

posted @ 2020-09-01 11:20  骑着蜗牛看海呀  阅读(337)  评论(0编辑  收藏  举报