1

I want to apply a function, of Python, to all files in sub-directories of a given directory.

In bash, .sh file, I list all files like so:

dir_list=()
while IFS= read -d $'\0' -r file ; do
dir_list=("${dir_list[@]}" "$file")
done < <(find give_directory_Here -mindepth 2 -maxdepth 2 -type d -print0)

However, there are some directories with a pattern, lets say the pattern is unwanted_pattern, in their name that I want to remove from dir_list.

How can I do that?

I tried things in here which did not work for me: solution 1 from stack over flow or solution 2 from stack exchange, so on!

OverFlow Police
  • 861
  • 6
  • 23
  • The first line mentions Python, but I see nothing else about Python here, and the meaning isn't clear. Would it be as accurate if you just edited that out? – Paul Hodges Apr 10 '19 at 14:21

1 Answers1

1

Delete some entries of a list or array in bash

Just loop and match:

   result=()
   for file in "${dir_list[@]}"
   do
     if [[ "$file" != *"unwanted_pattern"* ]]
     then
       result+=("$file")
     fi
   done
   dir_list=( "${result[@]}" )

However, this is the answer to your XY question and not the thing you should be doing.

The smarter way would be to not add them in the first place by adding such a check to your loop, and the even smarter way would be to just have find exclude them:

find give_directory_Here -mindepth 2 -maxdepth 2 -type d ! -path '*unwanted_pattern*' -print0
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Thanks a lot, about the XY thing, if I had not done this way, people would have said "what did you try?", "it is not reproducible", etc. :) And, on top of that, now I learned two things with 1 question :D – OverFlow Police Apr 10 '19 at 14:14