Skip to content

Instantly share code, notes, and snippets.

@archenemies
Created November 11, 2015 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archenemies/7b51ef6fb061b0738df1 to your computer and use it in GitHub Desktop.
Save archenemies/7b51ef6fb061b0738df1 to your computer and use it in GitHub Desktop.
# list files grouped by extension, and sorted in reverse chronological
# order, mostly
function dc () {
local dir=${1:-.}
local FIRST=1;
local l f files exts extfiles extorder;
(cd $dir
IFS="
"
files=(*~(*~)(N))
files=(${files:#\#*\#})
files=($(ls -tcd1 $files))
# list a group of files, printing a --- separator after the
# first call. restrict to contents of $files, and remove our
# output from $files afterwards
_lsgroup () {
# zsh array expansion is much faster than grep -Ff
# llua@#zsh says: use l=( "${(@)l:*files}" ) (but this only
# makes a difference if there is an empty string in one of
# the arrays)
# filter $l through $files
l=(${l:*files})
# remove elements of $l from $files
files=(${files:|l})
if [[ "$#l" > 0 ]]; then
{ [[ $FIRST -eq 0 ]] &&
{perl -le "print \"-\"x$COLUMNS"} || FIRST=0 }
ls --color=always -rtcd $l
fi
}
# make a list of extensions, in order of most recently appearing
# first. TODO: remove extensions with odd characters, which may
# not actually be extensions
extfiles=(${(M)files:#*.*})
dirs=(*(-/N)) # remove directories from extension list
extfiles=(${extfiles:|dirs})
extorder=()
while [[ $#extfiles -gt 0 ]]; do
f=$extfiles[1];
ext=${f##*.}
extorder+=$ext
extfiles=(${extfiles:#*.$ext})
done
# now display files with each extension in our list
for ext in ${(Oa)extorder}; do
l=(*.$ext(N-^/)) # exclude directories
_lsgroup
done
# files with no extension, not matching next two groups
rest=(*(-*N) *(-/N))
l=(${files:|rest})
_lsgroup
# executables (- means dereference symlinks)
l=(*(-*N))
_lsgroup
# directories
l=(*(-/N));
_lsgroup
)
}
@archenemies
Copy link
Author

Produces output like this:

Makefile.in  config.h.in
------------------------------------------------------------------------
autogen.sh
------------------------------------------------------------------------
README.FAQ
------------------------------------------------------------------------
config.sub
------------------------------------------------------------------------
aclocal.m4
------------------------------------------------------------------------
INSTALL  Changes  COPYING  MANIFEST
------------------------------------------------------------------------
install-sh  configure
------------------------------------------------------------------------
libev  doc  src  libptytty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment