Suppose I have the following list of files:
/aaa/bbb/file1.txt
/aaa/ccc/file2.txt
/aaa/bbb/file3.txt
/aaa/bbb/file4.txt
/aaa/ccc/file5.txt
And I'd like to have a set of all of the unique dirnames in an array. The resulting array would look something like this:
dirs=( "/aaa/bbb" "/aaa/ccc" )
I think I can do something like this, but it feels really verbose (pardon the syntax errors, i don't have a shell handy):
dirs=()
for f in filelist do
dir=$(dirname $f)
i=0
while [$i -lt ${#dirs[@]} ]; do
if [ dirs[$i] == $dir ]
break
fi
i=$[i + 1]
done
if [ $i -eq ${dirs[@]} ]
dirs+=($dir)
fi
done