How to check if a parent directory is a symbolic link
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
#!/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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.