Using find and grep to fill an array

In bash I am trying to create an array of pathnames for zero sized files.
I use

find . -size 0

      

which gives me a list of files and their paths. So the conclusion is:

./001/013/fileA
./001/014/fileB
./002/077/fileA

      

Now I want an array that I can loop through, for example:

(001/013 001/014 002/077)

      

I've tried this:

folderlist=($(find . -size 0 | grep '\<[0-9][0-9][0-9][/][0-9][0-9][0-9]\>' ))
for i in "${folderlist[@]}"
do
    echo $i
done

      

But the exit is empty.

What am I missing?

Thank you so much!

+3


source to share


2 answers


You can use dirname

to reliably get the directory path.



folderlist=($(find . -size 0 -print0|xargs -0 -I% dirname %))
for i in "${folderlist[@]}"
do
    echo $i
done

      

+1


source


To remove only filenames from the output find

, you can use:

folderlist=($(find . -size 0 -exec bash -c 'echo "${1%/*}"' - {} \;))

      



Or to scroll through these entries:

while read -r f; do
   echo "Processing: $f"
done < <(find . -size 0 -exec bash -c 'echo "${1%/*}"' - {} \;)

      

+2


source







All Articles