683

Is there any linux command that I can call from a Bash script that will print the directory structure in the form of a tree, e.g.,

folder1
   a.txt
   b.txt
folder2
   folder3
7
  • 15
    Just run find. Or find . -not -path '*/\.*' to hide files and folders starting with .. If you want to have output with spaces, as in the question, use it with this "find prettifier" script: find . -not -path '*/\.*' | python -c "import sys as s;s.a=[];[setattr(s,'a',list(filter(lambda p: c.startswith(p+'/'),s.a)))or (s.stdout.write(' '*len(s.a)+c[len(s.a[-1])+1 if s.a else 0:])or True) and s.a.append(c[:-1]) for c in s.stdin]"
    – user
    Jun 14, 2014 at 0:49
  • 14
    Shouldn't such questions get migrated to SuperUser rather than closed ?
    – Balmipour
    Oct 10, 2015 at 10:34
  • 15
    i dont think this question deserves to be closed as "off topic". The tags seem to be right. Dec 1, 2015 at 8:27
  • 25
    The policy of closing questions without migrating is harmful to both stackoverflow and human knowledge in general. In the last 3 days, every single questions I googled and came across was closed for similar reasoning, and no more activity was able to happen. This means no one can update it, no one can give a better answer, and it makes stackoverflow look shortsighted or elitist. Stackoverflow should consider requiring a migration when a topic is found to have these conditions.
    – Nay
    Nov 27, 2016 at 19:32
  • 6
    I agree with @NickYeates I am here in late September of 2017 still finding answers to this same question. Think long term when we design these question and answer policies!
    – Alex
    Sep 28, 2017 at 20:38

11 Answers 11

979

Is this what you're looking for tree? It should be in most distributions (maybe as an optional install).

