Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] find: use single instead of double quotes with *
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Thu Sep 17, 2020 9:33 pm    Post subject: [SOLVED] find: use single instead of double quotes with * Reply with quote

Hello,
please tell me why
Code:
find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*\.exe" ! -iname "*\.dll" ! -iname "*\.iso"
does what I want ( *.exe$ … -files are omitted).
But
Code:
 find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*games*"
does output paths containing 'games'.

I know that I could use 'grep -v …' but I'd rather like to do find all the stuff because the query is much bigger and I would like to avoid -print0 / --null … if possible.

Actually I'd like to use printf to escape all the paths.
I hoped that
Code:
find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*\.exe" ! -iname "*\.dll" ! -iname "*\.iso"   -exec echo $(printf %q '{}' ) \; >>escaped_paths.txt
would work but all I get are lines containing solely
Quote:
\{\}
:? Why is '{}' not replaced by the line found (one by one)?

What's the mistake here?
Thanks in advance!


EDITS:
- renamed to what's probably a much better title (from "can't find mistake in a find query")
- marked as solved
- 'to quote all the paths' -> 'to escape …'


Last edited by as.gentoo on Sat Sep 19, 2020 6:19 pm; edited 3 times in total
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 7:05 am    Post subject: Reply with quote

Show your issue on some common linux tree or make your own with:
Code:
#!/bin/bash
set -x
b=$(dirname $0)
c=$b/AfewSamples/deeper
mkdir -p $c
for i in abcinstall installabc abcsetup setupabc abc.exe abc.dll \
         abc.iso abcgames gamesabc abcsomemore somemoreabc setup.exe \
         install.iso setup.dll games.setup installgames setupgamesabc ; do
    touch $c/$i
done

s_base_dir="$b"
echo "---------------- first find"
find ${s_base_dir} -type f     \( -iname '*install*' -o -iname '*setup*' \)    ! -iname '*\.exe' ! -iname '*\.dll' ! -iname '*\.iso'
echo "---------------- second"
find ${s_base_dir} -type f     \( -iname '*install*' -o -iname '*setup*' \)    ! -iname '*games*'
above Script makes a tree "AfewSamples/deeper" down under and outputs as requested two times a find command on an ext4 filesystem.

[edit:] yes, e3k has catched your issue probably: a typical bash quoting error with "*"
_________________
ultra left enrich


Last edited by ulenrich on Fri Sep 18, 2020 8:19 am; edited 5 times in total
Back to top
View user's profile Send private message
e3k
Guru
Guru


Joined: 01 Oct 2007
Posts: 516
Location: Inner Space

PostPosted: Fri Sep 18, 2020 8:01 am    Post subject: Re: can't find mistake in a find query Reply with quote

as.gentoo wrote:
But
Code:
 find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*games*"
does output paths containing 'games'.


could you try to run this with * escaped? like \* or:
Code:
find ${s_base_dir} -type f     \( -iname "\*install\*" -o -iname "\*setup\*" \)    ! -iname "\*games\*"

_________________

Flux & Contemplation - Portrait of an Artist in Isolation

Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Fri Sep 18, 2020 8:46 am    Post subject: Reply with quote

ulenrich wrote:
Show your issue on some common linux tree

doing that on /usr and excluding everything that contains share does still show /usr/share* and other paths
Code:
$ s_base_dir="/usr" ; sudo find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*share*" | grep share | head -n3
/usr/portage/x11-misc/seetxt/files/0.72-0002-fix-shared-files-install.patch
/usr/share/aclocal/ax_boost_log_setup.m4
/usr/share/aclocal/ax_f90_library_setup.m4
$
$ s_base_dir="/usr" ; sudo find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "share" | grep share | tail -n3
/usr/lib64/libreoffice/share/config/soffice.cfg/desktop/ui/updateinstalldialog.ui
/usr/lib64/libreoffice/share/config/soffice.cfg/svt/ui/printersetupdialog.ui
/usr/lib64/metasploit4.17/lib/msf/core/exploit/smb/server/share/command/session_setup_andx.rb
$


