bug-findutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing directories and files


From: Bernhard Voelker
Subject: Re: Passing directories and files
Date: Wed, 25 May 2022 19:35:10 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]