2

I have a folder with lots of images named "clip01234-randomlongstring.png", where 01234 is a random five digit number. I also have an array "clipnumbers" with a list of integers.

Now I want to create a list "files" containing all file names which match the numbers in the "clipnumbers" array. How would I do that?

The resulting output should be something I can process in the same way as my current list (of all files) I create with: files=($(printf "%s\n" *.* | sort -V | tr '\n' ' '))

6

In a loop:

shopt -s nullglob

files=()
for number in "${clipnumbers[@]}"; do
    printf -v pattern 'clip%s-*.png' "$number"
    files+=( $pattern )
done

This loops over the numbers and creates a filename globbing pattern for each. The pattern is expanded to add the filenames matching it to the array files. The nullglob shell option makes non-matching patterns expand to nothing (as opposed to remain unexpanded).


Using find (for recursion into all directories beneath the current directory, and for performing some action on each found file):

patterns=()
for number in "${clipnumbers[@]}"; do
    printf -v pattern 'clip%s-*.png' "$number"
    patterns+=( -o -name "$pattern" )
done

find . -type f \( "${patterns[@]:1}" \) -exec action-to-perform-on-files {} \;

The :1 removes the initial -o from the list in patterns in the expansion.

This combines searching for the files with performing some action on them. It would fail if your clipnumbers array contains many thousands of numbers (the argument list would become too long).

  • oh wow! i did not see the other posts yet but this here certainly works. it's beautiful! thank you so much, all of you!!! – user3647558 May 19 at 1:09
3

Option #1

Similar to Kusalananda's answer but with array expansion instead of a loop:

setup

$ touch clip12710-x.png  clip30443-x.png  clip57592-x.png  clip76672-x.png  clip93493-x.png
$ declare -a array=([0]="30443" [1]="76672" [2]="42424")

Note that the array contains only two items that are expected to match; there are filenames with clips that are not present and there are clip numbers in array that do not exist as filenames.

execution

$ shopt -s nullglob
$ pfiles=( "${array[@]/#/clip}" )
$ oIFS="$IFS"
$ IFS=
$ pfiles=( ${pfiles[@]/%/-*.png} )
$ IFS="$oIFS"
$ declare -p pfiles
declare -a pfiles=([0]="clip30443-x.png" [1]="clip76672-x.png")

Note the careful inclusion of double-quotes in the first assignment and the lack of double-quotes in the second assignment. The initial assignment translates the "array" array of numbers into a "pfiles" array of partial filenames by prepending the string clip to each element. The second assignment appends the -*.png wildcard to each element of the array; the lack of quoting in this assignment allows the shell to split each element on $IFS (normally space, tab, and newline), but we've temporarily overridden IFS to be empty. The shell then also "globs" the results, which is what we want here -- for it to expand the "clip...*-png" names into any matching filenames. With the nullglob shell option set, any non-matching wildcards are dropped. The final result is an array in pfiles of files matching clip numbers from your original array.


Option #2

(ab)use extended globbing:

shopt -s extglob nullglob
declare -a array=([0]="30443" [1]="76672" [2]="42424")
oIFS="$IFS"
IFS='|'
p="${array[*]}"
IFS="$oIFS"
pfiles=( clip@($p)-*.png )

This works by setting IFS to the pipe symbol | so that the subsequent assignment to p of array[*] joins the elements of array by pipes (the first character of $IFS at that point). Pipes are the delimiters that bash's extended globbing syntax requires between options in an extended globbing pattern. The last line expands to an array of files that match the extended glob pattern we've constructed:

  • start with clip
  • contain one of the given patterns (clip numbers), now contained in the variable p
  • followed by - then anything
  • and ending in .png

The nullglob shell option is required in case your clips array does not overlap with any existing filenames.

2

With zsh:

clipnumbers=(01234 33333)
files=(clip$^clipnumbers-*.png(N.))

That expands one glob per clip number. Alternatively, you could turn the array into a glob alternation operator:

files=(clip(${(j:|:)~clipnumbers})-*.png(N.))
1

Using GNU grep and printf:

grep -F $(printf '%s\n' "${clipnumbers[@]}") clip?????-randomlongstring.png

Which can be assigned to an array like so:

files=($(grep -F $(printf '%s\n' "${clipnumbers[@]}") clip?????-randomlongstring.png))
1
mapfile -t files <  <( shopt -s nullglob ; printf "%s\n" $(printf "clip%s-*.png " "${clipnumbers[@]}" ) )
  • mapfile -t files read lines into files as an array, strip trailing line break.
  • shopt -s nullglob expand non existing pattern to a null string
  • printf "%s\n" ... expand patterns, one per line.
  • $(printf "clip%s-*.png " "${arr[@]}") ) build patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.