该命令列出当前路径下的目录:

ls -d */

模式*/到底做什么?

我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?


当前回答

供您参考,如果您想以多行打印所有文件,您可以执行ls -1,这将以单独的行打印每个文件。 file1 file2 file3

其他回答

我只是把它添加到我的.bashrc文件中(如果你只需要/想要一个会话,你也可以在命令行上输入它):

alias lsd='ls -ld */'

然后lsd会产生想要的结果。

使用Perl:

ls | perl -nle 'print if -d;'

回答最初的问题,*/和ls本身没有关系;它是由shell/Bash完成的,这个过程被称为globbing。

这就是为什么echo */和ls -d */输出相同的元素。(-d标志使ls输出目录名,而不是目录内容。)

*/是一个文件名匹配模式,匹配当前目录中的目录。

只列出目录,我喜欢这个函数:

# Long list only directories
llod () {
  ls -l --color=always "$@" | grep --color=never '^d'
}

把它放在你的。bashrc文件中。

使用例子:

llod       # Long listing of all directories in current directory
llod -tr   # Same but in chronological order oldest first
llod -d a* # Limit to directories beginning with letter 'a'
llod -d .* # Limit to hidden directories

注意:如果您使用-i选项,它将中断。这里有一个解决方案:

# Long list only directories
llod () {
  ls -l --color=always "$@" | egrep --color=never '^d|^[[:digit:]]+ d'
}

这是我正在用的

ls -d1 /Directory/Path/*;