在 bash 中删除列表或数组的一些条目
Delete some entries of a list or array in bash
我想将 Python 的函数应用于给定目录的子目录中的所有文件。
在 bash、.sh
文件中,我列出了所有文件,如下所示:
dir_list=()
while IFS= read -d $'[=11=]' -r file ; do
dir_list=("${dir_list[@]}" "$file")
done < <(find give_directory_Here -mindepth 2 -maxdepth 2 -type d -print0)
但是,有些目录有一个模式,假设模式是 unwanted_pattern
,我想从 dir_list
中删除它们的名称。
我该怎么做?
我在这里尝试了一些对我不起作用的东西:
solution 1 from stack over flow
要么
solution 2 from stack exchange,等等!
Delete some entries of a list or array in bash
只需循环并匹配:
result=()
for file in "${dir_list[@]}"
do
if [[ "$file" != *"unwanted_pattern"* ]]
then
result+=("$file")
fi
done
dir_list=( "${result[@]}" )
但是,这是对您 XY question 的回答,而不是您应该做的事情。
更聪明的方法是通过在循环中添加这样的检查来首先不添加它们,更聪明的方法是 find
排除它们:
find give_directory_Here -mindepth 2 -maxdepth 2 -type d ! -path '*unwanted_pattern*' -print0
我想将 Python 的函数应用于给定目录的子目录中的所有文件。
在 bash、.sh
文件中,我列出了所有文件,如下所示:
dir_list=()
while IFS= read -d $'[=11=]' -r file ; do
dir_list=("${dir_list[@]}" "$file")
done < <(find give_directory_Here -mindepth 2 -maxdepth 2 -type d -print0)
但是,有些目录有一个模式,假设模式是 unwanted_pattern
,我想从 dir_list
中删除它们的名称。
我该怎么做?
我在这里尝试了一些对我不起作用的东西: solution 1 from stack over flow 要么 solution 2 from stack exchange,等等!
Delete some entries of a list or array in bash
只需循环并匹配:
result=()
for file in "${dir_list[@]}"
do
if [[ "$file" != *"unwanted_pattern"* ]]
then
result+=("$file")
fi
done
dir_list=( "${result[@]}" )
但是,这是对您 XY question 的回答,而不是您应该做的事情。
更聪明的方法是通过在循环中添加这样的检查来首先不添加它们,更聪明的方法是 find
排除它们:
find give_directory_Here -mindepth 2 -maxdepth 2 -type d ! -path '*unwanted_pattern*' -print0