How can I find and count number of files matching a given string?

删除回忆录丶 提交于 2019-12-06 01:22:08

问题


I want to find and count all the files on my system that begin with some string, say "foo", using only one line in bash.

I'm new to bash so I'd like to avoid scripting if possible - how can I do this using only simple bash commands and maybe piping in just one line?

So far I've been using find / -name foo*. This returns the list of files, but I don't know what to add to actually count the files.


回答1:


You can use

find / -type f -name 'foo*' | wc -l
  • Use the single-quotes to prevent the shell from expanding the asterisk.
  • Use -type f to include only files (not links or directories).
  • wc -l means "word count, lines only." Since find will list one file per line, this returns the number of files it found.



回答2:


find / -name foo* | wc -l should do it. Here is a link to man wc. wc -l counts the number of lines




回答3:


You can pipe it into wc

find / -name foo * | wc -l


来源:https://stackoverflow.com/questions/9397816/how-can-i-find-and-count-number-of-files-matching-a-given-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!