Discussion:
Match optional character in filename expansion with find
gvim
2012-02-16 14:57:21 UTC
Permalink
In the `find' command how do I match an optional end character, eg. match all files ending in 'html' or 'htm'?

find . -name '*.ht{m,ml}' -print

... doesn't work. Perl regexes have ? so what's the equivalent using `find'.

gvim
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Robert McKay
2012-02-16 15:03:10 UTC
Permalink
Post by gvim
In the `find' command how do I match an optional end character, eg. match
all files ending in 'html' or 'htm'?
find . -name '*.ht{m,ml}' -print
... doesn't work. Perl regexes have ? so what's the equivalent using `find'.
I'm not sure if there's a better answer but find does support using
multiple -name parameters that can be -and'd or -or'd together. So you
could simply do;

find . -name '*.htm' -o -name '*.html' -print

Rob
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Sunny Aujla
2012-02-16 15:08:21 UTC
Permalink
Post by gvim
In the `find' command how do I match an optional end character, eg. match
all files ending in 'html' or 'htm'?
find . -name '*.ht{m,ml}' -print
This is how I would do it:

find /path/to/dir -type f \(-name "*.html" -o -name "*.htm" \)

Quick and Easy!

Sunny
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
John Edwards
2012-02-16 15:10:53 UTC
Permalink
Post by gvim
In the `find' command how do I match an optional end character, eg. match all files ending in 'html' or 'htm'?
find . -name '*.ht{m,ml}' -print
... doesn't work. Perl regexes have ? so what's the equivalent using `find'.
GNU find has a '-regex' option, but the man page says it only
supports Emacs regular expressions and not Perl compatible ones.
--
#---------------------------------------------------------#
| John Edwards Email: ***@cornerstonelinux.co.uk |
#---------------------------------------------------------#
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Stuart Sears
2012-02-16 16:06:22 UTC
Permalink
Post by John Edwards
Post by gvim
In the `find' command how do I match an optional end character, eg.
match all files ending in 'html' or 'htm'?
find . -name '*.ht{m,ml}' -print
... doesn't work. Perl regexes have ? so what's the equivalent using `find'.
GNU find has a '-regex' option, but the man page says it only
supports Emacs regular expressions and not Perl compatible ones.
You can specify the regex type you'd like to use - there are a number of
choices (at least on my version of findutils)

man find:
-regextype type
Changes the regular expression syntax understood by -regex and
-iregex tests which occur later on the command line. Currently-
implemented types are emacs (this is the default), posix-awk,
posix-basic, posix-egrep and posix-extended.


so something like this:

find . -type f -regextype posix-extended -iregex '.*\.html?'

but in this case even the standard 'emacs' regex class works the way you
want

find . -type f -regex '.*\.html?'

should probably do what you're after

n.b. these examples all from
find (GNU findutils) 4.5.10

Stuart
--
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Nix
2012-02-28 22:07:17 UTC
Permalink
Post by Stuart Sears
You can specify the regex type you'd like to use - there are a number of
choices (at least on my version of findutils)
-regextype type
Changes the regular expression syntax understood by -regex and
-iregex tests which occur later on the command line. Currently-
implemented types are emacs (this is the default), posix-awk,
posix-basic, posix-egrep and posix-extended.
[...]
Post by Stuart Sears
n.b. these examples all from
find (GNU findutils) 4.5.10
The -regextype option was introduced in findutils 4.2.24: before that
release, GNU locate's --regex option used POSIX BREs and GNU find
generally used Emacs-compatible regexes except when the maintainer
forgot for a release or two :) (This behaviour was somewhat suboptimal,
hence the addition of tests and -regextype to make it hard to get it
wrong again.)
--
NULL && (void)
--
Gllug mailing list - ***@gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
Loading...