# Unix Kornshell q:  Wildcard directory listing limits to first file found

**URL:** <https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059>\
**Category:** Factual Questions\
**Created:** [October 13, 2004, 7:31pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059 "2004-10-13T19:31:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spectre\_of\_Pithecanthropus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/spectre_of_pithecanthropus/32/12343_2.png) [@Spectre\_of\_Pithecanthropus](https://boards.straightdope.com/u/Spectre_of_Pithecanthropus)\
**Post date:** [October 13, 2004, 7:31pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/1 "2004-10-13T19:31:59Z")

</div>

You know how in Unix you can do this:  
\*\*  
/sales4/dpscalc/prod/log :(beast) \>ls -tr LCL\_To\_DIF\__.log \>testlist  
\*\*  
and it will give you a chronologically sorted list of all files matching LCL\_To\_DIF\__.log that are in the current directory. In my particular environment, the resulting file **testlist** looks like this:  
\*\*  
/sales4/dpscalc/prod/log :(beast) \>cat testlist  
LCL\_To\_DIF\_Cust\_Ld.log  
LCL\_To\_DIF\_Dlr\_Ld.log  
LCL\_To\_DIF\_Smrtcrd\_Ld.log  
LCL\_To\_DIF\_Srvcs\_Mstr\_Ld.log  
LCL\_To\_DIF\_Dlr\_Sub\_Ld.log  
LCL\_To\_DIF\_Sub\_Srvcs\_Spl.log  
LCL\_To\_DIF\_Sub\_Hist\_Ld.log  
LCL\_To\_DIF\_SQLLdr\_Sub\_Srvcs.log  
\*\*

But when I try to use this logic in a Kornshell script, and pass the value LCL\_To\_DIF\_\*.log as a paramter, only the first entry in the list gets written to the file, resulting in a file that looks like this:

\*\*  
/sales4/dpscalc/prod/log :(beast) \>cat testlist  
LCL\_To\_DIF\_Cust\_Ld.log  
\*\*

Why is this happening in this context? How can I work around it? All I’m trying to accomplish is to have a convenient way of selecting a group of log files through a wildcarded spec and then page through them on my screen in chronological order.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 13, 2004, 7:41pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/2 "2004-10-13T19:41:09Z")

</div>

What does your script look like? Are you quoting the parameter that you pass? If not, the shell will expand the wildcard before giving it to your script.

---

<div class="post-metadata">

**Author:** ![Spectre\_of\_Pithecanthropus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/spectre_of_pithecanthropus/32/12343_2.png) [@Spectre\_of\_Pithecanthropus](https://boards.straightdope.com/u/Spectre_of_Pithecanthropus)\
**Post date:** [October 13, 2004, 8:20pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/3 "2004-10-13T20:20:43Z")

</div>

Right now I have the following: The part that was supposed to actually display each file is commented out, now that I’ve narrowed down the problem to the way the parameter is being handled. If I use no parameter and hardcode a wildcarded filespec where I now have “$1”, then it works.

\*\*  
Jobname=“showlogs”  
FileName=""

# 

#rm FileList  
ls -tr $1 \>FileList  
cat FileList  
#exec 9\<FileList  
#while read -u9 i  
#do

# echo “File name is: $i”

# echo “Currently processing $i”

# echo “”

# more ${i}

# echo “Status is $?”

#done  
\*\*

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [October 13, 2004, 8:49pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/4 "2004-10-13T20:49:21Z")

</div>

OK, So what does your command line look like when calling the script? Do you put the parameter in quotes, like this?

```auto

yourscript.sh 'LCL_To_DIF_*.log'

```

or like this?

```auto

yourscript.sh LCL_To_DIF_*.log

```

If the latter, then the shell is going to expand LCL\_To\_DIF\_\*.log into a file list and pass _that_ to your script. Then $1 will simply contain the first file in the list, and pass _that_ to **ls**. To prove it, add this to the top of your script:

```auto

echo "Parameter passed is: $1" 

```

Make sure you’re quoting the argument right to prevent interpolation. Usually that’s single-quotes, but I’m not too familiar with Korn so it might be different.

---

<div class="post-metadata">

**Author:** ![Omphaloskeptic](https://avatars.discourse-cdn.com/v4/letter/o/bcef8e/32.png) [@Omphaloskeptic](https://boards.straightdope.com/u/Omphaloskeptic)\
**Post date:** [October 13, 2004, 9:11pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/5 "2004-10-13T21:11:56Z")

</div>

> [@Spectre of Pithecanthropus](#):
>
> Right now I have the following: The part that was supposed to actually display each file is commented out, now that I’ve narrowed down the problem to the way the parameter is being handled. If I use no parameter and hardcode a wildcarded filespec where I now have “$1”, then it works.

Like **friedo** said, the wildcard expansion is done before the arguments are passed in, so 1 just gets the first file in the list. \*\*friedo\*\* gives one way of solving the problem. Another way would be to use the shell variable \* (or $@), which just lists all of the arguments:

```auto

ls -tr "$@" > FileList

```

---

<div class="post-metadata">

**Author:** ![Spectre\_of\_Pithecanthropus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/spectre_of_pithecanthropus/32/12343_2.png) [@Spectre\_of\_Pithecanthropus](https://boards.straightdope.com/u/Spectre_of_Pithecanthropus)\
**Post date:** [October 13, 2004, 9:37pm UTC](https://boards.straightdope.com/t/unix-kornshell-q-wildcard-directory-listing-limits-to-first-file-found/269059/6 "2004-10-13T21:37:28Z")

</div>

Thanks for the help.

Both **friedo’s** and **Omphaloskeptic’s** approaches work equally well, but I’m going with the latter as it permits me to use the invocation syntax that is more habitual to me. I didn’t know that “$@” was a stand-in for the entire parameter list.
