# How To Get The String Right After the Directory Name

**URL:** <https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [November 4, 2009, 3:30pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851 "2009-11-04T15:30:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wtolentino](https://community.unix.com/letter_avatar/wtolentino/32/5_5575768a8748004e209b776fc1b2916d.png) [@wtolentino](https://community.unix.com/u/wtolentino)\
**Post date:** [November 4, 2009, 3:30pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851/1 "2009-11-04T15:30:33Z")

</div>

i have this basic line of code that accepts for an input parameter. Example of input string is "/u02/app/ccalloc/dev/out/\*.txt", a directory name with a wild card filename. my code below can get the directory name by using the `dirname $p1` command. when i attempted to use the `basename $p1` to get the filename with wild card character it failed.

```nohighlight
$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt"
Usage: basename String [Suffix]
the directory is /u02/app/ccalloc/dev/out

the file string is

$

```

below is the code

```nohighlight
#!/bin/sh

p1=$1
pdir=`dirname $p1`
pfile=`basename $p1`
echo "the directory is $pdir\n"
echo "the file string is $pfile\n"

```

please help. thanks.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/5_5575768a8748004e209b776fc1b2916d.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [November 4, 2009, 3:47pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851/2 "2009-11-04T15:47:21Z")

</div>

Do you have more than one .txt file in that directory?

By quoting this "/u02/app/ccalloc/dev/out/\*.txt" all text files will become $1 to the script. Basename can only handle one file at a time.

Either unquote the argument so basename can use the first one, or use a loop to process all of the .txt files:

```nohighlight
for FILE in $@; do
  basename "$FILE"
done

```

---

<div class="post-metadata">

**Author:** ![wtolentino](https://community.unix.com/letter_avatar/wtolentino/32/5_5575768a8748004e209b776fc1b2916d.png) [@wtolentino](https://community.unix.com/u/wtolentino)\
**Post date:** [November 4, 2009, 4:13pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851/3 "2009-11-04T16:13:04Z")

</div>

great that works. i need to add a second parameter when i added a second parameter it appears that it is also including the second parameter string.

```nohighlight
$ sh sample.sh "/u02/app/ccalloc/dev/out/*.txt" "/export/home/ccalftdv/data"
EATVDAILY02132008.txt
EATVDAILY03292007.txt
EATVDAILY04082008.txt
EATVDAILY05012008.txt
EATVDAILY05282008.txt
EATVDAILY06052007.txt
EATVDAILY06062007.txt
EATVDAILY06192009.txt
EATVDAILY06222009.txt
EATVDAILY06242009.txt
EATVDAILY07132009.txt
EATVDAILY07142009.txt
EATVDAILY07152009.txt
EATVDAILY07162009.txt
EATVDAILY10212009.txt
PCARDDAILY07102009.txt
citicc_pre_pack.txt
data
$

```

the last display data is coming from the second parameter "/export/home/ccalftdv/data". how do i exclude that? thanks.

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/5_5575768a8748004e209b776fc1b2916d.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [November 4, 2009, 4:19pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851/4 "2009-11-04T16:19:40Z")

</div>

In the for-loop, change $@ to $1.

$2 is "/export/home/ccalftdv/data"

---

<div class="post-metadata">

**Author:** ![wtolentino](https://community.unix.com/letter_avatar/wtolentino/32/5_5575768a8748004e209b776fc1b2916d.png) [@wtolentino](https://community.unix.com/u/wtolentino)\
**Post date:** [November 4, 2009, 4:46pm UTC](https://community.unix.com/t/how-to-get-the-string-right-after-the-directory-name/252851/5 "2009-11-04T16:46:51Z")

</div>

wonderful that works. many thanks.
