Re: 'find' do multi path search be completed and output once at a time

Bernhard Voelker <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs,gmane.comp.gnu.coreutils.general
Message-ID <[email protected]>
On 10/25/20 2:22 AM, Budi wrote:
> Can 'find'  do multi path search be completed and output once at a
> time each in merely one execution/invocation

First of all, 'find' is not part of the GNU coreutils but instead maintained
in the GNU findutils package.  Thus, you'd better be off asking questions
where it tell you:

   $ find --help | tail -n5
   Please see also the documentation at http://www.gnu.org/software/findutils/.
   You can report (and track progress on fixing) bugs in the "find"
   program via the GNU findutils bug-reporting page at
   https://savannah.gnu.org/bugs/?group=findutils or, if
   you have no web access, by sending email to <[email protected]>.

Thus, I'm CCing the bug-findutils mailing list.

Second, I'm not sure from the above sentence what you mean by "multi path search".

> Problem is if done use -o to do the multi path search it'll show all
> result in immediate time e.g:
> 
> find . -path *.sh -o -path *.c

The unquoted patterns *.sh and *.c are for sure a problem.
You should quote each of them like '*.sh' and '*.c'.

Furthermore, as you're only referring to the basename of each file processed,
you could use the -name test instead of -path (which matches against the full
path strings relative to the starting argument '.').

> bash.sh
> 
> bash1.sh
> 
> main.c
> 
> bash2.sh
> 
> main1.c
> 
> 
> what needed is to output this, in one execution/invocation:
> 
> bash.sh
> 
> bash1.sh
> 
> bash2.sh
> 
> main.c
> 
> main1.c
> 
> Is it possible

Ah, do you mean that you first want the result of the search for files matching '*.sh',
and then those for searching for files matching '*.c'?

As Lion Yang already suggested, I'd do this by the well-known decorate-sort-undecorate
pattern, i.e., adding a marker to each pattern when printing, then sorting by that
pattern, and finally removing the pattern again.  Lion suggested something like:

   find .  -name '*.sh' -printf 'a:%p\n' -o -name '*.c' -printf 'b:%p\n' \
     | sort -s -t':' -k1,1 \
     | cut -d':' -f2-

Please note that the above will probably work in most of your cases, but will fail
e.g. in the case a file name include a newline character ('\n').  You'd have to
cater for this by chosing the safer NUL '\0' character as separator between the
file names, and teaching the post-processing commands 'sort' and 'cut' to use
that as well (-z options); likewise for the part which consumes the final output.

Have a nice day,
Berny
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.