Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: make an array of all directory names in a directory that DON'T have a certain pattern in the name

Tags:

bash

I have a directory that contains a bunch of .zip files as well as their unpacked version. I need to get a list of all the directory's and ignore the .zip files. How can I do this?

I am thinking of using grep and ls, but am not sure how to put it together.

like image 511
farid99 Avatar asked Jul 07 '15 19:07

farid99


2 Answers

Get a list of all sub-directories and store it into an array:

shopt -s nullglob
dirs=( */ )
like image 144
anubhava Avatar answered Sep 18 '22 11:09

anubhava


If you can turn on extglob like so:

shopt -s extglob
declare -a files=( !(*.zip) )

See more about bash pattern matching on the Pattern Matching man page.

like image 21
Jeff Bowman Avatar answered Sep 18 '22 11:09

Jeff Bowman