RandomCoder
RandomCoder

Reputation: 99

AIX: Find Files with name containing specific string and add those to a list

We need to find files in a directory with name containing a specific string and add them in a list.

Suppose we create list containing files names in a specific directory containing string ABC.

Tried this one:

file_list=()
str="ABC"
while IFS= read -d $'\0' -r file ; do
file_list=("${file_list[@]}" "$file")
done < <(find . -name "*$str*" -print0)
echo "Files getting appended: ${file_list[@]}"

If the directory contain files:

ABC.txt, ABCD.txt, XYZ.txt, WXYZ.txt

Then expected output of the above snippet should be:

Files getting appended: ABC.txt ABCD.txt

Getting error message in AIX:

find: 0652-017 -print0 is not a valid option.

Got a related post which works for Linux but got no luck in AIX.

Any help will really be appriciated!

Upvotes: 0

Views: 944

Answers (1)

Lorinczy Zsigmond
Lorinczy Zsigmond

Reputation: 1910

Indeed, AIX!find doesn't support -print0. Try something like this:

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

result:

"./x ABC y"
"./xABCy"

Upvotes: 1

Related Questions