e3k wrote:
as.gentoo wrote:
But
Code:
 find ${s_base_dir} -type f     \( -iname "*install*" -o -iname "*setup*" \)    ! -iname "*games*"
does output paths containing 'games'.


could you try to run this with * escaped? like \* or:
Code:
find ${s_base_dir} -type f     \( -iname "\*install\*" -o -iname "\*setup\*" \)    ! -iname "\*games\*"
That results in no output at all.
Code:
$ s_base_dir="/usr" ; sudo find ${s_base_dir} -type f     \( -iname "\*install\*" -o -iname "\*setup\*" \)    ! -iname "\*share\*"
$
Back to top
View user's profile Send private message
e3k
Guru
Guru


Joined: 01 Oct 2007
Posts: 516
Location: Inner Space

PostPosted: Fri Sep 18, 2020 10:36 am    Post subject: Reply with quote

probably without the qutes: -iname "\*games\*" -> -iname \*games\*
_________________

Flux & Contemplation - Portrait of an Artist in Isolation

Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 10:49 am    Post subject: Reply with quote

single quotes are safe:
Code:
find /usr/lib64 -type f  \( -iname '*install*' -o -iname '*setup*'  \)  ! -ipath '*share*'

and attention with -ipath
_________________
ultra left enrich
Back to top
View user's profile Send private message
e3k
Guru
Guru


Joined: 01 Oct 2007
Posts: 516
Location: Inner Space

PostPosted: Fri Sep 18, 2020 11:03 am    Post subject: Reply with quote

no single quotes would search for * character instead of * as any number of any chars imho.
_________________

Flux & Contemplation - Portrait of an Artist in Isolation

Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 11:06 am    Post subject: Reply with quote

it works:
Code:
# find ./gentoo/x11-misc -type f  \( -iname '*install*' -o -iname '*setup*'  \)  ! -ipath '*share*'
./gentoo/x11-misc/slim/files/slim-1.3.6-strip-systemd-unit-install.patch
./gentoo/x11-misc/xfractint/files/xfractint-20.04p16-install-phase.patch
./gentoo/x11-misc/xdialog/files/xdialog-2.3.1-install.patch

But mark this: -ipath also inhibits 'share' in the filename not only path!
Code:
# find ./gentoo/x11-misc -type f  \( -iname '*install*' -o -iname '*setup*'  \)
./gentoo/x11-misc/shared-mime-info/files/shared-mime-info-2.0-pkgconfig_install_path.patch
./gentoo/x11-misc/slim/files/slim-1.3.6-strip-systemd-unit-install.patch
./gentoo/x11-misc/xfractint/files/xfractint-20.04p16-install-phase.patch
./gentoo/x11-misc/seetxt/files/0.72-0002-fix-shared-files-install.patch
./gentoo/x11-misc/xdialog/files/xdialog-2.3.1-install.patch

_________________
ultra left enrich


Last edited by ulenrich on Fri Sep 18, 2020 11:16 am; edited 2 times in total
Back to top
View user's profile Send private message
e3k
Guru
Guru


Joined: 01 Oct 2007
Posts: 516
Location: Inner Space

PostPosted: Fri Sep 18, 2020 11:08 am    Post subject: Reply with quote

that proves that i again should learn bash. congratulations Ulenrich.
_________________

Flux & Contemplation - Portrait of an Artist in Isolation

Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 11:13 am    Post subject: Reply with quote

But you got the issue with double quotes and "*" first! I totally had ignored it.
_________________
ultra left enrich
Back to top
View user's profile Send private message
e3k
Guru
Guru


Joined: 01 Oct 2007
Posts: 516
Location: Inner Space

PostPosted: Fri Sep 18, 2020 11:20 am    Post subject: Reply with quote

for me it worked without quotes and * escaped.
_________________

Flux & Contemplation - Portrait of an Artist in Isolation

Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 11:25 am    Post subject: Reply with quote

Everyone his own taste! (That kind of choice makes bash a bad coding language - see the current python discussions). I like the single quote, but you cannot insert bash variables then.
_________________
ultra left enrich
Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Fri Sep 18, 2020 1:06 pm    Post subject: Reply with quote

