Linux 所有文件夹和子文件夹的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14827686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:00:02  来源:igfitidea点击:

List of All Folders and Sub-folders

linuxls

提问by Sandeep540

In Linux, I want to find out all Folder/Sub-folder name and redirect to text file

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

I tried ls -alR > list.txt, but it gives all files+folders

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

采纳答案by Adem ?zta?

You can use find

您可以使用 find

find . -type d > output.txt

or tree

或者 tree

tree -d > output.txt

tree, If not installed on your system.

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

If you are using ubuntu

如果您正在使用 ubuntu

sudo apt-get install tree

If you are using mac os.

如果您正在使用mac os.

brew install tree

回答by Amber

find . -type d > list.txt

Will list all directories and subdirectories under the current path. If you want to list all of the directories under a path other than the current one, change the .to that other path.

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

If you want to exclude certain directories, you can filter them out with a negative condition:

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

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

回答by Tony Delroy

As well as findlisted in other answers, better shells allow both recurvsive globs and filtering of glob matches, so in zshfor example...

除了find在其他答案中列出之外,更好的 shell 允许递归全局匹配和全局匹配过滤,zsh例如......

ls -lad **/*(/)

...lists all directories while keeping all the "-l" details that you want, which you'd otherwise need to recreate using something like...

...列出所有目录,同时保留您想要的所有“-l”详细信息,否则您需要使用类似...

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

(not quite as easy as the other answers suggest)

(不像其他答案所建议的那么容易)

The benefit of find is that it's more independent of the shell - more portable, even for system()calls from within a C/C++ program etc..

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