List all folders and subfolders

On Linux, I want to find out the entire folder / subfolder name and redirect to a text file

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

+6
source share
3 answers

You can use find

 find . -type d > output.txt 

or tree

 tree -d > output.txt 

tree if not installed on your system.

If you are using ubuntu

 sudo apt-get install tree 

If you are using mac os .

 brew install tree 
+33
source
 find . -type d > list.txt 

List all directories and subdirectories in the current path. If you want to list all directories in a path other than the current one, change it . to this other way.

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

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

Like find , listed in other answers, the best shells allow you to perform both recurvsive globs and glob filtering, so in zsh for example ...

 ls -lad **/*(/) 

... lists all directories, preserving all the required "-l" details that you would need to recreate using something like ...

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

(not as easy as other answers suggest)

The advantage of find is that it is more shell independent - more portable, even for calls to system() from a C / C ++ program, etc.

+2
source

All Articles