How to get top level parent directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get top level parent directory
# 1  
Old 10-15-2012
How to get top level parent directory

Hi All,
I have a directory like this: /u01/app/oracle/11gSE1/11gR203

How do i get the top level directory /u01 from this? Tried dirname and basename but dint help. I can this using echo $ORACLE_HOME | awk -F"/" '{print "/"$2}'. But I am trying to find out if there is a better way of doing it other than using awk. Thanks in advance.
# 2  
Old 10-15-2012
Try:
Code:
echo "${ORACLE_HOME%%/${ORACLE_HOME#*/*/}}"

# 3  
Old 10-15-2012
Thanks a lot Scrutinizer. It worked. Does it work for any number of child directories? Would you mind explaining what exactly is happening here. Regex is new to me. So what are the symbols i need to escape to work in a ruby script. Thanks a lot. If the mod if busy, any one else could help me understand it. Thanks.

Last edited by nilayasundar; 10-15-2012 at 05:22 PM..
# 4  
Old 10-15-2012
Try this in ruby:
Code:
topdir = `echo "${ORACLE_HOME%%/${ORACLE_HOME#*/*/}}"`

# 5  
Old 10-16-2012
Quote:
Originally Posted by nilayasundar
Thanks a lot Scrutinizer. It worked. Does it work for any number of child directories? Would you mind explaining what exactly is happening here. Regex is new to me. So what are the symbols i need to escape to work in a ruby script. Thanks a lot. If the mod if busy, any one else could help me understand it. Thanks.
You're welcome. It should work for any number of subdirs. It is not regex, but Unix pattern matching within parameter expansion, in this case it is two parameter expansions in one...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Top 10 Users in mount level

Hi Members, I'm new to unix. Could you help me in solving my issue. My requirement is I need to pull Top 15 users in every mount. I could able to get the mount level information but I couldn't able to pull the top users in every mount. I see in every mount I could see a lot of nested... (3 Replies)
Discussion started by: UBEE
3 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Catching the xml tag when only parent directory is known ..not the actual directory

Hi folks, I have an query that is let say i have to search in an xml file an tag that is <abcdef> now this xml file is at /opt/usr/local so one fastest way to achieve this is go to this location by cd /opt/usr/local and then do grep like this... grep -i abcdef but for this I must know the... (4 Replies)
Discussion started by: punpun66
4 Replies

4. UNIX for Dummies Questions & Answers

cd to parent directory

Is there anyway i can cd to the parent directory of my current directory without using .. entries? (6 Replies)
Discussion started by: linux17
6 Replies

5. Shell Programming and Scripting

Traverse file structure from top and rename the immediate parent

Hello All, I am trying to write a script to:- 1. Traverse entire file system, look for directories which has .git directory in it 2. Rename the immediate parent directory to <orignal_name.git> 3. Traverse new file structure and look for all directories with <original_name>.git 4. cd to... (2 Replies)
Discussion started by: sahil_jammu
2 Replies

6. UNIX for Dummies Questions & Answers

Top level TCSH while Loop doen't work

Hey guys... I'm learning some shell scripting on OS X using the tcsh shell. For some reason... my while loop isn't executing right (or more likely I am doing something wrong.) Something as simple as this doesn't work: #!/bin/tcsh set g = 0 while ($g <10) echo "this" $g @ g =... (2 Replies)
Discussion started by: sprynmr
2 Replies

7. Shell Programming and Scripting

Help needed removing two top level folders from path

Hi, I am trying to use either awk or sed to drop the first two folders in a path. So if I had path /folder1/folder2/folder3/folder4.... I need to drop folder1&2, so the new path would be /folder3/folder4... If folder1 and folder2 were the same all the time, this would be easy. But... (4 Replies)
Discussion started by: robertinohio
4 Replies

8. Shell Programming and Scripting

How to exclude top level directory with find?

I'm using bash on cygwin/windows. I'm trying to use find and exclude the directory /cygdrive/c/System\ Volume\ Information. When I try to use the command below I get the error "rm: cannot remove `/cygdrive/c/System Volume Information': Is a directory. Can someone tell me what I am doing... (3 Replies)
Discussion started by: siegfried
3 Replies

9. Shell Programming and Scripting

Is there any way to set env variable in top level Makefile and unset when done

Hello I have compilation directory structure the top level Makefile is the one that contains all the sub directories I want to set in this Makefile env variable say : setenv OPTIMIZATION_LEVEL "1" and when all the sub directories done compiling it will set this variable to different lavel... (0 Replies)
Discussion started by: umen
0 Replies

10. OS X (Apple)

Let's get a .mac top level domain! As in www.xyz.mac

ICANN, the Internet Corporation for Assigned Names and Numbers, is inviting proposals for new sponsored top level domains. This may be wishful thinking, but isn't that a chance to push for a .mac top level domain (TLD)? I for one would VASTLY like the idea of having a domain of something.mac --... (0 Replies)
Discussion started by: ropers
0 Replies
Login or Register to Ask a Question