• glob path question

    From Corey H@21:1/5 to All on Wed Aug 21 14:10:01 2024
    Hi

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

    root@cloud:~# cd /etc/

    root@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael =?utf-8?B?S2rDtnJsaW5n?=@21:1/5 to All on Wed Aug 21 14:10:02 2024
    On 21 Aug 2024 14:00 +0200, from coreyh@free.fr (Corey H):
    I use this command trying to find a file in /etc whose name contains "spf",

    root@cloud:~# cd /etc/

    root@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?

    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?”

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From tomas@tuxteam.de@21:1/5 to Corey H on Wed Aug 21 14:20:01 2024
    On Wed, Aug 21, 2024 at 02:00:02PM +0200, Corey H wrote:
    Hi

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

    root@cloud:~# cd /etc/

    root@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?

    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

    -----BEGIN PGP SIGNATURE-----

    iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCZsXZdwAKCRAFyCz1etHa RhNKAJ4lKKaMCACYecFRwWv4mD1cr7+NUwCfURyfKwMkFUsPjPZq1XQwTPtxCIw=
    =/YQT
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From tomas@tuxteam.de@21:1/5 to Cindy Sue Causey on Wed Aug 21 16:20:01 2024
    On Wed, Aug 21, 2024 at 09:44:34AM -0400, Cindy Sue Causey wrote:

    [...]

    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:

    [...]

    Glad you enjoyed it :-)

    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

    -----BEGIN PGP SIGNATURE-----

    iF0EABECAB0WIQRp53liolZD6iXhAoIFyCz1etHaRgUCZsX1TwAKCRAFyCz1etHa RqUnAJ0Yh3Wcpx0Gbvtmeln35aAZEGthyQCePQppc51b2dniFdmvfaydt5NhNM4=
    =a+ZE
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)