53

Could you please give me an example of using the tree command to filter out result as follows:

  • to ignore directories (say bin, unitTest)
  • only listing certain files having extensions (say .cpp, .c, .hpp, .h)
  • providing full path-names of only the resultant files matching the criteria.
mtk
  • 26,802
  • 35
  • 91
  • 130
Linda
  • 531
  • 1
  • 4
  • 4

4 Answers4

82

One way is to use patterns with the -I and -P switches:

tree -f -I "bin|unitTest" -P "*.[ch]|*.[ch]pp." your_dir/

The -f prints the full path for each file, and -I excludes the files in the pattern here separated by a vertical bar. The -P switch inlcudes only the files listed in the pattern matching a certain extension.

4

The true solution is to output full paths, filter unwanted paths out, and finally fix the outputs.

tree -df | egrep -v "\./(bin|unitTest)/.*" | sed -r 's%\./.*/%%g'

If all files are necessary in the output, remove the "d" parameter.

Detailed explanation can be found at: http://qaon.net/press/archives/572 if you can understand Japanese.

Afante
  • 41
  • 1
4

use find and tree command that is use find's prune to exclude directories of search and use tree -P for searching the pattern.

Use the prune switch, for example if you want to exclude the misc directory just add a -path ./misc -prune -oto your find command.

for eg.find . -path ./misc -prune -o -exec tree -P <pattern> {} \; or you can use -name "*.cpp" in find

for excluding multiple directories use

find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o

harish.venkat
  • 7,313
  • 2
  • 25
  • 30
  • For me, example with find gives error "find: expected an expression after '-o' " – WebComer Nov 18 '18 at 15:13
  • did you try just `find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o` or `find . -path ./misc -prune -o -exec tree -P *.cpp {} \;` – harish.venkat Nov 28 '18 at 21:32
0

Use awk:

tree -f | awk ‘!/bin|unitTest/ && /\.cpp|\.c|\.hpp|\.h/ {print}’

The first pattern is your excludes and the second is your includes.

mcg256
  • 1