linux查找目录下所有的文件内容中是否有指定的字符串
脚本名称:ffind
安装命令
wget -O /usr/bin/ffind http://bsxtapp.gdbs.gov.cn/data/admin/file/seafile/f/b37884bff6e4418186f8/?dl=1 && chmod 755 /usr/bin/ffind
#!/bin/sh
echo_x()
{
if [ "${1}" = "info" ]; then
echo -e "\033[37m${2}\033[0m";
elif [ "${1}" = "notice" ];then
echo -e "\033[36m${2}\033[0m";
elif [ "${1}" = "warn" ];then
echo -e "\033[33m${2}\033[0m";
elif [ "${1}" = "error" ]; then
echo -e "\033[31m${2}\033[0m";
fi
}
core()
{
file_var="${1}"
find_str="${2}"
if [ ! -f "${file_var}" ]; then
return
fi
if [ `file -i "${file_var}" | grep "charset=binary" | wc -l` != 0 ]; then
return
fi
target_num=`cat "${file_var}" | grep "${find_str}" | wc -l`
if [ "${target_num}" == 0 ]; then
return
fi
echo_x "error" "查找到${target_num}处: ${file_var}"
line_num=0
cat "${file_var}" | while read line; do
line_num=$((${line_num} + 1))
if [[ "${line}" == *"${find_str}"* ]]; then
if [ "${#line}" -ge 350 ]; then
target_location=`awk -v a="${line}" -v b="${find_str}" 'BEGIN{print index(a,b)}'`
a_num=$(($((350-${#find_str}))/2))
start_num=$((${target_location}-${a_num}))
end_num=$((${target_location}+${a_num}))
if [ "${start_num}" -lt 0 ]; then
start_num=1;
fi
if [ "${end_num}" -ge ${#line} ]; then
end_num=${#line};
fi
target_str=`echo "${line}" | cut -c ${
start_num}-${
end_num}`
echo -e "\033[33m第${line_num}行(截断显示): \033[0m${target_str}"
else
echo -e "\033[33m第${line_num}行: \033[0m${line}"
fi
fi
done
echo_x "notice" "---------------------------------------------"
}
use_help()
{
echo_x "error" "ffind ${*} 参数错误"
echo_x "notice" "Model 1: find /home | ./ffind \"target str\""
echo_x "notice" "Model 2: ./ffind \"target dir\" \"target str\""
}
if [ $
target_dir="${1}"
find_str="${2}"
if [ ! -d "${target_dir}" ]; then
use_help
exit 0
fi
find "${target_dir}" -type f | while read file_var; do
core "${file_var}" "${find_str}"
done
elif [ $
var_stdin=`cat /dev/stdin`
if [ "${#var_stdin}" != 0 ]; then
find_str="${1}"
for file_var in ${var_stdin}; do
core "${file_var}" "${find_str}"
done
fi
else
use_help ${*}
exit 0
fi
使用示范1:
[root@localhost logs]# ./ffind /home/wnh/filesync/ "const string&"
查找到1处: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //实现rm_dir(const string& path)函数删除目录中的所有文件,在rm_dir()中遍历每一个文件,如果遇到目录文件,则递归删除该目录文件。
---------------------------------------------
查找到16处: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋转hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一个hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己带的算法
---------------------------------------------
使用示范2(管道):
[root@localhost logs]# find /home/wnh/filesync/ | grep -E '.h$|.cpp$' | ./ffind "const string&"
查找到1处: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //实现rm_dir(const string& path)函数删除目录中的所有文件,在rm_dir()中遍历每一个文件,如果遇到目录文件,则递归删除该目录文件。
---------------------------------------------
查找到16处: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋转hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一个hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己带的算法
---------------------------------------------