How would I find out how many students have an o
in their account name in my server. Their home directory is in /home/students
I have tried
grep *o* /home/students
this does not work.
你可以说:
find /home/students -mindepth 1 -maxdepth 1 -type d -name "*o*"
这将返回包含o
.
为了找到计数,管道到wc -l
:
find /home/students -mindepth 1 -maxdepth 1 -type d -name "*o*" | wc -l
这篇文章解释了为什么你不应该解析ls
.
这不会列出目录内容:
ls -1d *o* | wc -l
使用具有适当文件模式的数组:
o_students=( /home/students/*o*/ )
echo "${#o_students[@]}"
利用
ls -1 /home/students | grep o | wc -l
- 你不能这样做,ls -1 /home/students/*o*
因为它会列出匹配目录中的文件。