How to get last directory path in script

I have a below directory name

/root/logs/testing/today/

Here i have get to the value as '/root/logs/testing/' only without the last directory in a shell script. Do i need to use substr function here. Is there any other way around

How do you get that path: /root/logs/testing/today ?
I mean I am quite sure you give that path to the command dirname, it will return you
/root/logs/testing

ant:/sm/export $ dirname /sm/export/hpux/
/sm/export

This directory name is located in environment variable. I get it from there in my shell script.

So try dirname then, and see what it outputs...

Below is the error which i got when using dirname

dirname: missing operand
Try `dirname --help' for more information.
dirname <path>
#so:
#just tested:
aco $ dirname /root/logs/testing/today/
/root/logs/testing

Another way:

~/unix.com$ d='/root/logs/testing/today/'; echo ${d%/*/}
/root/logs/testing
1 Like