Make an array of files you find

Submitted by:Tim Kelly

Date added:30 October, 2013

Category:Bash

Example code showing how to make an array of files you find

Tags: array

Code Snippet:

    # 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" {} \;)
 
 

Comments