Home > Net >  Exclude specific subfolder from tree
Exclude specific subfolder from tree

Time:08-10

me@s:/tmp$ mkdir -p test/raw test/{a,b,c}/data/x
me@s:/tmp$ tree test/
test/
├── a
│   └── data
│       └── x
├── b
│   └── data
│       └── x
├── c
│   └── data
│       └── x
└── raw

I need to exclude test/raw and test/b/data but tree -I only want folder names:

me@s:/tmp$ tree test -I 'raw|b/data'
test
├── a
│   └── data
│       └── x
├── b
│   └── data
│       └── x
└── c
    └── data
        └── x

Without the b/data that exclude all data folders:

me@s:/tmp$ tree test -I 'raw|data'
test
├── a
├── b
└── c

me@s:/tmp$

CodePudding user response:

As of the current version of tree (v2.0.2), this is not possible.

The pattern is applied to the filename only (the d_name member of the a struct dirent).

See here, the getinfo function gets called from here.

CodePudding user response:

The options -P and -I of the tree command, only work on the file-names and not on the path-names of the file [1].

Having hat said, you could use grep to extract the wanted information:

$ tree -f test | grep -vE '(raw|b/data)'
test
├── test/a
│   └── test/a/data
│       └── test/a/data/x
├── test/c
│   └── test/c/data
│       └── test/c/data/x

Here, we make use of the flag -f which prints the relative path-name with respect to the directory test. The grep command does the rest.

As you notice, the exclusion of raw does not allow for a clean tree-output as you have spurious |'s .


Notes:

  1. in Linux, everything is a file.
  •  Tags:  
  • bash
  • Related