22

Let say we have

Scripts
Test 
Test1 

When we do ls it shows all the three directories but I need to exclude the test1. So I should get only

Scripts
Test 

What command should I use here to exclude a directory?

Thanks in advance

viraptor
  • 33,322
  • 10
  • 107
  • 191
Madhumitha
  • 297
  • 1
  • 2
  • 6

3 Answers3

38

ls -ITest1

from man ls:

  -I, --ignore=PATTERN
          do not list implied entries matching shell PATTERN

You can use it multiple times like:

ls -ITest1 -ITest2

or you could use a matching pattern:

ls -ITest?

Shawn Balestracci
  • 7,380
  • 1
  • 34
  • 52
  • How about excluding multiple directories tried ls -I dir1 di2 didn't work ? – Madhumitha Aug 14 '15 at 00:11
  • 2
    you can also use a list such as {item1,item2,item3}. That gives you ls -I{a,b,c,d,e,f} – Tarek Feb 02 '18 at 18:00
  • 1
    What if you want to ignore all sub directories and files, but not a directory itself? For example showing the `build` directory or the `.git` directory, but ignoring its contents (when used with `-R`) – Brandon Nov 13 '21 at 23:22
  • 1
    @Tarek didn't say this, but this works when your list contains **two** or more items, separated by a comma. – Jason Hemann Jan 06 '22 at 17:54
  • 1
    Needs a space delimiter between `-I Test1`. – CodeFinity Apr 18 '22 at 17:52
  • That's funny cause i'm looking at `man` for `ls` on macOS and it describes the parameter `-I` as: `-I Prevent -A from being automatically set for the super-user. This option is not defined in IEEE Std 1003.1-2008 (“POSIX.1”).` – Sebastian Nov 04 '22 at 11:35
7

You can do this via grep -v. Command will be ls /mydir/ | grep -v 'test1'

The -v means exclude.

Good references here: http://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/

Burning Hippo
  • 787
  • 1
  • 20
  • 47
4

Try these:

ls -la | egrep -v ^d

-or-

ls -p | egrep -v /$

-or-

find . -type f -maxdepth 1
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103