# Trying to run a command when a file matching a regex does not exist.

**URL:** <https://forum.ansible.com/t/trying-to-run-a-command-when-a-file-matching-a-regex-does-not-exist/14968>\
**Category:** Ansible Project\
**Created:** [April 8, 2014, 9:56am UTC](https://forum.ansible.com/t/trying-to-run-a-command-when-a-file-matching-a-regex-does-not-exist/14968 "2014-04-08T09:56:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Kelly](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@Michael\_Kelly](https://forum.ansible.com/u/Michael_Kelly)\
**Post date:** [April 8, 2014, 9:56am UTC](https://forum.ansible.com/t/trying-to-run-a-command-when-a-file-matching-a-regex-does-not-exist/14968/1 "2014-04-08T09:56:42Z")

</div>

Hi,

I’m trying to run a command when a file matching a regex does not exist.

My use case is

I am unzipping an artifact - ec2-api-tools.zip  
This will unzip to a directory that includes the version number - e.g. ec2-api-tools-1.6.13.0  
I want to check if there is a directory that matches the regex ‘ec2-api-tools-\*’ and only run the unzip command if it doesn’t exist.  
I don’t want to tie the script to a particular version as it will change over time.

This is the approach I took -

- name: download ec2 command line tools  
command: chdir=~/tools wget [http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip](http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip) creates=ec2-api-tools.zip

- name: Register if ec2 directory exists  
command: chdir=~/tools test -d ec2-api-tools-\*  
ignore\_errors: True  
register: ec2\_tools\_installed

- name: unzip ec2-tools  
command: chdir=~/tools unzip ec2-api-tools.zip  
when: ec2\_tools\_installed.rc == 1

The problem is the 'test -d ec2-api-tools-_’ always fails. The command passes fine if I run it from the machine itself. The script will work if I change the ec2-api-tools-_ to match the exact version number.

So what I’m wondering is -

Why does the \* in the ‘test -d ec2-api-tools-\*’ command not work correctly?  
Is there a better way of accomplishing this that I’m just not seeing? I imagine it’s a common enough use case.

Thanks in advance,

Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 9, 2014, 1:54pm UTC](https://forum.ansible.com/t/trying-to-run-a-command-when-a-file-matching-a-regex-does-not-exist/14968/2 "2014-04-09T13:54:04Z")

</div>

Change “command:” to “shell:” if you want to use shell features like wildcards, and this should work fine.

Thanks!
