# Checking existence of file using file pattern

**URL:** <https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559>\
**Category:** Shell Programming and Scripting\
**Created:** [September 30, 2008, 12:25pm UTC](https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559 "2008-09-30T12:25:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spkandy](https://community.unix.com/letter_avatar/spkandy/32/5_5575768a8748004e209b776fc1b2916d.png) [@spkandy](https://community.unix.com/u/spkandy)\
**Post date:** [September 30, 2008, 12:25pm UTC](https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559/1 "2008-09-30T12:25:35Z")

</div>

Hi Experts:),  
I need to check the existense of file using patterns.How can i do it?  
Ex:  
if my current directory has a number of files of pattern (ins\_\*),  
i need to check the existense of atleast one file.  
pls reply me.

---

<div class="post-metadata">

**Author:** ![ShawnMilo](https://community.unix.com/user_avatar/community.unix.com/shawnmilo/32/3304_2.png) [@ShawnMilo](https://community.unix.com/u/ShawnMilo)\
**Post date:** [September 30, 2008, 2:43pm UTC](https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559/2 "2008-09-30T14:43:18Z")

</div>

This seems to work for me. There may be (and probably is) a better way to do it.

```nohighlight
 if test -n "$(ls ins_*)"; then echo "yep"; else echo "nope"; fi

```

---

<div class="post-metadata">

**Author:** ![otheus](https://community.unix.com/user_avatar/community.unix.com/otheus/32/505_2.png) [@otheus](https://community.unix.com/u/otheus)\
**Post date:** [September 30, 2008, 3:09pm UTC](https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559/3 "2008-09-30T15:09:17Z")

</div>

ShawnMilo's response is pretty good, I think. Just to be sure, you might want to use:

```nohighlight
 if test -n "`/bin/ls -1 ins_* 2>/dev/null`"; then echo "yep"; else echo "nope"; fi

```

Otherwise, you'll get a "file not found" error, or if your output starts with a dash, you'll have problems.

---

<div class="post-metadata">

**Author:** ![spkandy](https://community.unix.com/letter_avatar/spkandy/32/5_5575768a8748004e209b776fc1b2916d.png) [@spkandy](https://community.unix.com/u/spkandy)\
**Post date:** [October 1, 2008, 12:15pm UTC](https://community.unix.com/t/checking-existence-of-file-using-file-pattern/214559/4 "2008-10-01T12:15:43Z")

</div>

🙂  
Thanks a lot ShawnMilo & Otheus...!  
Its working fine.
