Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-05-2013, 08:57 PM   #1
LQ Newbie
Registered: Feb 2013
Posts: 4

Rep: Reputation: Disabled
How to make a shell script do not resolve a symbolic link

In my shell script I want to do a ls on the parent directory to a symlink in the path

ls -1 -tr $my_dir/../../../

the second directory above $my_dir is a symbolic link, and when a .. is done, it goes to the parent directory of the path which the symbolic link is referring to.

I do not want to go to the referred path, but to the actual parent of the symlink. How to achieve this. Please advice.
Old 02-05-2013, 11:56 PM   #2
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Do you know how to do this at the command line? What have you googled and started with? We like to see some effort on any OP's part and then offer assistance.
Old 02-06-2013, 12:08 AM   #3
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
One workaround is to first unlink the file at beginning and link it again at the end, like:-
unlink <linkname>  ## Removing link
ln -s <linkname> </path/to/parentdirectory>   ## Adding link again
Old 02-06-2013, 01:35 AM   #4
LQ Newbie
Registered: Feb 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
cd $my_dir/,,/,,/ to ignore symbolic link and go to the symbolic link parent directory

I am struggling with the same question, I did not get a way to ignore resolving the symbolic link in a relative path and go to the actual parent directory of the symbolic link.

I have code ready by parsing the string using SED, but I felt, there will be an option to get through this directly using cd /ls . Please advice.
Old 02-06-2013, 02:49 AM   #5
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In bash you can use
set +P
to force the shell to not follow symbolic links using shell built-in like cd. It doesn't affect the behaviour of ls, which is an external command, anyway. A workaround might be:
(cd $my_dir/../../../ && ls)
Since it runs in a subshell, it doesn't actually change the working directory of the parent shell. Moreover, if you simply want to retrieve the path of the directory and use it in other commands:
my_path=$(cd $my_dir/../../../ && pwd)
find $my_path -name something
ls -lrt $my_path
echo "The path I'm looking for is $my_path"
Hope this helps.

Last edited by colucix; 02-06-2013 at 02:55 AM. Reason: Changed variable name to avoid redundancy with the OP example.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Can I make a symbolic link that is dynamic link->$HOME/file? croog Linux - General 4 10-14-2012 07:55 PM
Unable to resolve new symbolic link added to /bin directory without reboot buddhahat *BSD 2 04-21-2007 03:01 PM
cannot make symbolic link dave`2005 Debian 3 10-08-2005 10:15 AM
vsftpd - Cannot resolve symbolic link in browser chpchai Linux - Networking 13 05-26-2004 11:07 PM
need help with symbolic link to shell script scottsteibel Linux - Software 8 08-02-2003 11:11 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

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