bug-findutils
[Top][All Lists]
Advanced

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

Re: find utility to start search directory listed in a file


From: Bernhard Voelker
Subject: Re: find utility to start search directory listed in a file
Date: Thu, 2 Sep 2021 23:30:45 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.0.1

On 9/1/21 14:36, Budi wrote:
> How do have find utility to start search directory listed in a file ?
> 
>  find "`cat a.txt`"
> can't go right

no, all the file names in 'a.txt' would be passed as one string to
the find utility due to the "..." quoting around.

  $ cat a.txt
  hello
  world

  $ find "`cat a.txt`"
  find: 'hello\nworld': No such file or directory

Without the quoting, it would work (given the 2 files exist):

  $ find `cat a.txt` -exec ls -logd '{}' +
  -rw-r--r-- 1 0 Sep  2 22:59 hello
  -rw-r--r-- 1 0 Sep  2 22:59 world

BUT: this will not work, if the file names in 'a.txt':
- contain white-spaces or other special characters (like a newline),
- begin with a '-' minus (because find would treat this as an option,
- are too many to fit in one command line.

The next version of the GNU findutils will introduce a new option to
safely handle starting points from a file: -files0-from.  It is a GNU
extension, but it allows an arbitrary number of files with arbitrary
file names to be passed.  The separator is the ASCII NUL character '\0'.
See description here:

https://git.sv.gnu.org/cgit/findutils.git/tree/doc/find.texi?id=a5659a42fa#n385

The above example would look like:

  $ printf '%s\0' 'hello' 'world' | find -files0-from - -exec ls -logd '{}' +
  -rw-r--r-- 1 0 Sep  2 22:59 hello
  -rw-r--r-- 1 0 Sep  2 22:59 world

The code is already in our Git repo, so if you may want to try, then
you're encouraged to build your own find from there and test.

With that new option, one could even feed another 'find' process with the
NUL-separated output from a previous one via pipes; here's an extreme
example where the 1st find selects regular files, the 2nd filter empty ones,
and the 3rd and last one filters files not owned by 'root':

  $ find /tmp -xdev -type f -print0 \
      | find -files0-from - -maxdepth 0 -empty -print0 \
      | find -files0-from - -maxdepth 0 -not -user root -exec ls -ld '{}' +

The -maxdepth 0 is not necessary here as the 1st find ensures that only
regular files are given, but it illustrates that the 2nd/3rd 'find' processes
shouldn't descend into directories.

Have a nice day,
Berny



reply via email to

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