简体   繁体   中英

How to recursively get all files filtered by multiple extensions within a folder including working folder without using find in Bash script

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 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.

PS I know this can be done with but I really want to know if there's another way for the sake of learning. 来完成,但是我真的很想知道是否还有另一种学习方法。

Thanks!

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})"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM