Reputation: 91
can anyone help me with a regex that will find files that dont have a .tar.gz extension
i need it to work in my unix script
for i in <REGEX for non tar.gx files>
do
something
done
thanks
Upvotes: 1
Views: 232
Reputation: 40759
In bash, with extglob turned on (which appears to be the default) you can negate a glob by using !(<glob>)
. So:
for i in !(*.tar.gz)
do
something
done
If you wanted to match two globs, you'd write for i in *.tar.gz *~
. Similarly, you can write for i in !(*.tar.gz|*~)
if you want to match all files that are neither gzipped archives nor backup files.
Upvotes: 3
Reputation: 343201
you can use find
as well...
find . -type f -not -iname "*.tar.gz"
Upvotes: 1
Reputation: 12514
Another possibility (which illustrates an often-overlooked technique) is simply:
ls | grep -v 'pdf$'|while read i; do echo "i=$i"; done
This is hammer-to-crack-a-nut territory for this particular problem, but if you need to do something with a set of files with slightly complicated selection criteria, this can pay off quite quickly.
And it'd work in any sh-like shell.
Upvotes: 1
Reputation: 49850
If you don't want to use extended globbing, you can also include a simple test:
for file in *
do
if [[ $file != *.tar.gz ]]
then
something
fi
done
Upvotes: 1
Reputation: 14396
Never, never, never use regexes for the task "match every X that doesn't match Y". Instead, use the trivial regex for X and negate the result.
(In shell expressions, replace ==
with !=
, or use grep -v
. In Perl, use !~
instead of =~
. In JQuery, use the :not
operator, etc. If your API doesn't allow you to do anything else but specify one single regex, beat the vendor over the head with Chomsky's A hierarchy of formal languages.)
Complemented regexes are always either inefficient, unreadable or engine-specific - usually all of the above. Just say no to abuse of regexes, use tools for the tasks they're good at!
Upvotes: 3