grep - Count of no of files which contain a particular string in its name
问题描述
I am simply trying to count how many files in a given directory contain a particular string anywhere in its name ( not the total occurance of string ) & ignore hidden directories
Example
>> ls -a
.hidden_dir
string1.nfo
'string1 - string.exe'
string5-.mkv
Here , ignoring the hidden directory , 3 files contain the letter "string" anywhere in their name , so i expect grep to return 3 correctly but its not working & returns 0 instead. Here's the grep command i am using -
grep --exclude-dir=".*" -l "string" * | wc -l
解决方案
You can use this pure bash code:
arr=(*string*) && printf '%d\n' "${#arr[@]}"
Details:
arr=(*string*)
: Find all entries in current directories that have namestring
anywhere and put them into arrayarr
printf '%d\n' "${#arr[@]}"
: Print length of arrayarr
Based on comments below here is a find
solution:
find . -type f -maxdepth 1 -iname '*string*' ! -name '*.txt' \
-exec bash -c 'printf "%s\n" "${@//*/.}"' _ {} + | wc -l
推荐阅读
- pytest - 如何延长夹具(pytest)中标记的寿命上下文?
- haskell - 如何显示两个输入是否相同
- python - 在类构造函数中定义逻辑以根据python中的其他构造函数参数修改实例变量
- json - Scala 映射选项
- rxdatasources - 如果 UITableview 中有大量数据,则 BehaviorRelay/Variable 缓慢的 RxDataSources
- css - 使用 sass 自动增加类特异性
- html - 如何在 CSS 网格中偏移 div
- servlets - 在 felix jetty 中部署包时,Servlet 类不是 javax.servlet.Servlet
- apache-nifi - 一起处理传入队列中存在的 nifi 流文件
- protocol-buffers - 由于默认值,Protobuf 忽略 bool 和 ints 值