Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Help with paths and file sorting



> 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."



Messages sorted by: Reverse Date, Date, Thread, Author