Alright, first issue solved - thanks!

Code:
s_base_dir="/usr" ; sudo find ${s_base_dir} -type f     \( -iname '*install*' -o -iname '*setup*' \)    ! -ipath '*share*'


Now what's wrong about using '{}' in the exec statement?
Code:
$ sudo touch '/usr/ a +e$t setup \'
$ ls -al /usr/ | head -n4 | grep -vE '\.$|0$'
-rw-r--r-- 1 root    root       0 Sep 18 14:57  a +e$t setup \

$$ sudo touch '/usr/ a +e$t setup \' ; s_base_dir="/usr" ; sudo find ${s_base_dir} -type f     \( -iname '*install*' -o -iname '*setup*' \)    ! -ipath '*share*' -exec echo "$(printf %q '{}')" \; | head -n4
\{\}
\{\}
\{\}
\{\}
find: ‘echo’ terminated by signal 13
find: ‘echo’ terminated by signal 13
find: ‘echo’ terminated by signal 13

find: ‘echo’ terminated by signal 13
Same result when I omit the double quotes around $(printf %q '{}').

Last edited by as.gentoo on Fri Sep 18, 2020 1:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Fri Sep 18, 2020 1:13 pm    Post subject: Reply with quote

ulenrich wrote:
Everyone his own taste! (That kind of choice makes bash a bad coding language - see the current python discussions). I like the single quote, but you cannot insert bash variables then.
I assume something like
Code:
$ s_person='_we_'
$ s_temp='here'${s_person}'go'
$ echo ${s_temp}
will not cover all use cases?

Last edited by as.gentoo on Fri Sep 18, 2020 2:09 pm; edited 1 time in total
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 2:06 pm    Post subject: Reply with quote

You have concenated 3 strings but not inserted a veriable into a quote. Ugly for my eyes. But that is just taste.
_________________
ultra left enrich
Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Fri Sep 18, 2020 2:16 pm    Post subject: Reply with quote

ulenrich wrote:
You have concenated 3 strings but not inserted a veriable into a quote. Ugly for my eyes. But that is just taste.
Right. (Strictly speaking) As you already wrote only double quotes allow that in bash.
Setting
Code:
s_person='_they_'
will not change s_temp!
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Fri Sep 18, 2020 2:41 pm    Post subject: Reply with quote

as.gentoo wrote:
ulenrich wrote:
You have concenated 3 strings but not inserted a veriable into a quote. Ugly for my eyes. But that is just taste.
Right. (Strictly speaking) As you already wrote only double quotes allow that in bash.
Setting
Code:
s_person='_they_'
will not change s_temp!
You would like to look at "eval" for such.

The other "-exec" is what you want:
Code:
find ${s_base_dir} -type f   \( -iname '*install*' -o -iname '*setup*' \)  ! -ipath '*share*' -exec printf  '{}'" "  \;
Don't use sudo if not needed. I think all of /usr is readable but all of it should not be tempered with.
_________________
ultra left enrich
Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Fri Sep 18, 2020 10:12 pm    Post subject: Reply with quote

ulenrich wrote:
Don't use sudo if not needed. I think all of /usr is readable but all of it should not be tempered with.
100% agreed. But in this case - I use a liveDVD ATM - there is one folder (and its content) in /usr only accessible by root.
Code:
$ find /usr 1>/dev/zero
find: ‘/usr/share/polkit-1/rules.d’: Permission denied
$
$ sudo ls -al /usr/share/polkit-1/rules.d | grep -Ev '^total|\.$'
-rw-r--r-- 1 root    root 432 Apr 14  2019 01-org.blueman.rules
-rw-r--r-- 1 root    root 293 Apr  9  2019 01-org.freedesktop.ModemManager1.rules
-rw-r--r-- 1 root    root 265 Apr 24  2019 01-org.freedesktop.NetworkManager.settings.modify.system.rules
-rw-r--r-- 1 root    root 434 Apr 14  2019 blueman.rules
-rw-r--r-- 1 root    root 264 Apr 10  2019 org.gtk.vfs.file-operations.rules
$
And the place I'm really searching in contains a lot of files - either copied or compressed - that belonged to other user accounts or root - obviously I don't want them chown liveDVDuser.

