17

Possible Duplicate:
How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

I've read the man page for ls, and I can't find the option to list all that do not match the file selector. Do you know how to perform this operation?

For example: lets say my directory is this:

> ls
a.txt b.mkv c.txt d.mp3 e.flv

Now I would like to do something that does the following

> ls -[SOME_OPTION] *.txt
b.mkv d.mp3 e.flv

Is there such an option?

If not, is there a way to pipe the output of ls to another function (possibly sed) that shows only the ones that I would like?

I don't know exactly how to do this, but I'm imagining it would be something like:

> ls | sed [SOMETHING] 

I really should learn how to use sed,awk,and grep, but I keep getting stuck at understanding how to write the regexes. I understand the concept of regular expressions clearly, but I get confused between regexes that use different syntax.

Any help would be much appreciated!

EDIT:

I forgot to mention that I am running Mac OS X, so the functions may be slightly different from the ones discussed in other answers for the unix/linux shell (hence some of my confusion with sed,awk,and grep).

jww
  • 97,681
  • 90
  • 411
  • 885
Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36

5 Answers5

57

this may be help you

ls --ignore=*.txt

It will not display the .txt files in your directory.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Manikandan Rajendran
  • 1,142
  • 1
  • 8
  • 9
  • 2
    This one worked well for me. You can string --ignore switches together, and thus ignore e.g. all txt files, all files starting with a j and all files with a name containing 'bar': `ls --ignore=*.txt --ignore=j* --ignore=*bar*` – Gert Sønderby May 21 '13 at 12:28
  • 2
    I get illegal option when I use that on El Capitan ls --ignore=*pdf ls: illegal option -- - usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...] – Adil Hindistan Sep 11 '16 at 14:13
  • 2
    Thanks. Actually this should be the accepted answer!! – Veera Jan 17 '17 at 09:54
18

maybe this command will help you

find ./ -maxdepth 1 ! -path "*txt"
Robert Guo
  • 262
  • 1
  • 8
  • This works nicely with many different selectors as the `path` parameter. Thank you! One thing I found was that I could do `find . -maxdepth 1 ! -path "*txt"` too. Now what would I have to use if I had a Regex for filtering the list? – Kaushik Shankar Sep 28 '12 at 05:50
  • if you want use Regex , use grep . ex . you have 3 files like "a b c" , use ls | grep -v "[a|b]" , then it will display c .and then if you have 3 files a aa aaa , use "ls | grep -v "a+" , then it will only display "a". – Robert Guo Sep 28 '12 at 06:30
  • ./ Argument is optional since find command already start searching down from current Directory tree – kante May 24 '13 at 09:44
  • Mani R's asnwer is the valid answer for the original question. – karatedog Oct 17 '19 at 20:10
15
ls|grep -v ".txt"

does this helps?

nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • This works for the case I gave you, but really I'm looking for something that works with any selectors. For example, If I wanted to not show the ones that had the `x` character followed by the word `in`, I need to use the `*x*in*` selector. This wouldn't be possible using your example I think. – Kaushik Shankar Sep 28 '12 at 05:47
  • @KaushikShankar I think it will work. Give it a try. – nav_jan Sep 28 '12 at 06:28
7

ls just lists what arguments it is presented with. *.txt gets expanded to a.txt c.txt before ls sees it, try echo *.txt.

To do what you ask with sed you can delete the pattern from its input, for example:

ls | sed '/\.txt$/d'

Would delete all lines ending with .txt.

With bash and zsh you can have the shell do the inverted expansion, with bash it would be:

ls !(*.txt)

zsh:

ls *~*.txt

Note that both shells need the extended glob option to be enabled, shopt -s extglob with bash and setopt extendedglob with zsh.

Thor
  • 45,082
  • 11
  • 119
  • 130
4

One way using find:

find . -maxdepth 1 -type f -not -name "*.txt" -printf "%f\n"
Steve
  • 51,466
  • 13
  • 89
  • 103
  • Thank you for your response! Unfortunately, I think there may be an issue with running on Mac OS X. The error I get is the following: Command: `>find . -maxdepth 1 -type f -not -name "*.txt" -printf "%f\n"` Error: `39m\n"find: -printf: unknown primary or operator` – Kaushik Shankar Sep 28 '12 at 05:42
  • @KaushikShankar: Yes ignore the printf statement, it will only neaten your ouput to provide a list of files similar to how `ls` would work. It seems `BSD find` doesn't understand it. – Steve Sep 28 '12 at 06:13