我想计算当前目录及其子目录中的所有文件,文件中的第一个字母(grep ???)是z还是Z?
尝试使用grep,head等但没有成功
答案 0 :(得分:1)
#!/bin/bash
# find all the files (replace directory with the path to your folder)
find directory -type f | \
# read each file path
while read -r f; do
# read the first character of the file
read -rn1 c < $f
# if the 1 character matches eith Z or z, then echo the path
[[ "$c" =~ Z|z ]] && echo $f
done | \
# count them
wc -l
# here's a one line version:
find directory -type f | while read -r f; do read -rn1 c < $f; [[ "$c" =~ Z|z ]] && echo $f; done | wc -l
答案 1 :(得分:0)
grep有一个-c选项,它返回它找到的命中数。