问题
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
).
回答1:
maybe this command will help you
find ./ -maxdepth 1 ! -path "*txt"
回答2:
this may be help you
ls --ignore=*.txt
It will not display the .txt files in your directory.
回答3:
ls|grep -v ".txt"
does this helps?
回答4:
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.
回答5:
One way using find
:
find . -maxdepth 1 -type f -not -name "*.txt" -printf "%f\n"
来源:https://stackoverflow.com/questions/12634013/list-all-files-that-do-not-match-pattern-using-ls