Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to get last directory path in script

Shell Programming and Scripting


Tags
shell bash

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 07-10-2012
vbe's Unix or Linux Image
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 1 July 2015, 7:31 AM EDT
Location: Switzerland - GE
Posts: 6,062
Thanks: 193
Thanked 459 Times in 428 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 Unix and Linux 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 Unix and Linux 07-10-2012
vbe's Unix or Linux Image
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 1 July 2015, 7:31 AM EDT
Location: Switzerland - GE
Posts: 6,062
Thanks: 193
Thanked 459 Times in 428 Posts
So try dirname then, and see what it outputs...
Sponsored Links
    #5  
Old Unix and Linux 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 Unix and Linux 07-10-2012
vbe's Unix or Linux Image
vbe vbe is online now Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 1 July 2015, 7:31 AM EDT
Location: Switzerland - GE
Posts: 6,062
Thanks: 193
Thanked 459 Times in 428 Posts

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

Sponsored Links
    #7  
Old Unix and Linux 07-10-2012
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 29 June 2015, 3:46 PM EDT
Location: France
Posts: 319
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 08:01 AM.