13

Does ls have a way to show negated conditions like "all files which are not a symlink"? I use the latter a lot in a project directory but other negations would be useful as well.

For now, my research has only lead to creating an alias to something "like":

find . -maxdepth 1 ! -type l | sort # (...)

but obviously this way I don't get the colouring of ls, the column formatting, etc...

I am on Bash v3 on OS X 10.8.2 and Bash v4 on Pangolin sometimes.

1 Answers1

15

Instead of piping it to sort, use ls.

find . -maxdepth 1 \! -type l -exec ls -d {} +

find . -maxdepth 1 \! -type l | xargs ls -d

If you used the zsh shell you could use their non-portable glob extensions:

ls -d *(^@)
Random832
  • 10,666
  • Thx 4 the answer! Why do you escape !? It seems to be working even without.. What I am risking by not escaping it? Interesting how you use a final + and not \;, please explain? – Robottinosino Oct 05 '12 at 04:25
  • 3
    @Robottinosino The + makes it send all to a single ls command instead of running it separately for each file (this way ls can do the columns and sorting). I escaped the ! because it is a special character to some shells even though yours seems to accept it fine. – Random832 Oct 05 '12 at 04:27
  • Gotcha. Great stuff! Do you agree that it's a bit laborious though, just to negate a predicate in a simple ls? Could there be a better way? – Robottinosino Oct 05 '12 at 04:30
  • 2
    @Robottinosino Edited with something zsh can do. I don't think bash can do it though. – Random832 Oct 05 '12 at 04:34