If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Finding part of the current directory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-03-09, 12:38
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
Finding part of the current directory

I am on a Sun Solaris 5.10

Suppose say my pwd command gives me :
/opt/local/sys/data/01/backups

From the above information, would it be possible by any chance to execute a command that can return me just the the directory name(not the whole path) that is {current directory minus one level}

In other words, I am looking for a command that can return me
01

If my present directory was /opt/local/sys/data/20/ksh , gives me
20
Reply With Quote
  #2 (permalink)  
Old 11-03-09, 12:49
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,452
Code:
pwd | perl -n -e 'split /\//;print $_[@_-2];'
Reply With Quote
  #3 (permalink)  
Old 11-03-09, 13:09
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
Prompt in the same line

Thanks Nick. I tried it out and this is what happens

server01-(/opt/local/sys/data/01/backups)-> pwd | perl -n -e 'split /\//;print $_[@_-2];'

Gives me

01server01-(/opt/local/sys/data/01/backups)->

In other words, I do get 01 but the unix prompt follows right after it and does not get to the new line. Can I make it look like

01
server01-(/opt/local/sys/data/01/backups)->
Reply With Quote
  #4 (permalink)  
Old 11-03-09, 13:14
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,452
Well, how about adding a newline to the print statement? The newline looks like "\n".
Reply With Quote
  #5 (permalink)  
Old 11-03-09, 13:48
scooby_at_work scooby_at_work is offline
Registered User
 
Join Date: Sep 2009
Posts: 44
If you're going to invoke perl, you may as well do it all in Perl:

Code:
perl -MCwd=cwd -e 'print((split(m:/:, cwd))[-2]."\n")'
A somewhat more readable version:

Code:
perl -MCwd=cwd -e '@a = split(m:/:, cwd); print "$a[-2]\n"'
Otherwise, under bash, you could use something like:

Code:
basename `dirname "$PWD"`
Reply With Quote
  #6 (permalink)  
Old 11-03-09, 14:14
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
Thank you. All of them worked just fine in giving me

01

Even though I am using korn as my default shell and never switch over to anything at any point, the following worked just fine

basename `dirname "$PWD"`

Also keeping in fact, me being a big zero in perl, I would rather play safe and use the basename command.
Reply With Quote
  #7 (permalink)  
Old 11-04-09, 00:46
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
echo $(cd ..; echo ${PWD##*/})
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On