ulenrich wrote:
find ${s_base_dir} -type f \( -iname '*install*' -o -iname '*setup*' \) ! -ipath '*share*' -exec printf '{}'" " \;

I didn't describe good enough what I want as output.
Code:
$ find ~/Downloads/ -iname 'z*'
/home/xxx/Downloads/z\hapehape
/home/xxx/Downloads/z!hapehape
/home/xxx/Downloads/z$hapehape
/home/xxx/Downloads/zhape!
/home/xxx/Downloads/z y x
$
$ # adapted --->
$ find ~/Downloads/ -iname 'z*' -exec printf '{}'" " \;
/home/xxx/Downloads/z\hapehape /home/xxx/Downloads/z!hapehape /home/xxx/Downloads/z$hapehape /home/xxx/Downloads/zhape! /home/xxx/Downloads/z y x xxx@xxx ~/Downloads $
$
$ ls /home/xxx/Downloads/z\hapehape
ls: cannot access '/home/xxx/Downloads/zhapehape': No such file or directory
$ echo $(printf %q '/home/xxx/Downloads/z\hapehape' )
/home/xxx/Downloads/z\\hapehape
$ #^ desired (one per line, escapped)
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Sat Sep 19, 2020 8:11 am    Post subject: Reply with quote

Code:
find ${s_base_dir} -type f  \( -iname '*install*' -o -iname '*setup*' \) ! -ipath '*share*' \
|while read a; do \
  /bin/echo $(/usr/bin/printf %q "$a") ; \
done

For example without double quotes behind the printf as in my example the blanks are eaten. I have used fullpath /bin/echo to be sure it is not the bash internal "echo" . Try piping the normal find output and experiment further. I am sure this following isn't yet what you want, but for example:
Code:
find ${s_base_dir} -type f  \( -iname '*install*' -o -iname '*setup*' \) ! -ipath '*share*' \
|sed -e's/ /\\ /g'

_________________
ultra left enrich
Back to top
View user's profile Send private message
as.gentoo
Guru
Guru


Joined: 07 Aug 2004
Posts: 319

PostPosted: Sat Sep 19, 2020 4:59 pm    Post subject: Reply with quote

ulenrich wrote:
Code:
find ${s_base_dir} -type f  \( -iname '*install*' -o -iname '*setup*' \) ! -ipath '*share*' \
|while read a; do \
  /bin/echo $(/usr/bin/printf %q "$a") ; \
done
^ Works fine if I use bash.printf for the conversion. GNU coreutils /usr/bin/printf should work too but doesn't in my case.
Code:
$ echo $(/usr/bin/printf %q ' /usr/a +e$t setup \')
' /usr/a +e$t setup \'
$ echo $(printf %q ' /usr/a +e$t setup \')
\ /usr/a\ +e\$t\ setup\ \\
$
Adapted version:
Code:
find "${s_base_dir}" -type f \( -iname '*install*' -o -iname '*setup*' \) ! -ipath '*share*' \
  | while read s_path ; do \
    echo $(printf %q "${s_path}") ;
  done
  >files_found_by_name_as_quoted_list.txt


Code:
…sed -e's/ /\\ /g'
Thought about that already but printf %q knows which chars need to be escaped and which not - I actually don't. :?
Of course using only one command (sed) is probably faster than a while loop with some commands. Maybe it can do search in parallel.

ulenrich and e3k - thanks for your help!

My question is answered and I do have the list of quoted file names I need.
(Next step is looking in all of those files content for patterns. : )


PS:
Why do you advisde not to use bash.echo?
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1415

PostPosted: Sat Sep 19, 2020 6:15 pm    Post subject: Reply with quote

Quote:
Why do you advisde not to use bash.echo?
Only in the first phase when you suspect some software is buggy. But on that one you made the experience the other way round :/
_________________
ultra left enrich
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum