Search code examples
shellglob

Is it possible to craft a glob that matches files in the current directory and all subdirectories?


For this directory structure:

.
|-- README.txt
|-- firstlevel.rb
`-- lib
    |-- models
    |   |-- foo
    |   |   `-- fourthlevel.rb
    |   `-- thirdlevel.rb
    `-- secondlevel.rb

3 directories, 5 files

The glob would match:

firstlevel.rb 
lib/secondlevel.rb 
lib/models/thirdlevel.rb
lib/models/foo/fourthlevel.rb

Solution

  • Apologies if I've missed the real point of the question but, if I was using sh/bash/etc., then I would probably use find to do the job:

    find . -name '*.rb' -type f
    

    Globs can get a bit nasty when used from within a script and find is much more flexible.