Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

osx: tree -I command error opening dir

Tags:

shell

macos

tree

I just installed tree with brew, and when I try to run it like this (on a python project folder):

tree -I *.pyc

It returns [error opening dir] for the pyc files, for example:

I get this running ls:

a.pyc b.pyc

and when I run tree -I *.pyc, I get:

b.pyc [error opening dir]

0 directories, 0 files

Any ideas why?

like image 989
eLRuLL Avatar asked Oct 25 '25 11:10

eLRuLL


2 Answers

This is not a problem with tree, it is the shell (bash) doing it's filename expansions - also known as globbing. So what is passed into tree is a list of all the file names in the current directory ending in .pyc. Only the first filename will follow the -I option (a.pyc), the others will be seen as directory names.

tree wants to do it's own globbing (find is the same) so you must protect it from the shell by adding quotes:

tree -I '*.pyc'

The quotes will be stripped off by the shell and will not be seen by tree, it will just see *.pyc.

In this case either single or double quotes would do the trick, but probably safer to stick with single quotes since other expansions are done inside double quotes.

BTW, when you get this kind of issue use:

set -x

that will show the command after bash expansions. Use set +x to switch it off. These can be used on the command-line or in a script for debugging.

like image 199
cdarke Avatar answered Oct 27 '25 01:10

cdarke


Surround the wildcard patterns passed to -I and -P with quotes to avoid globbing:

tree -I '*.pyc'

From man tree:

Valid wildcard operators are
'*' (any zero or more characters)
'?' (any single character)
'[...]' (any single character listed between brackets)

like image 25
Eugeniu Rosca Avatar answered Oct 26 '25 23:10

Eugeniu Rosca