计数总目录中的文件 - 找到VS LS [英] Counting total files in directory - find vs ls

查看:212
本文介绍了计数总目录中的文件 - 找到VS LS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有一个原因

 找到。 -mindepth 1 -maxdepth 1 |厕所-l

建议对

  LS -1 |厕所-l

(或者相反?)

要算一个文件夹内的文件/目录总数

注:


  1. 这个问题是更关心的只是计数的东西。

  2. 有与领先的
  3. 没有文件
  4. 有可能是非标准的文件,说 \\它ñ


解决方案

第一个命令...

 找到。 -mindepth 1 -maxdepth 1 |厕所-l

...将列出与启动文件和目录。,而你的 LS 命令不会。等效 LS 命令是:

  LS -A |厕所-l

双方会给你同样的答案。正如人们在评论中指出,这两种会给你错误的答案,如果有包含嵌入式换行符项,因为上面的命令只是计算输出的行数。

下面是计算的文件数是独立的一种​​方式
文件名怪癖:

 找到。 -mindepth 1 -maxdepth 1 -print0 | xargs的-0i回声|厕所-l

这传递的文件名的xargs NUL 终止,而不是依靠换行符,然后只需xargs的打印每个文件的空行,我们计算输出的行数从的xargs

Is there a reason why

find . -mindepth 1 -maxdepth 1 | wc -l

is suggested against

ls -1 | wc -l

(or vice-versa ?)

to count the total number of files/directories inside a folder

Notes:

  1. This question is more concerned with just counting stuff.
  2. There are no files with leading .
  3. There may be non-standard files with say a \n in it.

解决方案

The first command...

find . -mindepth 1 -maxdepth 1 | wc -l

...will list files and directories that start with ., while your ls command will not. The equivalent ls command would be:

ls -A | wc -l

Both will give you the same answers. As folks pointed out in the comments, both of these will give you wrong answers if there are entries that contained embedded newlines, because the above commands are simply counting the number of lines of output.

Here's one way to count the number of files that is independent of filename quirks:

find . -mindepth 1 -maxdepth 1 -print0 | xargs -0i echo | wc -l

This passes the filenames to xargs with a NUL terminator, rather than relying on newlines, and then xargs simply prints a blank line for each file, and we count the number of lines of output from xargs.

这篇关于计数总目录中的文件 - 找到VS LS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