zsh-users
 help / color / mirror / code / Atom feed
From: "Jesper Nygårds" <jesper.nygards@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Some problems with recursive globbing
Date: Thu, 7 May 2015 16:35:57 +0200	[thread overview]
Message-ID: <CABZhJg-f_PDTtaJf8Fg6qVZQ2wLmAXG-tLXLub7Sq=GW9vXwDA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1853 bytes --]

I am trying to write a function where I collect files within a file tree to
do some processing.

My goal is to find all files which are not in any directory called
'target', at any level.

Here's a simplified version of my function:

myfiles() {
    setopt LOCAL_OPTIONS EXTENDED_GLOB

    local dir

    [[ -n $1 ]] && dir=${1:a}/

    local filepattern="${dir}**/*~${dir}(**/|)target(/**/*|/*|)"

    print -c ${~filepattern}
}

So this prints out the relative paths of all files in the current
directory. If a directory is given as an argument, the absolute paths are
printed of files within the given directory. (I know there's no check that
the argument is actually a directory).

This works, sort of, but I have a question and a problem.

Question: I found it surprisingly difficult to to find a glob pattern that
excluded target directories and their contents at all levels. Have I
complicated this too much, is there an easier way to express this glob?

Problem: I can't get this to work for both cases of a) directories with
spaces in their names and b) directories with parenthesis in their names

Suppose I'm standing in a directory called "/tmp/test(1)", and this
directory contains a directory called "src".

% myfiles src

I get this:

myfiles:9: no matches found:
/tmp/test(1)/src/**/*~/tmp/test(1)/src/(**/|)target(/**/*|/*|)

If I change the fifth line in myfiles() to this:

  [[ -n $1 ]] && dir=${(q)1:a}/

it works for the root directory with parentheses. However, standing in a
directory called "/tmp/test 1", I now get this:

myfiles:9: no matches found: /tmp/test\ 1/src/**/*~/tmp/test\
1/src/(**/|)target(/**/*|/*|)

So with quoting, it works with parentheses. Without quoting, it works with
spaces. It feels as though I've tried everything, but I can't find a way to
quote this so that it works for both parentheses and spaces.

             reply	other threads:[~2015-05-07 14:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-07 14:35 Jesper Nygårds [this message]
2015-05-07 15:52 ` Peter Stephenson
2015-05-07 15:59   ` Peter Stephenson
2015-05-07 16:46     ` Jesper Nygårds
2015-05-07 17:00       ` Peter Stephenson
2015-05-07 17:21         ` Jesper Nygårds
2015-05-08  3:37           ` Jesper Nygårds
2015-05-08  8:40             ` Peter Stephenson
2015-05-08 10:34               ` Jesper Nygårds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABZhJg-f_PDTtaJf8Fg6qVZQ2wLmAXG-tLXLub7Sq=GW9vXwDA@mail.gmail.com' \
    --to=jesper.nygards@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).