是否有一个bash命令来计算匹配模式的文件数量?
例如,我想获取目录中所有文件的计数,这些文件都符合这个模式:log*
是否有一个bash命令来计算匹配模式的文件数量?
例如,我想获取目录中所有文件的计数,这些文件都符合这个模式:log*
当前回答
这可以用标准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}
))
其他回答
你可以用bash安全地做到这一点(即不会被名称中有空格或\n的文件所bug):
$ shopt -s nullglob
$ logfiles=(*.log)
$ echo ${#logfiles[@]}
您需要启用nullglob,以便在没有匹配的文件时不会在$logfiles数组中获得*.log文本。(参见如何“撤消”一个“set -x”?有关如何安全重置的示例。)
这里有很多答案,但有些没有考虑在内
包含空格、换行符或控制字符的文件名 以连字符开头的文件名(想象一个名为-l的文件) 隐藏文件,以点开始(如果glob是*.log而不是log*) 匹配glob的目录(例如,一个名为logs的目录匹配log*) 空目录(即结果为0) 非常大的目录(列出所有目录会耗尽内存)
这里有一个解决方案可以处理所有这些问题:
ls 2>/dev/null -Ubad1 -- log* | wc -l
解释:
-U causes ls to not sort the entries, meaning it doesn't need to load the entire directory listing in memory -b prints C-style escapes for nongraphic characters, crucially causing newlines to be printed as \n. -a prints out all files, even hidden files (not strictly needed when the glob log* implies no hidden files) -d prints out directories without attempting to list the contents of the directory, which is what ls normally would do -1 makes sure that it's on one column (ls does this automatically when writing to a pipe, so it's not strictly necessary) 2>/dev/null redirects stderr so that if there are 0 log files, ignore the error message. (Note that shopt -s nullglob would cause ls to list the entire working directory instead.) wc -l consumes the directory listing as it's being generated, so the output of ls is never in memory at any point in time. -- File names are separated from the command using -- so as not to be understood as arguments to ls (in case log* is removed)
shell会将log*扩展到完整的文件列表,如果文件很多,可能会耗尽内存,所以通过grep运行会更好:
ls -Uba1 | grep ^log | wc -l
最后一种方法在不使用大量内存的情况下处理超大文件目录(尽管它使用了子shell)。不再需要-d,因为它只列出当前目录的内容。
如果您有很多文件,并且不想使用优雅的shopt -s nullglob和bash数组解决方案,您可以使用find等,只要不打印文件名(可能包含换行符)。
find -maxdepth 1 -name "log*" -not -name ".*" -printf '%i\n' | wc -l
这将找到所有匹配log*且不以。*开头的文件-“not name .*”是冗余的,但重要的是要注意,“ls”的默认值是不显示点文件,但find的默认值是包含它们。
这是一个正确答案,可以处理任何类型的文件名,因为文件名永远不会在命令之间传递。
但是,shopt nullglob答案是最好的答案!
ls -1 log* | wc -l
这意味着每行列出一个文件,然后将其输送到单词计数命令,参数切换到计数行。
这是我经常做的事:
液晶日志* |awk 'END{print NR}'