From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: Clint Adams <schizo@debian.org>, zsh-users@sunsite.auc.dk
Subject: Re: more fun with parameter expansion
Date: Fri, 16 Jun 2000 14:00:19 -0700 [thread overview]
Message-ID: <000616140019.ZM4608@candle.brasslantern.com> (raw)
In-Reply-To: <20000616145330.A8031@scowler.net>
On Jun 16, 2:53pm, Clint Adams wrote:
> Subject: more fun with parameter expansion
> This is for someone who wants to take a directory tree and convert all
> the filenames (and directory names) to lowercase, replacing spaces
> with underscores. It presumes that you are using GNU mv and have
> MARK_DIRS set. I'm sure that someone can make it more efficient.
I dunno about efficient, but you can do it with only one glob and one
loop, and it doesn't need to care about MARK_DIRS.
Oh, and your solution missed file names that began with a dot, unless you
meant to say that both MARK_DIRS and GLOB_DOTS were needed ...
for f in **/*(DNon); do
mv -v $f ${${(M)f%/*}:+${${f%/*}:l:gs/ /_/}}${${${(M)f%/*}:-$f}:l:gs/ /_/}
done
The `(on)' (order by name) is probably unnecessary, and won't work in 3.0.x
(though everything else should).
Writing that one-liner out a bit longer might make this more readable:
t=${(M)f%/*} # The tail of the path, including leading slash
h=${t:+${f%/*}} # The head of the path if the tail is non-empty
t=${t:-$f} # The tail is the path when the tail is empty
h=$h:l # Downcase the head, same as ${(L)h}
t=$t:l # Downcase the tail
h=$h:gs/ /_/
t=$h:gs/ /_/
mv $f $h$t
The last five steps could be written as
mv $f ${${:-$h$t}:l:gs/ /_/}
Do you see what that's doing? Exercise: Rewrite the "mv" in my for-loop
with that trick, to make it at least 8 characters shorter.
Incidentally, you can turn on MARK_DIRS for individual globs like this:
echo *(M)
next prev parent reply other threads:[~2000-06-16 21:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-06-16 18:53 Clint Adams
2000-06-16 19:54 ` Clint Adams
2000-06-16 21:00 ` Bart Schaefer [this message]
2000-06-16 21:44 ` Bart Schaefer
2000-06-17 1:17 ` Clint Adams
2000-06-18 22:02 ` Peter Stephenson
2000-06-19 9:08 ` Peter Stephenson
2000-06-19 15:21 ` Clint Adams
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=000616140019.ZM4608@candle.brasslantern.com \
--to=schaefer@candle.brasslantern.com \
--cc=schizo@debian.org \
--cc=zsh-users@sunsite.auc.dk \
/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).