How to get last directory path in script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to get last directory path in script

Shell Programming and Scripting


Tags
shell bash

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-10-2012
vel4ever vel4ever is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 7 July 2014, 8:51 PM EDT
Location: Chennai, India
Posts: 121
Thanks: 41
Thanked 1 Time in 1 Post
How to get last directory path in script

I have a below directory name


Code:
/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
Sponsored Links
    #2  
Old 07-10-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
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

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

Sponsored Links
    #3  
Old 07-10-2012
vel4ever vel4ever is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 7 July 2014, 8:51 PM EDT
Location: Chennai, India
Posts: 121
Thanks: 41
Thanked 1 Time in 1 Post
This directory name is located in environment variable. I get it from there in my shell script.
    #4  
Old 07-10-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
So try dirname then, and see what it outputs...
Sponsored Links
    #5  
Old 07-10-2012
vel4ever vel4ever is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 7 July 2014, 8:51 PM EDT
Location: Chennai, India
Posts: 121
Thanks: 41
Thanked 1 Time in 1 Post
Below is the error which i got when using dirname


Code:
dirname: missing operand
Try `dirname --help' for more information.

Sponsored Links
    #6  
Old 07-10-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts

Code:
dirname <path>
#so:
#just tested:
aco $ dirname /root/logs/testing/today/
/root/logs/testing

Sponsored Links
    #7  
Old 07-10-2012
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 1 September 2014, 3:59 PM EDT
Location: France
Posts: 318
Thanks: 19
Thanked 81 Times in 76 Posts
Another way:
Code:
~/unix.com$ d='/root/logs/testing/today/'; echo ${d%/*/}
/root/logs/testing

The Following User Says Thank You to tukuyomi For This Useful Post:
vel4ever (07-11-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get the directory name from a path using csh? vdhingra123 Shell Programming and Scripting 5 03-19-2012 08:11 AM
"find . -printf" without prepended "." path? Getting path to current working directory? pstein Shell Programming and Scripting 1 10-02-2011 08:23 AM
Retrieve directory path from full file path through sh royzlife Shell Programming and Scripting 7 09-21-2010 08:28 AM
absolute path for a script ran with relative path rss67 Shell Programming and Scripting 10 04-13-2010 07:48 AM
How to get directory name from its path? ordano UNIX for Dummies Questions & Answers 1 02-12-2009 03:08 AM



All times are GMT -4. The time now is 07:50 PM.