所有文件夹和子文件夹列表[关闭]

问题描述 投票:6回答:3

在Linux中,我想找出所有文件夹/子文件夹名称并重定向到文本文件

我尝试了ls -alR> list.txt,但它提供了所有文件+文件夹

linux ls
3个回答
32
投票

你可以使用find

find . -type d > output.txt

或者tree

tree -d > output.txt

tree,如果没有安装在您的系统上。

如果你正在使用ubuntu

sudo apt-get install tree

如果你正在使用mac os

brew install tree

6
投票
find . -type d > list.txt

将列出当前路径下的所有目录和子目录。如果要列出当前路径以外的所有目录,请将.更改为其他路径。

如果要排除某些目录,可以使用否定条件过滤掉它们:

find . -type d ! -name "~snapshot" > list.txt

2
投票

除了在其他答案中列出的find,更好的shell允许recurvsive globs和过滤glob glob匹配,所以在zsh例如......

ls -lad **/*(/)

...列出所有目录,同时保留所需的所有“-l”详细信息,否则需要使用类似的东西重新创建...

find . -type d -exec ls -ld {} \;

(不像其他答案那么简单)

find的好处是它更独立于shell - 更便携,即使是来自C / C ++程序的system()调用等。

© www.soinside.com 2019 - 2024. All rights reserved.