Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find and grep to fill an array

In bash I am trying to produce an array of path-names to files with zero size.
I am using

find . -size 0

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

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

Now I want an array that I can loop through, like this:

(001/013 001/014 002/077)

I 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 output is empty.

What am I missing?

Thanks a lot!

like image 724
user3060684 Avatar asked Nov 27 '25 05:11

user3060684


1 Answers

To strip just file names from find output you can use:

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

Or to loop through those entries:

while read -r f; do
   echo "Processing: $f"
done < <(find . -size 0 -exec bash -c 'echo "${1%/*}"' - {} \;)
like image 125
anubhava Avatar answered Nov 30 '25 16:11

anubhava