该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
当前回答
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'
*/是一个文件名匹配模式,匹配当前目录中的目录。
只列出目录,我喜欢这个函数:
# 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 -1d /some/folder/*/ | awk -F "/" "{print \$(NF-1)}"
如果你想知道为什么'ls -d */'的输出会给你两个斜杠,比如:
[prompt]$ ls -d */
app// cgi-bin// lib// pub//
这可能是因为您的shell或会话配置文件将ls命令别名为包含-F标志的ls版本。该标志将一个字符附加到每个输出名称(这不是普通文件),表明它是什么类型的文件。因此,一个斜杠来自匹配模式'*/',另一个斜杠是附加的类型指示符。
要解决这个问题,当然可以为ls定义一个不同的别名。然而,为了暂时不调用别名,你可以在命令前加上反斜杠:
\ls -d */
*/是一个匹配当前目录中所有子目录的模式(*将匹配所有文件和子目录;/将其限制为目录)。类似地,要列出/home/alice/Documents下的所有子目录,请使用ls -d /home/alice/Documents/*/