Andy Ray
Andy Ray

Reputation: 32066

bash globbing (globstar) pattern errors if top level directory empty with "No such file or directory"

I have a folder structure as such:

a/
  b/
    test.js
  c/
    another_test.js

When I want to find all these files, I tried the globstar approach:

ls a/{,**/}*.js

However, this command errors (but still outputs files) because there is no a/*.jsx file:

$ ls a/{,**/}*.jsx
    ls: a/*.jsx: No such file or directory
    a/b/test.js

I want to use this specific glob because in the future, at some point, a/test.js could exist.

Is there a glob pattern that will find all .js files in a/ recursively and not error?

I looked at some of the options in this question but couldn't find anything that doesn't error and lists all files.

Upvotes: 0

Views: 968

Answers (2)

dawg
dawg

Reputation: 103844

Given:

% tree .
.
└── a
    ├── b
    │   └── test.js
    └── c
        └── another_test.js

You can use the pattern a/**/*.js on either zsh or Bash4+ with shopt -s globstar set:

zsh:

% ls -1 a/**/*.js
a/b/test.js
a/c/another_test.js

Bash 5.1:

$ ls -1 a/**/*.js
a/b/test.js
a/c/another_test.js

Upvotes: 0

izabera
izabera

Reputation: 669

With bash4 and above, just use:
ls dir/**/*.js

With previous bash versions, such as 3.2 shipped with osx, you can use find:
find dir -name '*.js'

Upvotes: 2

Related Questions