Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of all files in a directory, except one

Tags:

arrays

bash

Trying to figure out how to include all .txt files except one called manifest.txt.

FILES=(path/to/*.txt) 
like image 637
Richi D Avatar asked Oct 23 '25 15:10

Richi D


1 Answers

You can use extended glob patterns for this:

shopt -s extglob
files=(path/to/!(manifest).txt)

The !(pattern-list) pattern matches "anything except one of the given patterns".

Note that this exactly excludes manifest.txt and nothing else; mmanifest.txt, for example, would still go in to the array.


As a side note: a glob that matches nothing at all expands to itself (see the manual and this question). This behaviour can be changed using the nullglob (expand to empty string) and failglob (print error message) shell options.

like image 60
Benjamin W. Avatar answered Oct 25 '25 04:10

Benjamin W.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!