0

I've searched the site and I've seen similar questions but I couldn't get them to work. I tried to follow: Splitting a string up to a specific location but it didn't work. I working with Korn.

I mainly want the directory of a file passed in. So the file would be:

/home/me/working/from/here/howareyou.txt

So I want to up get:

/home/me/working/from/here/

I've tried this but it didn't work:

THEDIR="${1%//*}"

But it just spit out the whole parameter. Any help would be appreciated. Thanks.

dev4life
  • 103

2 Answers2

3

Use one slash

THEDIR="${1%/*}"

Slower but easier to remember is dirname.

Walter A
  • 736
-1

I like using cut, in association with rev. Basicly, you pipe the full path of your file to rev, to reverse the string name. Then, you pipe it to cut, with a "-" after the field number you want, and then, you reverse it again.

This results in something like that:

echo "$name" | rev | cut -d'/' -f2- | rev

If you don't know it yet, cut split the string around a delimiter char, indicated by -d, and select the field you want, indicated by -f (passing it 2 meaning that you want the second part of the string), adding a minus char meaning that you want it until the end of the string (not using minus would stop the string at the next occurrence of delimiter)

Tloz
  • 121