2

when I type ls, I don't want to see .o files because everytime I ls, It see X.cpp X.h X.o, how do I not list .o files?

3 Answers 3

3

OS X's BSD ls does not support the -I argument present in GNU ls to filter file types.

You can run the following expression that returns all files in the current directory that don't have a .o extension, and otherwise prints the same format as ls -l:

find * -maxdepth 0 ! -name '*.o' -print0 | xargs -0 ls -dl

You can create a function for this in your ~/.zprofile so you can run it by typing lso <extension>:

function lso {
    find * -maxdepth 0 ! -name "*.$1" -print0 | xargs -0 ls -dl
}

It's not suitable as a complete ls substitute, as it doesn't support arguments.

Sample output:

$ ls -l
total 8
-rw-r--r--  1 danielbeck  staff    0  2 Sep 12:52 pi.c
-rw-r--r--  1 danielbeck  staff    0  2 Sep 12:52 pi.o
-rw-r--r--  1 danielbeck  staff  278 31 Aug 06:52 pi.py
-rw-r--r--  1 danielbeck  staff    0  2 Sep 12:52 pi.pyc

$ lso
-rw-r--r--  1 danielbeck  staff    0  2 Sep 12:52 pi.c
-rw-r--r--  1 danielbeck  staff  278 31 Aug 06:52 pi.py
-rw-r--r--  1 danielbeck  staff    0  2 Sep 12:52 pi.pyc
4
  • maybe you could try piping it to grep -v? ls | grep -v ".o" Sep 2, 2012 at 8:23
  • Can you please try to parametrize this using bash parameters? IBM tutorial to do this seems very complicated. Sorry. Sep 14, 2012 at 14:44
  • @snihalani, are we talking about bash or zsh? Not much difference though...
    – cYrus
    Sep 14, 2012 at 15:53
  • @snihalani, I don't know how does the notifications system work, but... I've edited the function to take a parameter (e.g. lso o).
    – cYrus
    Sep 14, 2012 at 17:20
0

If you've setopt extended_glob, you can do something like ls -ld ^*.o and maybe assign that to an alias of your choice.

0

@cYrus is correct that the BSD ls -I ... filter option is not supported. However here's a quick-and-dirty hack that postprocesses ls -F output to exclude directories, symbolic links, sockets, whiteouts, FIFOs:

ls -1F | grep -v '[/@=%|]'

(Note: obviously this relies on assuming those special characters don't occur in filenames, which is not 100% reliable like a find-based approach.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .