Unix Shell - find files are either "test*" or "TEST*"

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > find files are either "test*" or "TEST*"





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author find files are either "test*" or "TEST*"
wong_powah@yahoo.ca

2007-12-10, 7:20 pm

How to find files that are either "test*" or "TEST*" with bash shell?
I will use "-exec" option on the files.
These does not work:
find . -name "test*|TEST*"
find . -name "(test*|TEST*)"
find . -name "\(test*|TEST*\)"

Thanks.
Chris F.A. Johnson

2007-12-10, 7:20 pm

On 2007-12-10, wong_powah@yahoo.ca wrote:
>
>
> How to find files that are either "test*" or "TEST*" with bash shell?


The shell you are using is irrelevant; you use find, not the shell.

> I will use "-exec" option on the files.
> These does not work:
> find . -name "test*|TEST*"
> find . -name "(test*|TEST*)"
> find . -name "\(test*|TEST*\)"


find . -name test -o -name TEST

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Martin Klar

2007-12-10, 7:20 pm

wong_powah@yahoo.ca schrieb:
> How to find files that are either "test*" or "TEST*" with bash shell?
> I will use "-exec" option on the files.
> These does not work:
> find . -name "test*|TEST*"
> find . -name "(test*|TEST*)"
> find . -name "\(test*|TEST*\)"


Do you know that

find . -iname "test*"

does case-insensitive search? (It also matches TeSt ...)

HTH Martin
mallin.shetland

2007-12-10, 7:20 pm

wong_powah@yahoo.ca scrisse:

> These does not work:
> find . -name "test*|TEST*"
> find . -name "(test*|TEST*)"
> find . -name "\(test*|TEST*\)"



These does work:
find . -name 'test*' -or -name 'TEST*'

PS A wildcard is not a regular expression.

PS2 man find

Bill Marcum

2007-12-10, 7:20 pm

On 2007-12-10, wong_powah@yahoo.ca <wong_powah@yahoo.ca> wrote:
>
>
> How to find files that are either "test*" or "TEST*" with bash shell?
> I will use "-exec" option on the files.
> These does not work:
> find . -name "test*|TEST*"
> find . -name "(test*|TEST*)"
> find . -name "\(test*|TEST*\)"
>
> Thanks.


find . -iname "test*"
or if that gives an error
find . -name "test*" -o -name "TEST*"
(-iname will find any combination of upper and lower case, like "Test",
"tESt", etc)
Wayne

2007-12-10, 7:20 pm

wong_powah@yahoo.ca wrote:
> How to find files that are either "test*" or "TEST*" with bash shell?
> I will use "-exec" option on the files.
> These does not work:
> find . -name "test*|TEST*"
> find . -name "(test*|TEST*)"
> find . -name "\(test*|TEST*\)"
>
> Thanks.


The shell doesn't matter, this is a function of your find command.
If using Gnu version of find, try "-iname test\*. If you need
to disallow "Test", "tEsT", and other such variants, and you
really want to use regular expressions, use:

find -regex '.*/\(TEST\|test\)$'

But this is probably the most portable and simplest:

find \( -name TEST\* -o -name test\* \) -exec ...

-Wayne
Richard W

2007-12-11, 1:37 am

> The shell doesn't matter, this is a function of your find command.
> If using Gnu version of find, try "-iname test\*. If you need
> to disallow "Test", "tEsT", and other such variants, and you
> really want to use regular expressions, use:
>
> find -regex '.*/\(TEST\|test\)$'


More exact,

find -regex '.*/\(TEST\|test\)[^/]*'

> But this is probably the most portable and simplest:
>
> find \( -name TEST\* -o -name test\* \) -exec ...
>
> -Wayne

Stephane Chazelas

2007-12-11, 7:32 am

On Mon, 10 Dec 2007 23:19:38 +0100, mallin.shetland wrote:
> wong_powah@yahoo.ca scrisse:
>
>
>
> These does work:
> find . -name 'test*' -or -name 'TEST*'
>
> PS A wildcard is not a regular expression.

