zsh-users
 help / color / mirror / code / Atom feed
From: J <jean.chalard@gmail.com>
To: Tyler Spivey <tspivey@pcdesk.net>
Cc: zsh-users@sunsite.dk
Subject: Re: Help with paths and file sorting
Date: Fri, 22 Jul 2005 11:16:42 +0200	[thread overview]
Message-ID: <fb6be96e05072202162d4f854d@mail.gmail.com> (raw)
In-Reply-To: <20050721222024.GA14060@fast.vc.shawcable.net>

> 1. is there an easy way to take a filespec like:
> /home/tyler/dir1/*
> and go back a directory? so the spec is /home/tyler/*?

What about
${${var%/*}%/*}/${var##*/} ?
(assuming the filespec is in the variable var)

This take the pattern, chops two path elements off the end, and then
concatenates again the last path element of the pattern.


> 2. is there a way of, once having an array of files obtained by the * globbing operator,
> to sort them by size, time, etc?

Sure.
Suppose you have an array "files" that contain the names of the files
you want to sort.
You can just go along the lines of
tmp=\(\(${(pj:|:)files}\)\)
files=(${~tmp}(<put here any sort qualifiers you want>))

The first line constructs a glob pattern that matches all the file
names that are in the original array ; note the double parentheses
around it are there to force glob evaluation even when the array
contains only one file.
The second lines evaluates the glob pattern the first just
constructed, so you can append any globbing qualifiers you want. The
sort qualifiers are described in the 'globbing qualifiers' of the zsh
manual.

That being said, only use this kind of construct if you strictly want
to keep exactly the same file names in your array. For a file manager,
I'd imagine that you want to keep up-to-date with the files in the
directory, so it may be a simpler and wiser choice to simply re-glob
the files with the sort qualifiers you want, and it's not going to be
any slower anyway.


But there is a caveat :
If one of the filenames have a character in it that can be interpreted
as a pattern for globbing, it will, and that's not what you want. A
solution to that is to quote the ${files} array with a (q) flag, but
then spaces in filenames (or any other special char for that) would be
quoted too in the ${tmp} array, meaning they won't match themselves
because these quotes are not going to be removed by the ${~tmp}
construct :/
So the best I can come up with is to quote every of the glob patterns
manually, like
tmp=\(\(${(pj:|:)${${${${${files//\*/\\\*}//\(/\\\(}//\|/\\\|}//\</\\\<}//\[/\\\?}}\)\)
...but that's awfully ugly. Maybe someone knows of a better solution ?

-- 
J
"If you wish to leave a record of your call,
 please state your messij at the sound of the tone."


  reply	other threads:[~2005-07-22  9:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-21 22:20 Tyler Spivey
2005-07-22  9:16 ` J [this message]
2005-07-22  9:44   ` Peter Stephenson

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=fb6be96e05072202162d4f854d@mail.gmail.com \
    --to=jean.chalard@gmail.com \
    --cc=tspivey@pcdesk.net \
    --cc=zsh-users@sunsite.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).