Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Convert a Glob into an Array of Filenames (not paths) in ZSH

Tags:

zsh

I want to expand a glob in zsh into only the filenames, rather than paths, of the matching files. I know that I can do something like this:

paths=(/some/path/blah*blah*blah)
typeset -a filenames
for i ({1..$#paths}); do
  filenames[$i]=`basename $paths[$i]`
done

But I think there must be a better way.

like image 649
Sean Mackesey Avatar asked Apr 05 '14 16:04

Sean Mackesey


1 Answers

There is a two-step process that uses parameter modifiers:

paths=(/some/path/blah*blah*blah)
filenames=($paths[@]:t)

but you can also apply the :t modifier directly to the glob itself:

filenames=( /some/path/blah*blah*blah(:t) )
like image 69
chepner Avatar answered Sep 28 '22 06:09

chepner