Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: zmv pattern for directory and multiple file types
- X-seq: zsh-users 26174
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Ahmad Ismail <ismail783@xxxxxxxxx>
- Subject: Re: zmv pattern for directory and multiple file types
- Date: Mon, 16 Nov 2020 19:53:51 +0000
- Archived-at: <https://zsh.org/users/26174>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-users/2020-11/20201116195351.679cbaa1%40tarpaulin.shahaf.local2>
- Authentication-results: zsh.org; iprev=pass (wout5-smtp.messagingengine.com) smtp.remote-ip=64.147.123.21; dkim=pass header.d=daniel.shahaf.name header.s=fm1 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm1 header.a=rsa-sha256; dmarc=none header.from=daniel.shahaf.name; arc=none
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:cc:subject:message-id :in-reply-to:references:mime-version:content-type :content-transfer-encoding; s=fm1; bh=kCriY40AGcZanMdSmshAnMQVHl Uqx7oWeRj3Wp0o09A=; b=TAPZUkCP4F+MVTJGAO2m0xKDZe6jUwk0Wfzu8yLcmB 4df4aPM6Xxh12wSHEy1fP70GtugKTzGTliw8o484eEAX845mIoyVr1XKRG5LWPAW egJc5G68Xcs7dj9CS9rmanDHvV++r374OFEkezYh58fdI2v6qNctEnZeHkYTN6C+ ANgpWn413q/fOB6vdBCddJbDBEVpXdc0tnN6I2wA66f567s+rcO7sDxgHbC+On5G HlS6RXNUMyNk4f5pIoxH0jpidV71m3g8WAM+LFJCoUVWUcuo6XgzixfRqrDHIQ8W jsKnNygawANPXqerKbBbTvN8yFfGvtiRLaPgjxr78dMA==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=kCriY40AGcZanMdSmshAnMQVHlUqx7oWeRj3Wp0o0 9A=; b=mbT/ACVEHB1ImItN/n9auX2S6vggOQvY0YMEFtec3eCGsJehtMryIQLKx qztkcDtCLDuyVpGr1W6gvmZ6NJIUOsowUJsNm6O9JS2tYG2GS0/1kwvW3JdVng+Z Xk1LPzKi/2bXnKEVy0ryg/d/cqk2ZtM6c9FwjIKjpzxbyx+mJnh6N1UZmpu5LQYh JP3RZY9/aM3R7zmis0OpJHcOUoFbHjNN5IfuJIgauSO2t+ZfpCEmo2CqL/J5gwb1 K4nMy1+LJTNNzmWkHV9EgSCNYCvqONsWb6qBCVqXB6ojSjYPm//s7TeaympW2Gy2 yuX3GwkGC/K6KRrtMb12kpiMNR06w==
- In-reply-to: <CAHAhJwJVTmCY0ALRDDzcEYCG8Voojp8ZBT2DK8Sb50F-RFAWpg@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-users>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-users.zsh.org>
- List-owner: <mailto:zsh-users-request@zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-users>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-users>
- References: <CAHAhJwJVTmCY0ALRDDzcEYCG8Voojp8ZBT2DK8Sb50F-RFAWpg@mail.gmail.com>
- Sender: zsh-users-request@xxxxxxx
Ahmad Ismail wrote on Sat, 14 Nov 2020 03:45 +0600:
> Hi All,
>
> I have the following functions that rename files from camel to sort of
> kebab case.
>
> function ctokfiles() {
> # Transform [Capital][Capital][Small] to [Capital]-[Capital][Small]
> zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(.adoc|.txt)' '$1${2//(#b)([A-Z])([A-Z][a-z])/$match[1]-$match[2]}$3'
>
> # Put - Between [Small][Capital]
> zmv -Q '(**/)(*[a-z][A-Z]*)(.adoc|.txt)' '$1${2//(#b)([a-z])([A-Z])/$match[1]-$match[2]}$3'
>
> # Change [Capital][Small] to Lovercase
> zmv -Q '(**/)(*[A-Z][a-z]*)(.adoc|.txt)' '$1${2//(#m)[A-Z][a-z]/${(L)MATCH}}$3'
> }
>
> So, IF the input name is "ThisIsMyOCDTalking", it becomes
> "this-is-my-OCD-talking".
>
> Now I want to rename directories, adoc & txt files only. For directories, I
> think I have to use
>
> zmv -Q '(**/)(*[A-Z][A-Z][a-z]*)(/)'
>
> How to make a function that will work on adoc, txt and directories. I tried
> (/|.adoc|.txt), apparently not working.
>
Consider how in «*foo*(bar)», the «foo» are matched against the
filename but the «bar» are parsed as glob qualifiers. You can't just
juxtapose them within the same set of parentheses; they're apples and
oranges.
I don't see a good solution for "Everything in «**/*» that's
a directory or named *.foo or *.bar". I'd consider find(1) for this.
> One more thing, is there any way I can reduce the steps in this functions.
As a rule, I wouldn't recommend reducing steps in functions in the
first place, unless you're coding for sport rather than robustness — in
which case, I'd probably opt for Perl:
% print -rNC1 -- *.(adoc|txt)(N) *(/N) | rename -0 -n '$_ = join "-", map { /[a-z]/ ? lc : $_ } split /(?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z])/'
rename(ThisIsMyOCDTalking.adoc, this-is-my-OCD-talking.adoc)
%
The main algorithmic difference is splitting the filename into an array
on zero-width lookaround matches. I don't think there's a native zsh
equivalent of this.
Note that changing «print -rNC1» to «ls» would introduce two separate bugs.
I'm not sure how to extend this to nested hierarchies, given the
possibility of foo/bar where both foo and bar have to be changed.
Perhaps with «rename -d» and ensuring the input is sorted children
before their parents.
Cheers,
Daniel
Messages sorted by:
Reverse Date,
Date,
Thread,
Author