[...]

Note that if you want to add the -print to make it clear which
action you want to be take on each matching file, you need to
write it:

find . \( -name 'test*' -or -name 'TEST*' \) -print

as the AND (-a here implicit) has precedence over the OR (-o)
operator.

--
Stephane
wong_powah@yahoo.ca

2007-12-11, 1:24 pm

On Dec 10, 5:24 pm, Wayne <nos...@all4me.invalid> wrote:
> wong_po...@yahoo.ca wrote:
>
>
> The shell doesn't matter, this is a function of your find command.
> If using Gnu version of find, try "-iname test\*. If you need
> to disallow "Test", "tEsT", and other such variants, and you
> really want to use regular expressions, use:
>
> find -regex '.*/\(TEST\|test\)$'
>
> But this is probably the most portable and simplest:
>
> find \( -name TEST\* -o -name test\* \) -exec ...
>
> -Wayne


Further question:
I want to run a sed script on each of the matched files to create
a .new file for each matched file (because I do not want to overwrite
the original file), but this only create a "{}.new" file.
find \( -name TEST\* -o -name test\* \) -exec sed -f /home/powah/sed/
test.sed {} > {}.new \;

Stephane Chazelas

2007-12-11, 1:24 pm

On Tue, 11 Dec 2007 07:57:23 -0800 (PST), wong_powah@yahoo.ca wrote:
[...]
> Further question:
> I want to run a sed script on each of the matched files to create
> a .new file for each matched file (because I do not want to overwrite
> the original file), but this only create a "{}.new" file.
> find \( -name TEST\* -o -name test\* \) -exec sed -f /home/powah/sed/
> test.sed {} > {}.new \;


Note that redirection is a shell's idiom. Here, you're
redirecting find's output, > {}.new are not parameters passed to
find but instructions to the command interpreter to redirect the
command's output.

find ... -exec sh -Cc '
exec sed -f .../test.sed < "$1" > "$1".new
' {} {} \;

(the -C option is to prevent the shell from overriding existing
files).

--
Stephane
wong_powah@yahoo.ca

2007-12-11, 7:24 pm

On Dec 11, 11:40 am, Stephane Chazelas <stephane_chaze...@yahoo.fr>
wrote:
> On Tue, 11 Dec 2007 07:57:23 -0800 (PST), wong_po...@yahoo.ca wrote:
>
> [...]
>
>
> Note that redirection is a shell's idiom. Here, you're
> redirecting find's output, > {}.new are not parameters passed to
> find but instructions to the command interpreter to redirect the
> command's output.
>
> find ... -exec sh -Cc '
> exec sed -f .../test.sed < "$1" > "$1".new
> ' {} {} \;
>
> (the -C option is to prevent the shell from overriding existing
> files).
>
> --
> Stephane


If I want to overwrite all the original files (i.e. mv test.new test),
how to do it?
Stephane Chazelas

2007-12-12, 7:31 am

On Tue, 11 Dec 2007 11:46:09 -0800 (PST), wong_powah@yahoo.ca wrote:
[...]
>
> If I want to overwrite all the original files (i.e. mv test.new test),
> how to do it?


find ... -exec sh -eCc '
sed -f .../test.sed < "$1" > "$1".new
mv -- "$1.new" "$1"
' {} {} \;

sh -c is a way to write inline scripts like that.

The -e option is to stop the scripts execution at the first
non-successful command.

Note that the command above will possibly change the ownership,
permissions and inode of the files. Another way to prevent that
is to do it:

find ... -exec sh -eCc '
cat < "$1" > "$1.back"
sed -f .../test.sed < "$1.back" > "$1"
rm -f -- "$1.back"
' {} {} \;

If there's a problem during the operation, at least, you'll have
the .back file to recover the original.

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com