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