该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
该命令列出当前路径下的目录:
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'
}
其他回答
这里是一个使用树的变种,它只在单独的行上输出目录名,是的,它很丑,但是,嘿,它工作。
tree -d | grep -E '^[├|└]' | cut -d ' ' -f2
或者用awk
tree -d | grep -E '^[├|└]' | awk '{print $2}'
然而,这可能更好,并且将保留目录名之后的/。
ls -l | awk '/^d/{print $9}'
file * | grep directory
输出(在我的机器上)——
[root@rhel6 ~]# file * | grep directory
mongo-example-master: directory
nostarch: directory
scriptzz: directory
splunk: directory
testdir: directory
以上输出可以通过使用cut进行进一步细化:
file * | grep directory | cut -d':' -f1
mongo-example-master
nostarch
scriptzz
splunk
testdir
* could be replaced with any path that's permitted
file - determine file type
grep - searches for string named directory
-d - to specify a field delimiter
-f1 - denotes field 1
显示没有/的文件夹列表:
ls -d */|sed 's|[/]||g'
使用Test -d测试项目是否为目录:
for i in $(ls); do test -d $i && echo $i ; done
使用Perl:
ls | perl -nle 'print if -d;'