Pragmatic QA

List Directory Structure in Unix/Linux

| Comments

Today I found a really useful combination of commands to list the directory structure in Unix/Linux operating systems as follows.

lang: bash
1
ls -R | grep “:$” | sed -e ‘s/:$//’ -e ‘s/\[^-\]\[^\/\]*\//–/g’ -e ‘s/^/ /’ -e ‘s/-/|/’

The result of this command is as below. It is listing the directory structure under the functionalTests directory.

lang: bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
functionalTests haroon$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/
   |-src
   |---main
   |-----java
   |-------com
   |---------pragmaticqa
   |-----------tests
   |---test
   |-----java
   |-------com
   |---------pragmaticqa
   |-----------tests
   |-target
   |---classes
   |-----com
   |-------pragmaticqa
   |---------tests
   |---surefire-reports
   |-----Command line suite
   |---test-classes
   |-----com
   |-------pragmaticqa
   |---------tests

You can also use Unix tree command to list the directory structure, as of writing this one I was not aware of the tree command.

Comments