~> tree -d /proc/self/
/proc/self/
|-- attr
|-- cwd -> /proc
|-- fd
|   `-- 3 -> /proc/15589/fd
|-- fdinfo
|-- net
|   |-- dev_snmp6
|   |-- netfilter
|   |-- rpc
|   |   |-- auth.rpcsec.context
|   |   |-- auth.rpcsec.init
|   |   |-- auth.unix.gid
|   |   |-- auth.unix.ip
|   |   |-- nfs4.idtoname
|   |   |-- nfs4.nametoid
|   |   |-- nfsd.export
|   |   `-- nfsd.fh
|   `-- stat
|-- root -> /
`-- task
    `-- 15589
        |-- attr
        |-- cwd -> /proc
        |-- fd
        | `-- 3 -> /proc/15589/task/15589/fd
        |-- fdinfo
        `-- root -> /

27 directories

sample taken from maintainer's web page.

You can add the option -L # where # is replaced by a number, to specify the max recursion depth.

Remove -d to display also files.

18
  • 78
    Note for any visitor seeing this: remove -d to display files also!
    – q9f
    May 11, 2013 at 8:49
  • 38
    Note for any visitor seeing this: The man page lists a truckload of more flags for you :)
    – oivvio
    Apr 26, 2014 at 18:52
  • 61
    To install on Mac OS X w/Homebrew: brew install tree
    – funfuntime
    Dec 23, 2014 at 8:48
  • 2
    To install on cygwin apt-cyg install tree (assuming you've installed apt-cyg)
    – blockloop
    Feb 19, 2015 at 19:36
  • 7
    Not even Ubuntu 16.04 comes with this. Use apt-get install tree will install it. Sep 6, 2017 at 9:52
444

You can use this one:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

It will show a graphical representation of the current sub-directories without files in a few seconds, e.g. in /var/cache/:

   .
   |-apache2
   |---mod_cache_disk
   |-apparmor
   |-apt
   |---archives
   |-----partial
   |-apt-xapian-index
   |---index.1
   |-dbconfig-common
   |---backups
   |-debconf

Source

9
  • 13
    If you want it with spaces, more like the OP requested, then this: ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\// /g' -e 's/^/ /'
    – Ben
    Oct 4, 2013 at 0:56
  • any way I can make this ignore dotfiles? E.g. prevent it from listing the contents of .git?
    – GMA
    Apr 25, 2014 at 6:31
  • @GeorgeMillo see my comment
    – user
    Jun 14, 2014 at 0:55
  • 108
    with files: find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/" Jun 29, 2018 at 19:31
  • 3
    @Ben thanks for that, all other (non-tree) answers produce a result that does not look perfectly correct to me. For example in this answer, there should be a line going down from apt then horizontally to archives, instead it comes down from . and goes to archives, only because of more indentation you can guess that it's actually a subfolder of apt. So you could as well just leave the lines away, it's at least not misleading then.
    – msa
    Nov 6, 2020 at 11:07
79

This command works to display both folders and files.

find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"

Example output:

.
 |-trace.pcap
 |-parent
 | |-chdir1
 | | |-file1.txt
 | |-chdir2
 | | |-file2.txt
 | | |-file3.sh
 |-tmp
 | |-json-c-0.11-4.el7_0.x86_64.rpm

Source: Comment from @javasheriff here. Its submerged as a comment and posting it as answer helps users spot it easily.

4
  • 1
    for python3 I found find . |grep -vE 'pyc|swp|__init' | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/" working well May 20, 2020 at 9:55
  • 1
    This was a great help as we didn't have tree installed on a certain server. This will most likely work on any standard Linux system.
    – jaw
    Jan 7, 2022 at 7:11
  • Thanks. How can we make the results in alphabetical order?
    – laviex
    Dec 5, 2022 at 14:49
  • @lavlex you can order the top-level directories with find * | sed ... but that's as far as I'm willing to think at the moment.
    – FlipMcF
    Aug 24 at 19:54
42

Since it was a successful comment, I am adding it as an answer:
To print the directory structure in the form of a tree,
WITH FILES

 find . | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/" 
1
  • 5
    If you would like to sort your result list then combine the nice solution above with sort this way: find . | sort | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
    – zappee
    Oct 14, 2021 at 13:18
22

Since I was not too happy with the output of other (non-tree) answers (see my comment at Hassou's answer), I tried to mimic trees output a bit more.

It's similar to the answer of Robert but the horizontal lines do not all start at the beginning, but where there are supposed to start. Had to use perl though, but in my case, on the system where I don't have tree, perl is available.

ls -aR | grep ":$" | perl -pe 's/:$//;s/[^-][^\/]*\//    /g;s/^    (\S)/└── \1/;s/(^    |    (?= ))/│   /g;s/    (\S)/└── \1/'

Output (shortened):

.
└── fd
└── net
│   └── dev_snmp6
│   └── nfsfs
│   └── rpc
│   │   └── auth.unix.ip
│   └── stat
│   └── vlan
└── ns
└── task
│   └── 1310
│   │   └── net
│   │   │   └── dev_snmp6
│   │   │   └── rpc
│   │   │   │   └── auth.unix.gid
│   │   │   │   └── auth.unix.ip
│   │   │   └── stat
│   │   │   └── vlan
│   │   └── ns

Suggestions to avoid the superfluous vertical lines are welcome :-)

I still like Ben's solution in the comment of Hassou's answer very much, without the (not perfectly correct) lines it's much cleaner. For my use case I additionally removed the global indentation and added the option to also ls hidden files, like so:

ls -aR | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//  /g'

Output (shortened even more):

.
  fd
  net
    dev_snmp6
    nfsfs
    rpc
      auth.unix.ip
    stat
    vlan
  ns
1
  • The only way to get rid of the unwanted vertical lines when processing the output of ls -R is to proceed from last to first line. See the awk-based solution I provided, which you could easily adapt to perl. Oct 9, 2021 at 20:26
21

To add Hassou's solution to your .bashrc, try:

alias lst='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/   /'"'"' -e '"'"'s/-/|/'"'"
2
  • 6
    Beware of the newline character at the end of the first line if copying this directly
    – Rahul
    Aug 5, 2016 at 16:46
  • 3
    Nice alias. But there is missing ' ' (2 single quote chars) at the end. It works even without it, but... if you want to add some more commands at the end you will see the literal is not complete. So it should go alias lst='ls -R | grep ":$" | sed -e '"'"'s/:$//'"'"' -e '"'"'s/[^-][^\/]*\//--/g'"'"' -e '"'"'s/^/ /'"'"' -e '"'"'s/-/|/'"'"''
    – Hero Qu
    Feb 15, 2019 at 19:00
9

The best answer is, of course, tree. But, to improve on other answers that rely on grepping the output of ls -R, here is a shell script that uses awk to print a tree of subdirectories. First, an example of output:

.
└── matching
    ├── bib
    ├── data
    │   └── source
    │       └── html
    ├── data
    │   └── plots
    ├── method
    │   ├── info
    │   └── soft
    │       ├── imgs
    │       │   ├── ascii
    │       │   └── symbol
    │       └── js
    └── ms

Then, the code:

ls -qLR 2>/dev/null \
| grep '^./' \
| sed -e 's,:$,,' \
| awk '
    function tip(new) { stem = substr(stem, 1, length(stem) - 4) new }
    {
        path[NR] = $0
    }
    END {
        elbow = "└── "; pipe = "│   "; tee = "├── "; blank = "    "
        none = ""
        #
        # Model each stem on the previous one, going bottom up.
        for (row = NR; row > 0; row--) {
            #
            # gsub: count (and clean) all slash-ending components; hence,
            # reduce path to its last component.
            growth = gsub(/[^/]+\//, "", path[row]) - slashes
            if (growth == 0) {
                tip(tee)
            }
            else if (growth > 0) {
                if (stem) tip(pipe) # if...: stem is empty at first!
                for (d = 1; d < growth; d++) stem = stem blank
                stem = stem elbow
            }
            else {
                tip(none)
                below = substr(stem, length(stem) - 4, 4)
                if (below == blank) tip(elbow); else tip(tee)
            }
            path[row] = stem path[row]
            slashes += growth
        }
        root = "."; print root
        for (row = 1; row <= NR; row++) print path[row]
    }
'

The code gives better-looking results than other solutions because in a tree of subdirectories, the decorations in any branch depend on the branches below it. Hence, we need to process the output of ls -R in reverse order, from the last line to the first.

2
  • What system did you test it on? I get an error awk: nonterminated character class [^ on gsub line on Mac.
    – Andrei
    Jan 4 at 21:25
  • @Andrei I tested it on Arch, where awk is symlinked to gawk (= GNU awk). So this may be another pesky awk/gawk difference. Perhaps you should escape the slash inside the character class: /[^\/]+\// Jan 6 at 14:34
8

I'm prettifying the output of @Hassou's answer with:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//──/g' -e 's/─/├/' -e '$s/├/└/'

This is much like the output of tree now:

.
├─pkcs11
├─pki
├───ca-trust
├─────extracted
├───────java
├───────openssl
├───────pem
├─────source
├───────anchors
├─profile.d
└─ssh

You can also make an alias of it:

alias ltree=$'ls -R | grep ":$" | sed -e \'s/:$//\' -e \'s/[^-][^\/]*\//──/g\' -e \'s/─/├/\' -e \'$s/├/└/\''

BTW, tree is not available in some environment, like MinGW. So the alternate is helpful.

2
  • gitbash on windows does not like the last expression, it says that it is not terminated Apr 22, 2020 at 10:51
  • 2
    @LeosLiterak: On Windows, native tree should be available with Command Prompt (cmd) or PowerShell though :-)
    – msa
    Nov 6, 2020 at 10:43
5

Adding the below function in bashrc lets you run the command without any arguments which displays the current directory structure and when run with any path as argument, will display the directory structure of that path. This avoids the need to switch to a particular directory before running the command.

function tree() {
    find ${1:-.} | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
}

This works in gitbash too.

Source: Comment from @javasheriff here

4

You can also use the combination of find and awk commands to print the directory tree. For details, please refer to "How to print a multilevel tree directory structure using the linux find and awk combined commands"

find . -type d | awk -F'/' '{ 
depth=3;
offset=2;
str="|  ";
path="";
if(NF >= 2 && NF < depth + offset) {
    while(offset < NF) {
        path = path "|  ";
        offset ++;
    }
    print path "|-- "$NF;
}}'
0

Combining and extending existing answers into t shell function

t() {
  find -E "${1:-.}" -maxdepth "${2:-3}" \
    -not -regex ".*\/((.idea|.git|.venv|node_modules|venv)\/.*|.DS_Store)" \
  | sort | sed \
    -e "s/[^-][^\/]*\// ├ /g" \
    -e "s/├ \//├ /g" \
    -e "s/├  ├/│  ├/g" \
    -e "s/├  ├/│  ├/g" \
    -e "s/├  │/│  │/g" \
    -e '$s/├/└/'
}

Works on Mac:

$ t
.
 ├ src
 │  ├ .idea
 │  ├ plugins
 │  │  ├ .flake8
 │  │  ├ .git
 │  │  ├ .github
 │  │  ├ .gitignore
 │  │  ├ .pre-commit-config.yaml
 │  │  ├ .python-version
 │  │  ├ Makefile
 │  │  ├ README.md
 │  │  ├ buildspecs
 │  │  ├ cicd
 │  │  ├ cicd.py
 │  │  ├ docker
 │  │  ├ packages
 │  │  ├ plugin_template
 │  │  ├ plugins
 │  │  ├ scripts
 │  │  └ venv


$ t . 2
.
 ├ src
 │  ├ .idea
 │  └ plugins


$ t src/plugins/ | more
 │  ├
 │  ├ .flake8
 │  ├ .git
 │  ├ .github
 │  │  ├ pull_request_template.md
 │  ├ .gitignore
 │  ├ .pre-commit-config.yaml
 │  ├ .python-version
 │  ├ Makefile
 │  ├ README.md
 │  ├ buildspecs
 │  │  ├ test-and-deploy.yml
 │  ├ cicd
:

| more can be put at the end of the function for convenience.

Not the answer you're looking for? Browse other questions tagged or ask your own question.