该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
当前回答
Ls和awk(不含grep)
No need to use grep since awk can perform regularexpressino check so it is enough to do this:ls -l | awk '/^d/ {print $9}'
ls -l列出有权限的文件 Awk滤波器输出 '/^d/'正则表达式,只搜索以字母d开头的行(作为目录),并查看第一行-权限 {print}将打印所有列 {print $9}将只打印ls -l输出中的第9列(name)
非常简单明了
其他回答
*/是一个文件名匹配模式,匹配当前目录中的目录。
只列出目录,我喜欢这个函数:
# 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本身没有关系;它是由shell/Bash完成的,这个过程被称为globbing。
这就是为什么echo */和ls -d */输出相同的元素。(-d标志使ls输出目录名,而不是目录内容。)
再加上一个完整的循环,检索每个文件夹的路径,使用Albert的答案和Gordans的组合。这应该很有用。
for i in $(ls -d /pathto/parent/folder/*/); do echo ${i%%/}; done
输出:
/pathto/parent/folder/childfolder1/
/pathto/parent/folder/childfolder2/
/pathto/parent/folder/childfolder3/
/pathto/parent/folder/childfolder4/
/pathto/parent/folder/childfolder5/
/pathto/parent/folder/childfolder6/
/pathto/parent/folder/childfolder7/
/pathto/parent/folder/childfolder8/
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 -1d */
如果你想要整理干净:
ls -1d */ | cut -c 1- | rev | cut -c 2- | rev | sort
记住:大写字符在排序中有不同的行为