Return to Snippet

Revision: 22658
at May 9, 2010 14:55 by tm


Updated Code
# Requires GNU find (for -print0). Since the find example starts in ".", I replace
# the leading "./" of all results with the working directory PWD to get an absolute
# path for each result. Alternatively use 'find "$PWD"' instead of 'find .'
unset a
declare -a a
while read -d '' -r; do
    a+=("$PWD/${REPLY#./}")
done < <(find . -name \*.txt -print0)
# Verify
printf ">%s<\n"  "${a[@]}"

# Same, on one line, without the, using "$PWD" and without GNU find:
unset a; declare -a a; while read -d '' -r; do a+=("$REPLY"); done < <(find "$PWD" -name \*.txt -exec printf "%s\0" {} \;)

Revision: 22657
at February 28, 2010 21:29 by tm


Updated Code
# Requires GNU find (for -print0). Since the find example starts in ".", I replace
# the leading "./" of all results with the working directory PWD to get an absolute
# path for each result
unset a
declare -a a
while read -d '' -r; do
    a+=("$PWD/${REPLY#./}")
done < <(find . -name \*.txt -print0)
# Verify
printf ">%s<\n"  "${a[@]}"

# Same, on one line, without the "./"-replacement and without GNU find:
unset a; declare -a a; while read -d '' -r; do a+=("$REPLY"); done < <(find . -name \*.txt -exec printf "%s\0" {} \;)

Revision: 22656
at February 28, 2010 21:22 by tm


Updated Code
# Requires GNU find (for -print0). Since the find example starts in ".", I replace
# the leading "./" of all results with the working directory PWD to get an absolute
# path for each result
unset a
declare -a a
while IFS= read -d '' -r; do
    a+=("$PWD/${REPLY#./}")
done < <(find . -name \*.txt -print0)
# Verify
printf ">%s<\n"  "${a[@]}"

Revision: 22655
at January 18, 2010 20:13 by tm


Initial Code
# Requires GNU find (for -print0)
unset a
declare -a a
while IFS= read -d '' -r; do
    r=${REPLY#./}
    a+=($PWD/$r)
done < <(find . -name \*.txt -print0)
# Verify
printf ">%s<\n"  "${a[@]}"

Initial URL


Initial Description


Initial Title
Make an array of files you find

Initial Tags
Bash, array, find

Initial Language
Bash