2

I'm trying to use the tree command to list my current git directory.

I want to exclude every files that match a gitignore pattern. The solution found here seems promising.

Unfortunately it seems that the tree command does not ignore full path pattern properly.

For example, here is a simple directory structure:

.
|-A
|-|-a.jpg
|-b.jpg

Here, I can exclude A and all of its file like that:

tree -I 'A'

I can also exclude a.jpg or b.jpg like that:

tree -I 'a.*|b.*'

But I can't exclude a.jpg like that:

tree -I 'A/a.jpg'

So, full path pattern matching seems broken.

Am I missing something or is this really impossible to have a fine subdirectory pattern matching with tree ?

Thanks !

ogr
  • 261
  • 2
  • 7

2 Answers2

2

I had the same problem. Indeed, I too noticed that $ tree -I 'A/a.jpg' is not working

But where there's a will there's a way :

$ tree
.
├── A
│   ├── a.jpg
│   └── b.jpg
└── a.jpg

$ tree --fromfile <<EOF
`tree -f -i -n -F --noreport | grep -v 'A/a.jpg' | grep -v -E '\.$' | sed "s/^\.//"`
EOF
.
├── A
│   └── b.jpg
└── a.jpg

PS : --fromfile option only available with tree 1.8 and later

ChennyStar
  • 1,319
  • 9
  • 22
0

Will this work?

[user2@server-01 ~]$ tree myDir/
myDir/
├── A
│   └── a.jpg
├── B
│   └── a.jpg
└── b.jpg

2 directories, 3 files

If you want to get a list of all the files in myDir, files in all the subdirectories have the same name; The quickest way is to grep -v?

Either with the find command, pipe and grep -v for the full path of the files that you'd like to exclude.

[user2@server-01 ~]$ find myDir/ | grep -v "myDir/A/a.jpg"
myDir/
myDir/A
myDir/b.jpg
myDir/B
myDir/B/a.jpg

or with your tree method. But add the -f flag to output full directory listing.

[user2@server-01 ~]$ tree -f myDir/ | grep -v "myDir/A/a.jpg"
myDir
├── myDir/A
├── myDir/B
│   └── myDir/B/a.jpg
└── myDir/b.jpg

grep -E can accept multiple parameters, split with the pipe character. And can be used in conjunction with the -v flag.

[user2@server-01 ~]$ tree -f myDir/ | grep -vE "myDir/A/a.jpg | myDir/B/a.jpg"
myDir
├── myDir/A
├── myDir/B
└── myDir/b.jpg

2 directories, 3 files
relevantRope
  • 126
  • 3