Using dirname on a file before uniq

Hello,

I have a list of files generated like this:

find dir -type f > file_list

I want to get a list of just the unique directories. I can't create a temporary file. So the idea is to do a working equivalent to this:

cat file_list | dirname | uniq

But of course that doesn't even come close to working. For now, I'm looping thru the file, creating a \n seperated list of names ina variable, and running that thru uniq. Seems like there should be a clever one liner. Ideas?

use pipe for this kind of things...

find dir -type f | dirname |sort | uniq

First that can't work. Dirname doesn't work the way you assume it does.

Second, I said I have a file, I can't change the contents of the file. The file is the file. I was trying to help you generate a file of your own if you wanted to play along at home.

Third, what you were trying to do could be trivially done with the -exec flag in find. If this were easy, I'd have already done it.

find dir -type f|awk '{sub(/\/[^\/]*$/,"")}1'|sort -u
1 Like
find dir -type f | awk -F "/" '{ $NF = ""}1' OFS="/" | sort -u