该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
该命令列出当前路径下的目录:
ls -d */
模式*/到底做什么?
我们如何在上面的命令中给出绝对路径(例如ls -d /home/alice/Documents),只列出该路径下的目录?
当前回答
树命令在这里也非常有用。默认情况下,它将完整地显示所有文件和目录,并使用一些ASCII字符显示目录树。
$ tree
.
├── config.dat
├── data
│ ├── data1.bin
│ ├── data2.inf
│ └── sql
| │ └── data3.sql
├── images
│ ├── background.jpg
│ ├── icon.gif
│ └── logo.jpg
├── program.exe
└── readme.txt
但是如果我们想要只获取目录,而不使用ASCII树,并且使用当前目录的完整路径,你可以这样做:
$ tree -dfi
.
./data
./data/sql
./images
论点是:
-d List directories only.
-f Prints the full path prefix for each file.
-i Makes tree not print the indentation lines, useful when used in conjunction with the -f option.
如果你想要绝对路径,你可以从指定当前目录的完整路径开始:
$ tree -dfi "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/data/sql
/home/alice/Documents/images
为了限制子目录的数量,你可以用-L level来设置子目录的最大级别,例如:
$ tree -dfi -L 1 "$(pwd)"
/home/alice/Documents
/home/alice/Documents/data
/home/alice/Documents/images
更多的争论可以看到与人树。
其他回答
如果你有空间在你的文件夹名称$9打印不工作尝试下面的命令
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
输出
ls -l yourfolder/alldata/ | grep '^d' | awk '{print $9" " $10}'
testing_Data
Folder 1
对于所有没有子文件夹的文件夹:
find /home/alice/Documents -maxdepth 1 -type d
对于所有带子文件夹的文件夹:
find /home/alice/Documents -type d
*/是一个文件名匹配模式,匹配当前目录中的目录。
只列出目录,我喜欢这个函数:
# 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'
}
我只是把它添加到我的.bashrc文件中(如果你只需要/想要一个会话,你也可以在命令行上输入它):
alias lsd='ls -ld */'
然后lsd会产生想要的结果。
我使用:
ls -d */ | cut -f1 -d'/'
这将创建一个没有结尾斜杠的单列——在脚本中很有用。