22

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

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

jww
  • 97,681
  • 90
  • 411
  • 885
Sandeep540
  • 897
  • 3
  • 13
  • 38

3 Answers3

58

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
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
9
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
Amber
  • 507,862
  • 82
  • 626
  • 550
3

As well as find listed in other answers, better shells allow both recurvsive globs and filtering of glob matches, so in zsh for example...

ls -lad **/*(/)

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

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..

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • In this Option , is there an Option to exclude one single folder... I have a ~snapshot folder in it, which I want to exclude? – Sandeep540 Feb 12 '13 at 09:58
  • @Sandeep50: in zsh, yes: `setopt EXTENDED_GLOB`, then `ls -lad **/*~**/~snapshot(/)`. Details: from `man zshall` / "x~y (Requires EXTENDED_GLOB to be set.) Match anything that matches the pattern x but does not match y. [...]". (There's also "^x (Requires EXTENDED_GLOB to be set.) Matches anything except the pattern x. [further explanation of slightly different syntax + example]") – Tony Delroy Feb 13 '13 at 01:54