Arrays 如何计算find命令返回的文件数?
这里我得到的结果是1,而不是应该找到的两个文件:Arrays 如何计算find命令返回的文件数?,arrays,bash,count,find,Arrays,Bash,Count,Find,这里我得到的结果是1,而不是应该找到的两个文件: mkdir -p mytestdir001 mkdir -p mytestdir002 LIST=`find -maxdepth 1 -name "mytestdir???"` echo ${#LIST[@]} 你为什么不使用: find -maxdepth 1 -name "mytestdir???" | wc 如果你的真正目标是知道数字?你可以使用字数(wc): 这将为您提供如下输出: 2 2 30 这些是: 行
mkdir -p mytestdir001
mkdir -p mytestdir002
LIST=`find -maxdepth 1 -name "mytestdir???"`
echo ${#LIST[@]}
你为什么不使用:
find -maxdepth 1 -name "mytestdir???" | wc
如果你的真正目标是知道数字?你可以使用字数(
wc
):
这将为您提供如下输出:
2 2 30
这些是:
- 行数
- 字数
- 字符数
使用wc-l仅获取行数。使
列表成为数组而不是变量:
LIST=( `find -maxdepth 1 -name "mytestdir???"` )
还可以开始使用$()
而不是旧的```:
LIST=( $(find -maxdepth 1 -name "mytestdir???") )
您可以将find的输出通过管道传输到wc中,并计算行数,因为每个文件都由一个不同的行表示——根据我的经验,find就是这种情况
范例
[user@machine查找]#查找-名称“*.file”
/3.文件
/1.1文件
/2.1文件
[user@machine查找]#查找-名称“*.file”| wc-l
三,
细分:
ls | awk '
# Match every folder / file starting with mytestdir
/^mytestdir/{a++}
# Print counted matches
END{print a}' # 2
也许你需要用一个点来说明从哪里开始查找-最大深度…
ls | awk '/^mytestdir/{a++}END{print a}' # 2
ls | awk '
# Match every folder / file starting with mytestdir
/^mytestdir/{a++}
# Print counted matches
END{print a}' # 2