简体   繁体   English

使用 OSX 终端命令计算目录中的文件数

[英]Counting number of files in a directory with an OSX terminal command

I'm looking for a specific directory file count that returns a number.我正在寻找返回数字的特定目录文件计数。 I would type it into the terminal and it can give me the specified directory's file count.我会把它输入终端,它可以给我指定目录的文件数。

I've already tried echo find "'directory' | wc -l" but that didn't work, any ideas?我已经尝试过 echo find "'directory' | wc -l"但这没有用,有什么想法吗?

You seem to have the right idea.你似乎有正确的想法。 I'd use -type f to find only files:我会使用-type f只查找文件:

$ find some_directory -type f | wc -l

If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:如果您只想直接在此目录下的文件而不是递归搜索子目录,则可以添加-maxdepth标志:

$ find some_directory -maxdepth 1 -type f | wc -l

Open the terminal and switch to the location of the directory.打开终端并切换到目录的位置。

Type in:输入:

find . -type f | wc -l

This searches inside the current directory (that's what the . stands for) for all files, and counts them.这会在当前目录(即 . 代表的内容)内搜索所有文件,并对它们进行计数。

The fastest way to obtain the number of files within a directory is by obtaining the value of that directory's kMDItemFSNodeCount metadata attribute.获取目录中文件数量的最快方法是获取该目录的kMDItemFSNodeCount元数据属性的值。

mdls -name kMDItemFSNodeCount directory_name -raw|xargs

The above command has a major advantage over find . -type f | wc -l与 find 相比,上述命令有一个主要优势find . -type f | wc -l find . -type f | wc -l find . -type f | wc -l in that it returns the count almost instantly, even for directories which contain millions of files. find . -type f | wc -l几乎立即返回计数,即使对于包含数百万个文件的目录也是如此。

Please note that the command obtains the number of files , not just regular files .请注意,该命令获取文件的数量,而不仅仅是常规文件

I don't understand why folks are using 'find' because for me it's a lot easier to just pipe in 'ls' like so:我不明白为什么人们使用'find',因为对我来说,像这样输入'ls'要容易得多:

ls *.png | ls *.png | wc -l wc -l

to find the number of png images in the current directory.查找当前目录中 png 图像的数量。

我正在使用树,这就是方式:tree ph

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM