AIX:查找名称包含特定字符串的文件并将其添加到列表中
AIX: Find Files with name containing specific string and add those to a list
我们需要在目录中找到名称包含特定字符串的文件,并将它们添加到列表中。
假设我们在包含字符串 ABC
.
的特定目录中创建包含文件名的列表
试过这个:
file_list=()
str="ABC"
while IFS= read -d $'[=10=]' -r file ; do
file_list=("${file_list[@]}" "$file")
done < <(find . -name "*$str*" -print0)
echo "Files getting appended: ${file_list[@]}"
如果目录包含文件:
ABC.txt, ABCD.txt, XYZ.txt, WXYZ.txt
那么上述片段的预期输出应该是:
Files getting appended: ABC.txt ABCD.txt
在 AIX 中获取错误消息:
find: 0652-017 -print0 is not a valid option.
得到了一个相关的 post,它适用于 Linux,但在 AIX 中没有成功。
我们将不胜感激任何帮助!
确实,AIX!find 不支持 -print0
。尝试这样的事情:
#!/usr/local/bin/bash
file_list=()
touch xABCy
touch 'x ABC y'
str="ABC"
while IFS='\n' read -r file ; do
file_list+=("$file")
done < <(find . -name "*$str*")
for i in "${file_list[@]}"; do
printf '\"%s\"\n' "$i"
done
结果:
"./x ABC y"
"./xABCy"
我们需要在目录中找到名称包含特定字符串的文件,并将它们添加到列表中。
假设我们在包含字符串 ABC
.
试过这个:
file_list=()
str="ABC"
while IFS= read -d $'[=10=]' -r file ; do
file_list=("${file_list[@]}" "$file")
done < <(find . -name "*$str*" -print0)
echo "Files getting appended: ${file_list[@]}"
如果目录包含文件:
ABC.txt, ABCD.txt, XYZ.txt, WXYZ.txt
那么上述片段的预期输出应该是:
Files getting appended: ABC.txt ABCD.txt
在 AIX 中获取错误消息:
find: 0652-017 -print0 is not a valid option.
得到了一个相关的 post,它适用于 Linux,但在 AIX 中没有成功。
我们将不胜感激任何帮助!
确实,AIX!find 不支持 -print0
。尝试这样的事情:
#!/usr/local/bin/bash
file_list=()
touch xABCy
touch 'x ABC y'
str="ABC"
while IFS='\n' read -r file ; do
file_list+=("$file")
done < <(find . -name "*$str*")
for i in "${file_list[@]}"; do
printf '\"%s\"\n' "$i"
done
结果:
"./x ABC y"
"./xABCy"