1

I have this question after quite a day of searching the net, perhaps I'm doing something wrong , here is my script:

#!/bin/bash

shopt -s extglob

FILE_EXTENSIONS=properties\|xml\|sh\|sql\|ksh
SOURCE_FOLDER=$1

if [ -z "$SOURCE_FOLDER" ]; then
   SOURCE_FOLDER=$(pwd)
fi # Set directory to current working folder if no input parameter.

for file in $SOURCE_FOLDER/**/*.*($FILE_EXTENSIONS)
do
    echo Working with file: $file
done

Basically, I want to recursively get all the files filtered by a list of extensions within folders from a directory that is passed as an argument including the directory itself.

I would like to know if there is a way of doing this and how without the use of the find command.

Imagine I have this file tree:

  • bin/props.properties
  • bin/xmls.xml
  • bin/source/sources.sh
  • bin/config/props.properties
  • bin/config/folders/moreProps.xml

My script, as it is right now and running from /bin, would echo:

  • bin/source/sources.sh
  • bin/config/props.properties
  • bin/config/folders/moreProps.xml

Leaving the ones in the working path aside.

P.S. I know this can be done with find but I really want to know if there's another way for the sake of learning.

Thanks!

  • 1
    Good first question, but [it has been asked before](http://stackoverflow.com/questions/1690809/what-expands-to-all-files-in-current-directory-recursively). So in your case, `$SOURCE_FOLDER/{,**/}*.*($FILE_EXTENSIONS)` should work – Reinstate Monica Please Jul 11 '14 at 23:54
  • 1
    Or just split it up into two globs `$SOURCE_FOLDER/**/*.*($FILE_EXTENSIONS) $SOURCE_FOLDER/*.*($FILE_EXTENSIONS)`. Though I think the first brace expansion syntax is cleaner – Reinstate Monica Please Jul 12 '14 at 00:00
  • Great! Thank you very much! This was confusing to me, you can tell by the way my question and the duplicate are made jeje. Thanks again! – Abraham Pérez Jul 14 '14 at 18:35

1 Answers1

1

You can use find with grep, just like this:

#!/bin/bash
SOURCE_FOLDER=$1
EXTENSIONS="properties|xml|sh|sql|ksh"

find $SOURCE_FOLDER | grep -E ".(${EXTENSIONS})"

#or even better
find $SOURCE_FOLDER -regextype posix-egrep -regex ".*(${EXTENSIONS})"