Make 'find -regextype egrep' as alias
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am just starting learning regex and want to use it instead of others everywhere for practice.
I encounter such a situation when tried to find files with extensions sh or md
$ find . regex ".*.(sh|md)$"
.
./bogus.py
./cofollow.py
./data8.txt
./example.sh
./longest_word_2.sh
./posit_param.sh
./cobroadcast2.py
Unfortunately it output /bogus.py
,
I notice the BRE rules and tried escape ()
$ find . -regex ".*.(sh|md)$"
#get nothing return
After series of search, I got -regextype solution Regular Expressions - Finding Files
$ find . -regextype posix-extended -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
$ find . -regextype egrep -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
Additionally, a nice modular solution
$ find -type f | egrep ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
However, there is a shortcut in BSD to accomplish such a task
with a -E
predicate.
$ /usr/bin/find -E . -regex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
I am determined to exclusively take the GNU tool in order to make my codes and skills portable.
So I am starting to alias 'find -regextype egrep`,
Unfortunately find obtain the $1 as path.
How could I solve them problem in a handy way?
bash
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
I am just starting learning regex and want to use it instead of others everywhere for practice.
I encounter such a situation when tried to find files with extensions sh or md
$ find . regex ".*.(sh|md)$"
.
./bogus.py
./cofollow.py
./data8.txt
./example.sh
./longest_word_2.sh
./posit_param.sh
./cobroadcast2.py
Unfortunately it output /bogus.py
,
I notice the BRE rules and tried escape ()
$ find . -regex ".*.(sh|md)$"
#get nothing return
After series of search, I got -regextype solution Regular Expressions - Finding Files
$ find . -regextype posix-extended -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
$ find . -regextype egrep -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
Additionally, a nice modular solution
$ find -type f | egrep ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
However, there is a shortcut in BSD to accomplish such a task
with a -E
predicate.
$ /usr/bin/find -E . -regex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
I am determined to exclusively take the GNU tool in order to make my codes and skills portable.
So I am starting to alias 'find -regextype egrep`,
Unfortunately find obtain the $1 as path.
How could I solve them problem in a handy way?
bash
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am just starting learning regex and want to use it instead of others everywhere for practice.
I encounter such a situation when tried to find files with extensions sh or md
$ find . regex ".*.(sh|md)$"
.
./bogus.py
./cofollow.py
./data8.txt
./example.sh
./longest_word_2.sh
./posit_param.sh
./cobroadcast2.py
Unfortunately it output /bogus.py
,
I notice the BRE rules and tried escape ()
$ find . -regex ".*.(sh|md)$"
#get nothing return
After series of search, I got -regextype solution Regular Expressions - Finding Files
$ find . -regextype posix-extended -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
$ find . -regextype egrep -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
Additionally, a nice modular solution
$ find -type f | egrep ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
However, there is a shortcut in BSD to accomplish such a task
with a -E
predicate.
$ /usr/bin/find -E . -regex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
I am determined to exclusively take the GNU tool in order to make my codes and skills portable.
So I am starting to alias 'find -regextype egrep`,
Unfortunately find obtain the $1 as path.
How could I solve them problem in a handy way?
bash
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am just starting learning regex and want to use it instead of others everywhere for practice.
I encounter such a situation when tried to find files with extensions sh or md
$ find . regex ".*.(sh|md)$"
.
./bogus.py
./cofollow.py
./data8.txt
./example.sh
./longest_word_2.sh
./posit_param.sh
./cobroadcast2.py
Unfortunately it output /bogus.py
,
I notice the BRE rules and tried escape ()
$ find . -regex ".*.(sh|md)$"
#get nothing return
After series of search, I got -regextype solution Regular Expressions - Finding Files
$ find . -regextype posix-extended -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
$ find . -regextype egrep -iregex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
Additionally, a nice modular solution
$ find -type f | egrep ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
./table_regex_bat.md
However, there is a shortcut in BSD to accomplish such a task
with a -E
predicate.
$ /usr/bin/find -E . -regex ".*.(sh|md)$"
./example.sh
./longest_word_2.sh
./posit_param.sh
I am determined to exclusively take the GNU tool in order to make my codes and skills portable.
So I am starting to alias 'find -regextype egrep`,
Unfortunately find obtain the $1 as path.
How could I solve them problem in a handy way?
bash
bash
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 4 hours ago


