Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.
share|improve this question

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.

share|improve this answer
Note: do not include trailing slashes in your -I flag, eg -I bin, not -I bin/. – ThorSummoner Mar 11 '15 at 18:13

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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