Discussion:
glob path question
(too old to reply)
Corey H
2024-08-21 12:10:01 UTC
Permalink
Hi

I use this command trying to find a file in /etc whose name contains
"spf",

***@cloud:~# cd /etc/

***@cloud:/etc# ls *spf*
policyd-spf.conf

But this file is not listed by 'ls' command.

# ls /etc/policyd-spf.conf
ls: cannot access '/etc/policyd-spf.conf': No such file or directory

instead it's located in a subdir of /etc,

# cd /etc/postfix-policyd-spf-python/
# ls policyd-spf.conf
policyd-spf.conf

it seems strange to me. does glob will search for subdir but won't
return its path?

Thanks.
Michael Kjörling
2024-08-21 12:10:02 UTC
Permalink
I use this command trying to find a file in /etc whose name contains "spf",
policyd-spf.conf
But this file is not listed by 'ls' command.
# ls /etc/policyd-spf.conf
ls: cannot access '/etc/policyd-spf.conf': No such file or directory
instead it's located in a subdir of /etc,
# cd /etc/postfix-policyd-spf-python/
# ls policyd-spf.conf
policyd-spf.conf
it seems strange to me. does glob will search for subdir but won't return
its path?
The glob matches the directory. This causes `ls` to get the directory
name on its command line, which makes `ls` list the contents of that
directory.

For the behavior you want, try using `ls -d`. That will list the
matching directory entries (if any) without descending into
directories to list their contents.

Filename globbing is done by the shell, not by the invoked application
(such as `ls`).
--
Michael Kjörling 🔗 https://michael.kjorling.se
“Remember when, on the Internet, nobody cared that you were a dog?”
Cindy Sue Causey
2024-08-21 13:50:01 UTC
Permalink
Please forgive any formatting glitches. I'm still a brand newbie at
sending emails out of Evolution. :)

On Wed, Aug 21, 2024 at 8:08AM Michael Kjörling
Post by Michael Kjörling
Post by Corey H
But this file is not listed by 'ls' command.
# ls /etc/policyd-spf.conf
ls: cannot access '/etc/policyd-spf.conf': No such file or directory
The glob matches the directory. This causes `ls` to get the directory
name on its command line, which makes `ls` list the contents of that
directory.
For the behavior you want, try using `ls -d`. That will list the
matching directory entries (if any) without descending into
directories to list their contents.
Filename globbing is done by the shell, not by the invoked application
(such as `ls`).
All these years of using ls and locate myself, just had an ah-ha moment
triggered by the above. Tried my favored (cognitively friendly) grep:

$ ls /etc/*|grep wi -i
/etc/nsswitch.conf
/etc/usb_modeswitch.conf
type-windows.xml
x-window-manager
x-window-manager.1.gz
wicd
wicd
K01wicd
K01wicd
S01wicd
S01wicd
S01wicd
S01wicd
K01wicd
/etc/usb_modeswitch.d:
/etc/wicd:
wired-settings.conf
wireless-settings.conf

Next further tested "/etc/*/*" which returned yet more results,
including several complaints about "permission denied" as ls reached
deeper into each parent directory.

Pretty cool. Thanks for triggering that thought! I could have used it to
save brain pain many dozens of times over the last few years.

PS Yeah, I know, some directories go very deep. I'm pretending those
don't exist just this second. Searches I've performed are much specific
than just "wi" so the results list would remain small for deeper
queries. Works for my humble single user needs. :)

Cindy :)
--
Talking Rock,
t***@tuxteam.de
2024-08-21 14:20:01 UTC
Permalink
On Wed, Aug 21, 2024 at 09:44:34AM -0400, Cindy Sue Causey wrote:

[...]
Post by Cindy Sue Causey
All these years of using ls and locate myself, just had an ah-ha moment
[...]

Glad you enjoyed it :-)
Post by Cindy Sue Causey
PS Yeah, I know, some directories go very deep. I'm pretending those
don't exist just this second. Searches I've performed are much specific
than just "wi" so the results list would remain small for deeper
queries. Works for my humble single user needs. :)
In bash, and when the shell option globstar is set, a double star matches
arbitrary levels of subdirectories: "ls **/muh" lists the content of
"foofoo/foofoo/foofoo/muh". But it takes a while, since my home directory
is pretty full and it has, of course, to descend recursively all that
stuff :)

By default the option is unset, you set it with "shopt -s globstar".
See the bash man page.

Not very practical, IMO, but fun.

Cheers
--
tomás
t***@tuxteam.de
2024-08-21 12:20:01 UTC
Permalink
Hi
I use this command trying to find a file in /etc whose name contains "spf",
policyd-spf.conf
But this file is not listed by 'ls' command.
# ls /etc/policyd-spf.conf
ls: cannot access '/etc/policyd-spf.conf': No such file or directory
instead it's located in a subdir of /etc,
# cd /etc/postfix-policyd-spf-python/
# ls policyd-spf.conf
policyd-spf.conf
it seems strange to me. does glob will search for subdir but won't return
its path?
No, it is not strange. To understand that, you need to remember,
absorb and re-remember: the shell is expanding your *spf* above.

Again: the glob expansion is the shell's job.

So if there is something in your current dir (/etc in this case)
with spf somewhere in its name, it will be *replaced* before ls
can even see it.

In your case, that's the directoy "postfix-policyd-spf-python",
so what ls is, at the end, seeing is

ls postfix-policyd-spf-python

which it dutifully does. Just try "echo" instead of "ls" to see
what I mean.

The real fun begins when you have more than one thing matching
the glob: the shell will expand to a list and "ls" will see the
list. Try

ls /etc/*conf*

then

echo /etc/*conf*

Cheers
--
t
Loading...