Inian
3,338822
3,338822
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago
Sawajiri
1926
1926
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Sawajiri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Don't use an alias
to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed
regexFind()
(( "$#" ))
and call the function as
regexFind "/home/foo/bar"
Also to add to your findings, note that bash
also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s
enables the option and -u
disables it.
The nullglob
allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh
and *.md
, you just need to navigate to that particular directory and do
shopt -s nullglob
fileList=(*.sh)
fileList+=(*.md)
shopt -u nullglob
and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.
printf '%sn' "$fileList[@]"
add a comment |Â
up vote
1
down vote
find . -type f ( -name '*.sh' -o -name '*.md' )
This would work with all implementations of find
since it doesn't require support for regular expression matching.
To make this more flexible:
suffixfind () (
dir=$1
shift
for suf do
set -- "$@" -o -name "*.$suf"
shift
done
shift
find "$dir" -type f ( "$@" )
)
This helper shell function (which would work in any sh
-like shell) would pick out the first command line argument and put it in the variable dir
. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.)
with all the filename suffixes on the function's command line before calling find
with that list to find files in or under $dir
.
You would use it like
suffixfind /usr sh md txt
to find all regular files with names ending in .sh
, .md
or .txt
in or under the path /usr
.
About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g
prefix to the tool names. GNU find
would therefore be available as gfind
to distinguish it from the native find
implementation on the system.
Your "GNU portable" approach would therefore have to test whether gfind
was available before testing whether find
is in fact GNU find
. Not until you've done that (possibly by testing the return status and output of find --version
) can you be comfortable in knowing that you are dealing with GNU find
.
And a big portability problem is that you cannot e.g. access GNU find under it's native namegfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
– schily
23 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Don't use an alias
to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed
regexFind()
(( "$#" ))
and call the function as
regexFind "/home/foo/bar"
Also to add to your findings, note that bash
also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s
enables the option and -u
disables it.
The nullglob
allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh
and *.md
, you just need to navigate to that particular directory and do
shopt -s nullglob
fileList=(*.sh)
fileList+=(*.md)
shopt -u nullglob
and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.
printf '%sn' "$fileList[@]"
add a comment |Â
up vote
3
down vote
accepted
Don't use an alias
to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed
regexFind()
(( "$#" ))
and call the function as
regexFind "/home/foo/bar"
Also to add to your findings, note that bash
also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s
enables the option and -u
disables it.
The nullglob
allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh
and *.md
, you just need to navigate to that particular directory and do
shopt -s nullglob
fileList=(*.sh)
fileList+=(*.md)
shopt -u nullglob
and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.
printf '%sn' "$fileList[@]"
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Don't use an alias
to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed
regexFind()
(( "$#" ))
and call the function as
regexFind "/home/foo/bar"
Also to add to your findings, note that bash
also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s
enables the option and -u
disables it.
The nullglob
allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh
and *.md
, you just need to navigate to that particular directory and do
shopt -s nullglob
fileList=(*.sh)
fileList+=(*.md)
shopt -u nullglob
and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.
printf '%sn' "$fileList[@]"
Don't use an alias
to pass arguments around. They are not portable and useful only on interactive shells. Use a function instead and pass the arguments as paths needed
regexFind()
(( "$#" ))
and call the function as
regexFind "/home/foo/bar"
Also to add to your findings, note that bash
also has an intrinsic way to glob files. You just need to enable a couple of extended shell options to make it work. The -s
enables the option and -u
disables it.
The nullglob
allows to ignore un-expanded glob results as valid matches. So assuming you want to match files ending with *.sh
and *.md
, you just need to navigate to that particular directory and do
shopt -s nullglob
fileList=(*.sh)
fileList+=(*.md)
shopt -u nullglob
and print the results to see below. Remember to quote the expansion to prevent the filenames from undergoing Word-Splitting.
printf '%sn' "$fileList[@]"
edited 4 hours ago
answered 4 hours ago


