Reading filenames into an array Reading filenames into an array unix unix

Reading filenames into an array


Don't use ls, it's not intended for this purpose. Use globbing.

shopt -s nullglobarray=(*)array2=(file*)array3=(dir/*)

The nullglob option causes the array to be empty if there are no matches.


Following will create an array arr with ls output in current directory:

arr=( $(ls) )

Though using output of ls is not safe at all.

Much better and safer than ls you can use echo *:

arr=( * )echo ${#arr[@]} # will echo number of elements in arrayecho "${arr[@]}" # will dump all elements of the array


In bash you can create an array of filenames with pathname expansion (globbing) like so:

#!/bin/bashSOURCE_DIR=path/to/sourcefiles=(   "$SOURCE_DIR"/*.tar.gz   "$SOURCE_DIR"/*.tgz   "$SOURCE_DIR"/**/*)

The above will create an array called files and add to it N array elements, where each element in the array corresponds to an item in SOURCE_DIR ending in .tar.gz or .tgz, or any item in a subdirectory thereof with subdirectory recursion possible as Dennis points out in the comments.

You can then use printf to see the contents of the array including paths:

printf '%s\n' "${files[@]}" # i.e. path/to/source/filename.tar.gz

Or using parameter substitution to exclude the pathnames:

printf '%s\n' "${files[@]##*/}" # i.e. filename.tgz