我想在某个目录中搜索某个字符串。然后我想按大小对文件进行排序(字节数就可以了)。这些文件是 .php 文件,但我认为其他非 php 文件不会有我正在寻找的字符串。我该怎么做呢?
我得到了 grep 部分:
grep -rl "foostring" ~/myfolder
您可以尝试遍历文件并使用wc -c
以字节为单位显示文件大小。
for f in ~/myfolder/*.php; do ##: Loop through the *.php files only.
size=$(wc -c < "$f") ##: Save the file size in a variables.
if grep -Fq foostring "$f"; then ##: -F means fixed strings and -q means quiet/silent nothing to output to stdout.
printf 'file [%s] has [%d] size.\n' "$f" "$size" ##: Print a fancy format using printf.
fi
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句