linux多文件查找
时间:2023-03-13 21:30:00
今天公司大神给了一个脚本来查找文件,记录下来:
#!/usr/bin/env bash usage() {
echo "[Usage]" echo "" echo "find-in-file [4mregex-for-file-name[0m [4mregex-in-file[0m [4m[options][0m" echo "Params: [4mregex-for-file-name[0m is used to find the file to search in." echo " [4mregex-in-file[0m is transfered directly to grep, can be a word or regex." echo " [4moptions[0m can be:" echo " -i ignore-case while using grep to filter content." echo " -I ignore-case while using find command to search file." echo " -f print full path-name instead of relative name." echo " -d instead of print result, remove all the found files." echo "" } print_fun() {
usage echo "[Function]" echo "" echo "1. find files contains certain content;" echo "2. show the line number and context of the content in found files." echo "3. or removed the found files." echo "" echo "This script is wrapped-find-grep, the executed command actually is:" echo " find . -type f -regex [4mregex-for-file-name[0m -print0 | xargs -0 grep -Hn --color [4mregex-in-file[0m" echo "In default, the find command and grep command is case-sensitive." echo "In other word, [4mregex-for-file-name[0m == .*exam.* will not match file Exam." echo "Use -I option to change this. The -i option has the same effect on command grep." echo "That is, the [4moptions[0m will change the command a little:" echo " with -i option, the grep will has a -i option." echo " with -I option, the find command will use -iregex instead of -regex." echo " with -f option, the path of find is not '.' but the result of executing command 'pwd'," echo " thus, the result is printed using absolute path name." echo " with -d option, even the -f option is also provided, it has no effect." echo " in such case, th found files will be removed, but no content will be printed."
echo ""
echo "[Additions]"
echo ""
echo "The search root is always current path. All files whose name matches"
echo "the param [4mregex-for-file-name[0m will be searched line by line."
echo "And only lines contains [4mregex-in-file[0m will be printed to standard output."
echo "Actually, the output is filtered by grep and printed in a pretty format."
echo ""
echo "If -d option is provided, no output will shown, ths found files will"
echo "be removed directly. So, user can use this script without -d option first,"
echo "and check the output. If all the founded files is correct, use this script"
echo "again with -d option to remove all the found files."
echo ""
}
if [ -z "$1" ] || [ -z "$2" ]; then
print_fun
exit 1
fi
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_fun
exit 0
fi
path="."
del_flag=""
pos=3
op="${
!pos}"
#find_op="-regex"
find_op="-regex"
while [ ! -z "${op}" ]
do
case "${op}" in
-i)
grep_op="-i"
;;
-I)
find_op="-iregex"
;;
-f)
path=`pwd`
;;
-d)
del_flag="true"
;;
*)
echo -e "Error. Unrecgnized value of the 3th param: $3\n"
usage
exit 2
;;
esac
pos=$[ ${pos} + 1 ]
op="${
!pos}"
done
fname_reg="$1"
if [[ ${fname_reg} != .** ]]; then
fname_reg=".*${fname_reg}"
fi
if [ -z "$del_flag" ]; then
find "${path}" -type f ${find_op} "${fname_reg}" -print0 | xargs -0 grep -Hn ${grep_op} --color "$2"
else
find "${path}" -type f ${find_op} "${fname_reg}" -print0 | xargs -0 grep -Hn ${grep_op} --color "$2" | awk -F: ' BEGIN{ ORS="\0" } { if(null == arr[$1]){ arr[$1] = "true" print $1 } } ' | xargs -0 rm -f
echo "Remove success."
fi
exit 0
语法:
./find-in-file "。*" "[查找内容]"
例如:查找当前文件夹下所文件,搜索:“2
”
./find-in-file ".*" "2"
准备了3个文件:1.log、2.log、3.log
-> % cat 1.log
1
-> % cat 2.log
2
-> % cat 3.log
1
2
搜索结果:
-> % find-in-file ".*" "2"
./2.log:1:2
./3.log:2:2
结果格式为:[文件路径]
[行数]
[grep到的内容]