0

Case: List all unique filenames in a directory.

$ ls 
a.h  a.i  b.h  b.i  c.h  d.i

So c and d are unique in this case. For fixed width we can do:

$ ls | uniq -w1 -u
c.h
d.i

Not very helpful though as filenames won't be fixed width and uniq can only skip fields (i.e. ignore fields from left to right not right to left).

ls | sort -u -t. -k1,1
a.h
b.h
c.h
d.i

I thought sort -u was equivalent to uniq -u (ref) and with sort being able to sort and a given field I expected the output to be c.h and d.i but it's not.

Any thoughts?

Note: I don't need help solving the problem, for instance one solution is just reverse the data twice:

$ ls | rev | uniq -u -s1 | rev
c.h
d.i
Community
  • 1
  • 1
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202

1 Answers1

2

sort -u is equivalent to sort | uniq, i.e. no flags for uniq given.

choroba
  • 231,213
  • 25
  • 204
  • 289