是否有一个bash命令来计算匹配模式的文件数量?

例如,我想获取目录中所有文件的计数,这些文件都符合这个模式:log*


当前回答

这是我经常做的事:

液晶日志* |awk 'END{print NR}'

其他回答

要计算所有内容,只需将ls管道到单词计数行:

ls | wc -l

要使用模式计数,首先将管道连接到grep:

ls | grep log | wc -l

这个问题的公认答案是错误的,但我有低代表,所以不能添加评论。

这个问题的正确答案由Mat给出:

shopt -s nullglob
logfiles=(*.log)
echo ${#logfiles[@]}

接受的答案的问题是wc -l计算换行符的数量,即使它们打印到终端,也将它们计算为'?'ls -l'的输出。这意味着当文件名包含换行符时,接受的答案失败。我已经测试了建议的命令:

ls -l log* | wc -l

而且即使只有一个文件名恰好包含换行符的文件与模式匹配,它也会错误地报告值2。例如:

touch log$'\n'def
ls log* -l | wc -l

如果您有很多文件,并且不想使用优雅的shopt -s nullglob和bash数组解决方案,您可以使用find等,只要不打印文件名(可能包含换行符)。

find -maxdepth 1 -name "log*" -not -name ".*" -printf '%i\n' | wc -l

这将找到所有匹配log*且不以。*开头的文件-“not name .*”是冗余的,但重要的是要注意,“ls”的默认值是不显示点文件,但find的默认值是包含它们。

这是一个正确答案,可以处理任何类型的文件名,因为文件名永远不会在命令之间传递。

但是,shopt nullglob答案是最好的答案!

这可以用标准POSIX shell语法完成。

下面是一个简单的count_entries函数:

#!/usr/bin/env sh

count_entries()
{
  # Emulating Bash nullglob 
  # If argument 1 is not an existing entry
  if [ ! -e "$1" ]
    # argument is a returned pattern
    # then shift it out
    then shift
  fi
  echo $#
}

对于紧凑的定义:

count_entries(){ [ ! -e "$1" ]&&shift;echo $#;}

特色POSIX兼容的文件计数器类型:

#!/usr/bin/env sh

count_files()
# Count the file arguments matching the file operator
# Synopsys:
# count_files operator FILE [...]
# Arguments:
# $1: The file operator
#   Allowed values:
#   -a FILE    True if file exists.
#   -b FILE    True if file is block special.
#   -c FILE    True if file is character special.
#   -d FILE    True if file is a directory.
#   -e FILE    True if file exists.
#   -f FILE    True if file exists and is a regular file.
#   -g FILE    True if file is set-group-id.
#   -h FILE    True if file is a symbolic link.
#   -L FILE    True if file is a symbolic link.
#   -k FILE    True if file has its `sticky' bit set.
#   -p FILE    True if file is a named pipe.
#   -r FILE    True if file is readable by you.
#   -s FILE    True if file exists and is not empty.
#   -S FILE    True if file is a socket.
#   -t FD      True if FD is opened on a terminal.
#   -u FILE    True if the file is set-user-id.
#   -w FILE    True if the file is writable by you.
#   -x FILE    True if the file is executable by you.
#   -O FILE    True if the file is effectively owned by you.
#   -G FILE    True if the file is effectively owned by your group.
#   -N FILE    True if the file has been modified since it was last read.
# $@: The files arguments
# Output:
#   The number of matching files
# Return:
#   1: Unknown file operator
{
  operator=$1
  shift
  case $operator in
    -[abcdefghLkprsStuwxOGN])
      for arg; do
        # If file is not of required type
        if ! test "$operator" "$arg"; then
          # Shift it out
          shift
        fi
      done
      echo $#
      ;;
    *)
      printf 'Invalid file operator: %s\n' "$operator" >&2
      return 1
      ;;
  esac
}

count_files "$@"

示例用法:

count_files -f log*.txt
count_files -d datadir*

计数没有循环的非目录条目:

#!/bin/sh

# Creates strings of as many dots as expanded arguments

# dotted string for entries matching star pattern
star=$(printf '%.0s.' ./*)
# dotted string for entries matching star slash pattern (directories)
star_dir=$(printf '%.0s.' ./*/)
# dotted string for entries matching dot star pattern
dot_star=$(printf '%.0s.' ./.*)
# dotted string for entries matching dot star slash pattern (directories)
dot_star_dir=$(printf '%.0s.' ./.*/)

# Print pattern matches count excluding directories matches
printf 'Files count: %d\n' $((
  ${#star} - ${#star_dir} +
  ${#dot_star} - ${#dot_star_dir}
))

一个重要的评论

(没有足够的声誉来评论)

这是BUGGY:

ls -1q some_pattern | wc -l

如果shop -s nullglob恰好被设置,它将打印所有常规文件的数量,而不仅仅是带有模式的文件的数量(在CentOS-8和Cygwin上测试)。谁知道他还有什么没用的毛病?

这是正确的,而且更快:

shopt -s nullglob; files=(some_pattern); echo ${#files[@]};

它完成了预期的工作。 运行时间不同。 第一个:CentOS是0.006,Cygwin是0.083(以防小心使用)。 第二:CentOS是0.000,Cygwin是0.003。