LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to make a shell script do not resolve a symbolic link (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-a-shell-script-do-not-resolve-a-symbolic-link-4175448785/)

mathewhai 02-05-2013 08:57 PM

How to make a shell script do not resolve a symbolic link
 
Hi,
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.

cbtshare 02-05-2013 11:56 PM

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.

shivaa 02-06-2013 12:08 AM

One workaround is to first unlink the file at beginning and link it again at the end, like:-
Code:

#!/bin/bash
unlink <linkname>  ## Removing link
.....
script
....
ln -s <linkname> </path/to/parentdirectory>  ## Adding link again


mathewhai 02-06-2013 01:35 AM

cd $my_dir/,,/,,/ to ignore symbolic link and go to the symbolic link parent directory
 
Still,
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.

colucix 02-06-2013 02:49 AM

In bash you can use
Code:

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:
Code:

(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:
Code:

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.


All times are GMT -5. The time now is 06:47 AM.