LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2005, 04:15 PM   #1
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Rep: Reputation: 0
Thumbs up How to check if a parent directory is a symbolic link


How can I check if a parent directory is a symbolic link.

Suppose I create a symbolic link like say,

ln -s /abc/xyz abc_xyz

now Suppose I am in another directory say /abc_xyz/tom/install/


how can I check by running a script in the install directory that abc_xyz is a symbolic link. I know by using

if [ -h /abc_xyz ] we can test if abc_xyz is a symbolic link. But by sitting in a subdirectory of this directory, how can we check if the parent is a symbolic link.

Thanks in advance
 
Old 10-05-2005, 05:32 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
How about:
Code:
if [[ -h . ]] ; then something ; fi
That's if all you want to evaluate is the parent directory.

Or:
Code:
while [[ $( cd ..; pwd; ) != '/' ]] ; do cd .. ; done
if [[ -h . ]] ; then something ; fi
The while loop will get you to the subdirectory that is an immediate child of /. Then it will perform the symlink test.

You might want to launch a child shell to perform that test if you're not keen on leaving the current directory.
 
Old 10-05-2005, 07:05 PM   #3
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Original Poster
Rep: Reputation: 0
I want to check for the symbolic link in the middle of a complex shell script. I should not change the current directory. All I have is a variable with the complete path say /home/harry/abc_xyz/tom/install/ . I initially thought of tokenizing the string with "/", but I am not sure how we can tokenize strings in linux scripts.

Anomie, How can I launch a child shell to do that? Thanks for your reply.
 
Old 10-05-2005, 09:09 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Here is one script called symlink-test
Code:
#!/bin/sh

while [[ $( cd ..; pwd; ) != '/' ]]
  do cd .. 
done

if [[ -h . ]] 
  then exit 1
fi

exit 0
And here is your main script:
Code:
#!/bin/sh

doing stuff....
doing stuff....

# Test if top-level parent directory is symlink
sh symlink-test

# Evaluate exit code from symlink-test
if [[ $? -eq 1 ]]
  ISSYMLINK=yes
else
  ISSYMLINK=no
end-if

doing stuff....
doing stuff....

exit 0
Does that answer your question? Even though symlink-test will be cd-ing up to the parent directories, your main script should stay put and never leave its directory.
 
Old 10-06-2005, 08:32 AM   #5
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Original Poster
Rep: Reputation: 0
The problem I am having is suppose I have a symbolic link say

ln -s /abc/xyz abc_xyz

now I have a variable set to say /home/tom/abc_xyz/install/config/

now from the main script if I echo the 'pwd', it shows /abc/xyz/install/config/ and not the whole path. And by using "cd .." I am not able to get to the symbolic link.

Thanks
 
Old 10-06-2005, 10:52 AM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Is there any constant information you know about the directory you want to test as a symlink? You either need to know the name of the directory you want to test, or you need a way to know where it will fall in the folder/subfolder hierarchy.

Without that there isn't a good way to test for it.
 
Old 10-06-2005, 11:58 AM   #7
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Original Poster
Rep: Reputation: 0
I have a varibale say $completePath = /home/tom/abc_xyz/tom/install/. From this I need a way to find out that the dir /home/tom/abc_xys is a symbolic link. I was just wondering if there is a way to parse the string.
 
Old 10-06-2005, 12:25 PM   #8
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I am still not clear what information you know going into this. Do you always know that abc_xyz is the directory you want to test as a symlink?

If so, try
Code:
PATHTOTEST=`echo $COMPLETEPATH | sed 's/abc_xyz.*/abc_xyz/'`
That will truncate everything after the 'abc_xyz' and assign it to PATHTOTEST. Then you can evaluate that.
 
Old 10-06-2005, 05:36 PM   #9
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Original Poster
Rep: Reputation: 0
I see what you are saying. Sorry for not making myself clear before. I dont know where exactly the symbolic link "abc_xyz" will be present in the complete path. So, I need to check each parent directory in the variable.

I wonder if there is a way, to parse the string with "/" delimiter, that would make my job easier.
 
Old 10-06-2005, 05:42 PM   #10
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Well, as long as you know that the directory will always be called abc_xyz, then my previous post should work for you.

After assigning that value to PATHTOTEST, it is a simple test of
Code:
if [[ -h $PATHTOTEST ]]
 
Old 10-06-2005, 05:44 PM   #11
compnovice
LQ Newbie
 
Registered: Dec 2003
Posts: 6

Original Poster
Rep: Reputation: 0
Oops, "abc_xyz" is not a constant, it changes. I just gave an example
 
Old 10-06-2005, 05:59 PM   #12
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
So would you like to check each level of the parent directories along the way? If so, here's a rewrite of symlink-test:
Code:
#!/bin/sh

while [[ $( cd ..; pwd; ) != '/' ]]
  do cd .. 

  if [[ -h . ]] 
    then exit 1
  fi
done

exit 0
Now it will check each directory as it goes up. And if it hits a symlink it will exit with a code of 1.

I am sure you could write something to parse $COMPLETEPATH using cut or awk as well. Lots of ways to accomplish the same task in *nix.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot remove symbolic link to directory. martint Linux - Newbie 6 09-17-2014 08:45 PM
'cp -rv .*' recurses into the parent directory??? stefanlasiewski SUSE / openSUSE 5 02-02-2005 05:36 PM
directory symbolic link resolution rusti999 Linux - Software 6 08-09-2004 04:13 PM
List only the parent directory scottrell Linux - General 6 11-07-2003 09:10 AM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 07:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration