Mavershang
Mavershang

Reputation: 1278

How to get files do not match a pattern

Here is my script

data_dir="/home/data"
shopt extglob
files=!($data_dir/*08142014*)
echo ${files[@]}

for file in $files[@]
do 
  #blabla
done

/home/data contains multiple files with different date info within file name, thus I should be able to get a list of files that does not contain "08142014".

But kept get syntax error. It seems files is just "!(/home/data/08202014)", while I want a list of file names.

Did I miss anything? Thanks

Upvotes: 1

Views: 53

Answers (2)

dganesh2002
dganesh2002

Reputation: 2210

You can use ->

files=`ls $data_dir | grep -v 08142014`

Upvotes: 1

anubhava
anubhava

Reputation: 785146

You can use:

data_dir="/home/data"
shopt -s extglob
files=($data_dir/!(*08142014*))

for file in "${files[@]}"
do 
  echo "$file"
done
  • To set extglob you need to use shopt -s extglob
  • To set array your syntax isn't right
  • Check how array is correctly iterated

Upvotes: 4

Related Questions