我想只列出指定路径中的目录(ls没有这样的选项)。 另外,这可以用单行命令完成吗?
当前回答
可以使用ls -d */或tree -d
另一种解决方案是通配符,但这取决于您正在使用的shell以及是否支持目录通配符。
例如ZSH:
zsh # ls *(/)
其他回答
查找指定路径-type d
如果你不想在子目录中递归,你可以这样做:
查找指定路径-type d -mindepth 1 -maxdepth 1
请注意,“点”目录(其名称以。开头)也将被列出;但不是特殊的目录。也不……如果你不想要“。”目录,你可以把它们grep出来:
查找指定路径-type d -mindepth 1 -maxdepth 1 | grep -v '^\.'
### If you need full path of dir and list selective dir with "name" of dir(or dir_prefix*):
find $(pwd) -maxdepth 1 -type d -name "SL*"
如果我有这个目录:
ls -l
lrwxrwxrwx 1 nagios nagios 11 août 2 18:46 conf_nagios -> /etc/icinga
-rw------- 1 nagios nagios 724930 août 15 21:00 dead.letter
-rw-r--r-- 1 nagios nagios 12312 août 23 00:13 icinga.log
-rw-r--r-- 1 nagios nagios 8323 août 23 00:12 icinga.log.gz
drwxr-xr-x 2 nagios nagios 4096 août 23 16:36 tmp
要获取所有目录,使用-L解析链接:
ls -lL | grep '^d'
drwxr-xr-x 5 nagios nagios 4096 août 15 21:22 conf_nagios
drwxr-xr-x 2 nagios nagios 4096 août 23 16:41 tmp
没有- l:
Ls -l | grep '^d'
drwxr-xr-x 2 nagios nagios 4096 août 23 16:41 tmp
Conf_nagios目录缺失。
我发现在我面前有很多好的答案。但我想添加一个命令,我们已经使用它几次,所以很容易列出所有的目录较少的努力:
cd
(注意:cd后面有空格),按tab键两次,它将只列出当前工作目录下的所有目录。希望这是容易使用的。如果有任何问题请让我知道。谢谢。
答案取决于您的shell。
以zsh为例,您可以执行以下操作:
echo *(/)
并且将显示当前工作目录中的所有目录。
更多信息请参见man zshexpn。
另一种方法是使用find(1),它应该适用于大多数Unix版本:
find . -maxdepth 1 -type d -print
find(1)有很多用途,所以我绝对推荐man find。