Inian
3,338822
3,338822
add a comment |Â
add a comment |Â
up vote
1
down vote
find . -type f ( -name '*.sh' -o -name '*.md' )
This would work with all implementations of find
since it doesn't require support for regular expression matching.
To make this more flexible:
suffixfind () (
dir=$1
shift
for suf do
set -- "$@" -o -name "*.$suf"
shift
done
shift
find "$dir" -type f ( "$@" )
)
This helper shell function (which would work in any sh
-like shell) would pick out the first command line argument and put it in the variable dir
. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.)
with all the filename suffixes on the function's command line before calling find
with that list to find files in or under $dir
.
You would use it like
suffixfind /usr sh md txt
to find all regular files with names ending in .sh
, .md
or .txt
in or under the path /usr
.
About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g
prefix to the tool names. GNU find
would therefore be available as gfind
to distinguish it from the native find
implementation on the system.
Your "GNU portable" approach would therefore have to test whether gfind
was available before testing whether find
is in fact GNU find
. Not until you've done that (possibly by testing the return status and output of find --version
) can you be comfortable in knowing that you are dealing with GNU find
.
And a big portability problem is that you cannot e.g. access GNU find under it's native namegfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
– schily
23 mins ago
add a comment |Â
up vote
1
down vote
find . -type f ( -name '*.sh' -o -name '*.md' )
This would work with all implementations of find
since it doesn't require support for regular expression matching.
To make this more flexible:
suffixfind () (
dir=$1
shift
for suf do
set -- "$@" -o -name "*.$suf"
shift
done
shift
find "$dir" -type f ( "$@" )
)
This helper shell function (which would work in any sh
-like shell) would pick out the first command line argument and put it in the variable dir
. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.)
with all the filename suffixes on the function's command line before calling find
with that list to find files in or under $dir
.
You would use it like
suffixfind /usr sh md txt
to find all regular files with names ending in .sh
, .md
or .txt
in or under the path /usr
.
About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g
prefix to the tool names. GNU find
would therefore be available as gfind
to distinguish it from the native find
implementation on the system.
Your "GNU portable" approach would therefore have to test whether gfind
was available before testing whether find
is in fact GNU find
. Not until you've done that (possibly by testing the return status and output of find --version
) can you be comfortable in knowing that you are dealing with GNU find
.
And a big portability problem is that you cannot e.g. access GNU find under it's native namegfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
– schily
23 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
find . -type f ( -name '*.sh' -o -name '*.md' )
This would work with all implementations of find
since it doesn't require support for regular expression matching.
To make this more flexible:
suffixfind () (
dir=$1
shift
for suf do
set -- "$@" -o -name "*.$suf"
shift
done
shift
find "$dir" -type f ( "$@" )
)
This helper shell function (which would work in any sh
-like shell) would pick out the first command line argument and put it in the variable dir
. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.)
with all the filename suffixes on the function's command line before calling find
with that list to find files in or under $dir
.
You would use it like
suffixfind /usr sh md txt
to find all regular files with names ending in .sh
, .md
or .txt
in or under the path /usr
.
About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g
prefix to the tool names. GNU find
would therefore be available as gfind
to distinguish it from the native find
implementation on the system.
Your "GNU portable" approach would therefore have to test whether gfind
was available before testing whether find
is in fact GNU find
. Not until you've done that (possibly by testing the return status and output of find --version
) can you be comfortable in knowing that you are dealing with GNU find
.
find . -type f ( -name '*.sh' -o -name '*.md' )
This would work with all implementations of find
since it doesn't require support for regular expression matching.
To make this more flexible:
suffixfind () (
dir=$1
shift
for suf do
set -- "$@" -o -name "*.$suf"
shift
done
shift
find "$dir" -type f ( "$@" )
)
This helper shell function (which would work in any sh
-like shell) would pick out the first command line argument and put it in the variable dir
. Then it would construct a list of -name "*.<suf1>" -o -name "*.<suf2>" (etc.)
with all the filename suffixes on the function's command line before calling find
with that list to find files in or under $dir
.
You would use it like
suffixfind /usr sh md txt
to find all regular files with names ending in .sh
, .md
or .txt
in or under the path /usr
.
About your mentioning of GNU tools and portability: Note that GNU tools on non-Linux systems are sometimes available, but with a g
prefix to the tool names. GNU find
would therefore be available as gfind
to distinguish it from the native find
implementation on the system.
Your "GNU portable" approach would therefore have to test whether gfind
was available before testing whether find
is in fact GNU find
. Not until you've done that (possibly by testing the return status and output of find --version
) can you be comfortable in knowing that you are dealing with GNU find
.
edited 16 mins ago
answered 1 hour ago


Kusalananda
113k15216344
113k15216344
And a big portability problem is that you cannot e.g. access GNU find under it's native namegfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
– schily
23 mins ago
add a comment |Â
And a big portability problem is that you cannot e.g. access GNU find under it's native namegfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.
– schily
23 mins ago
And a big portability problem is that you cannot e.g. access GNU find under it's native name
gfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.– schily
23 mins ago
And a big portability problem is that you cannot e.g. access GNU find under it's native name
gfind
if you are on Linux. So you are right, the portable way is to use POSIX features only - in special if you can do the job with POSIX features.– schily
23 mins ago
add a comment |Â
Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.
Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.
Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.
Sawajiri is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479298%2fmake-find-regextype-egrep-as-alias%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password