0

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.

Community
  • 1
  • 1
Andy Ray
  • 30,372
  • 14
  • 101
  • 138

2 Answers2

2

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'

izabera
  • 659
  • 5
  • 11
0

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
dawg
  • 98,345
  • 23
  • 131
  • 206