12

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.

14

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 *(^@)
  • 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 5 '12 at 4:25
  • 2
    @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 5 '12 at 4: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 5 '12 at 4:30
  • 1
    @Robottinosino Edited with something zsh can do. I don't think bash can do it though. – Random832 Oct 5 '12 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.