Re: Passing directories and files

Bernhard Voelker <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <[email protected]>
On 5/25/22 14:08, goncholden via Bug reports for the GNU find utilities wrote:
> I am using the FIND command on a number of directories stored in FDIR array to execute HEAD on each file. How can I adapt the code so users can also include files as well as directories?

well, I don't think there's anything to adjust, because `find` already
accepts both files and directories as starting points.

> hn=8
> nf=${#fdir[@]}
> for (( i=0 ; i < $nf ; i++ )); do
> find "${fdir[$i]}" -type f \
> -exec head -v -n "$hn" '{}' +
> done
> The synopsis of the GNU find command is
> 
> find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
> 
> where [starting-point...] is zero or more paths. There is nothing stopping the user from passing files as starting points

no problem, find will just work well both with regular files or directories.
For the latter, it will descend the directory hierarchy, obviously.

> Example, given:
> 
> $ cat subdir/somefile
> foo
> bar
> baz
> 
> then

the following came out a bit mangled via email.

> $ find subdir/somefile -
> 
> type
> 
> f -
> 
> exec
> 
> head -n 2 {} +
> foo
> bar
> 
> I could just do
> 
> find
> 
> "
> 
> ${fdir[@]}
> 
> "
> 
> -
> 
> type
> 
> f -
> 
> exec
> 
> head -v -n
> 
> "
> 
> $hn
> 
> "
> 
> '{}'
> 
> +
> 
> regardless of the mix of directories and files in FDIR.
> 
> But is this the right and proper way to use the FIND command?

Sure, why not? ;-)

  $ find subdir/somefile -type f -exec head -v -n1 '{}' +
  ==> subdir/somefile <==
  foo

  $ find subdir -type f -exec head -v -n2 '{}' +
  ==> subdir/somefile <==
  foo

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.