如何计算找到命令返回的文件数?

时间:2015-10-22 06:37:40

标签: arrays bash count find

这里我得到1的结果而不是应该找到的两个文件:

mkdir -p mytestdir001
mkdir -p mytestdir002
LIST=`find -maxdepth 1 -name "mytestdir???"`
echo ${#LIST[@]}

5 个答案:

答案 0 :(得分:2)

使LIST成为数组而不是变量:

LIST=( `find -maxdepth 1 -name "mytestdir???"` )

也开始使用$()而不是旧的``:

LIST=( $(find -maxdepth 1 -name "mytestdir???") )

答案 1 :(得分:0)

你为什么不用:

find -maxdepth 1 -name "mytestdir???" | wc

如果你的真正目标是知道这个号码吗?

答案 2 :(得分:0)

您可以使用 wordcount wc):

find -maxdepth 1 -name "mytestdir???" | wc

这将为您提供如下输出:

2       2      30

这些是:

  • 行数
  • 字数
  • 字符数

使用 wc -l <​​/ strong>来获取行数。

答案 3 :(得分:0)

你可以将find的输出输入到wc并计算行数,因为每个文件都由一条不同的行表示 - 根据我的经验,这就是find。

http://linux.die.net/man/1/wc

示例

  

[user @ machine find] #find。 -name&#34; * .file&#34;

     

./ 3.file

     

./ 1.FILE

     

./ 2.文件

     

[user @ machine find] #find。 -name&#34; * .file&#34; | wc -l <​​/ p>      

3

答案 4 :(得分:0)

var ele = $compile(tag)($scope);
angular.element('div').append(ele);

故障:

ls | awk '/^mytestdir/{a++}END{print a}' # 2