bash 仅列出与模式匹配的目录名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15486373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:27:47  来源:igfitidea点击:

List only directories names which match a pattern

bashshell

提问by Jorge Vega Sánchez

I'm bit confused about the command or modifiers to obtain: List of directories (only directories, not including subdirectories) which names include a pattern.

我对获取的命令或修饰符有点困惑:名称包含模式的目录列表(仅目录,不包括子目录)。

Thanks in advance.

提前致谢。

采纳答案by Jorge Vega Sánchez

That's works for me.

这对我有用。

But it outputs also the directory characteristics, permissions, date, etc. and I want to display only the directory name.

但它也输出目录特征、权限、日期等,我只想显示目录名称。

ls -t | grep '^d' | grep 'pattern'

回答by choroba

You are probably after the -dswitch of ls:

您可能正在-d切换ls

ls -d *pattern*/
ls --directory *pattern*/

回答by Gilles Quenot

Try this :

尝试这个 :

printf '%s\n' *pattern*/

if you prefer all on the same line :

如果你更喜欢在同一行:

echo *pattern*/

or using basharray :

或使用bash数组:

arr=( *pattern*/ )
printf '%s\n' "${arr[@]%/}"