5

I have a base directory with a set of subdirs inside, some of those dirs have files with extension of jpg. I want to copy those files to another dir, this is my objective. I decided to assign the value of find to a list variable and then iterate over the list and copy the source file to target destination.

mylist=$(find . -type f -name "*.png")

As you can tell the mylist var will have only one element. It will be string containing all the different files found, and there are a lot of files inside the index 0.

I have tried to use the following method to separate the lines on by one onto a new array

while IFS= read -r line; do
    files+=( "$line" )
done <"$mylist"

However this did not work and just dumps all the lines from the result of the find.

I am using bash 3.2 in MacOS.I would like to know how can I parse the $mylist var so that I could have another variable with a file path in each index. Or as an alternative to make the result of the find command into a list right after the execution

Enlico
  • 23,259
  • 6
  • 48
  • 102
Joao s
  • 150
  • 1
  • 8

1 Answers1

6

If the filenames are guaranteed to contain no white spaces, the following should suffice to store all the names in an array variable

mylist=($(find . -type f -name "*.png"))

Then you can access individual filenames by ${mylist[0]}, ${mylist[1]}, ${mylist[2]}, and so on.

Enlico
  • 23,259
  • 6
  • 48
  • 102