0

I have a GNU find command

find . -type l,f ! -path './bin/ash' <600 more condition like ! -path './bin/mv'> -printf "rm %P\n"

which runs in a directory with a bin/ash file (a symlink).

Problem: find prints rm bin/ash. Why does it do it?

find is supposed to add sequential conditions with -a, not -o. A shorter query works. I checked for paths that break quotation, but there are none.

What has no effect:

  • Switching type query part to \( -type l -o -type f \) .
  • Adding -a between all conditions.
  • Adding or removing ./ to paths.
  • Adding \( \) around the -path condition group or individual conditions.
  • Replacing ! with -not has no effect

Did I run into a bug in GNU find, or am I missing anything?

find (GNU findutils) 4.7.0
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)

Update: MCVE. On my Ubuntu 20.04 box, I do the following:

cd /usr/bin/
find . -type l,f > ~/mcve.txt
FIND_CONDITION=$(cat ~/mcve.txt | xargs printf " ! -path '%s' " | paste -s)
find . -type l,f ${FIND_CONDITION} -printf "rm %P\n"

The last command prints all the 2150 files/symlinks from /usr/bin/.

uname -a: Linux MyHostname 5.8.0-36-generic #40~20.04.1-Ubuntu SMP Wed Jan 6 10:15:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
  • `in GNU find` Just to be sure, what does `find --version` print? Would you be able to post an MCVE with some `mkdir -p tempdir/bin; cd tempdir; ln -s sh bin/ash; cp /bin/sh bin` ? `Replacing ! with -not has no effect` Note that history expansion in shell replaces `!` - I recommend quoting it `'!'` to be safe. Anyway, I can't reproduce - created a folder, with a symlink and a binary file, run it - all fine, symlink with the path `./bin/ash` is ignored. Maybe something in those `600 more condition like`? – KamilCuk Feb 03 '21 at 08:51
  • 1
    Updated the text. Working on MCVE that I can disclose. – Victor Sergienko Feb 03 '21 at 08:53
  • 1
    All of the single quotes you are adding to `FIND_CONDITION` are treated as literal parts of the file names, not shell quoting. See http://mywiki.wooledge.org/BashFAQ/050. – chepner Feb 03 '21 at 14:28
  • That is, there is a file named `bin/ash`; there is no file named `'bin/ash'`. – chepner Feb 03 '21 at 14:29
  • @chepner, I see, not the first time this bites me. Thank you! Won't you please convert the comment into an answer? – Victor Sergienko Feb 03 '21 at 19:31

0 Answers0