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

ls -d */

模式*/到底做什么?

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


当前回答

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

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

# 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'
}

其他回答

我发现这个解决方案是最舒服的,我添加到列表中:

find * -maxdepth 0 -type d

不同之处在于它在开始时没有./,并且文件夹名称可以随时使用。

只从“这里”列出目录的一行程序。

文件计数。

for i in `ls -d */`; do g=`find ./$i -type f -print| wc -l`; echo "Directory $i contains $g files."; done

下面是我只列出目录名的方法:

ls -1d /some/folder/*/ | awk -F "/" "{print \$(NF-1)}"

这里是一个使用树的变种,它只在单独的行上输出目录名,是的,它很丑,但是,嘿,它工作。

tree -d | grep -E '^[├|└]' | cut -d ' ' -f2

或者用awk

tree -d | grep -E '^[├|└]' | awk '{print $2}'

然而,这可能更好,并且将保留目录名之后的/。

ls -l | awk '/^d/{print $9}'

一个当前目录的普通列表,它将是:

ls -1d */

如果你想要整理干净:

ls -1d */ | cut -c 1- | rev | cut -c 2- | rev | sort

记住:大写字符在排序中有不同的行为