How do I test for link to a link? The 2019 Stack Overflow Developer Survey Results Are Indifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?sym link for android-studio/bin/studio.shWhat does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test command( test -n $st ) != ( test -z $st ) right?Trace route of a symbolic linkHow to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?
What is this business jet?
Why doesn't UInt have a toDouble()?
Can there be female White Walkers?
How to support a colleague who finds meetings extremely tiring?
Why are there uneven bright areas in this photo of black hole?
Why didn't the Event Horizon Telescope team mention Sagittarius A*?
What is the most efficient way to store a numeric range?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Is bread bad for ducks?
Geography at the pixel level
Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past
RequirePermission not working
Pokemon Turn Based battle (Python)
Are spiders unable to hurt humans, especially very small spiders?
What is this sharp, curved notch on my knife for?
The difference between dialogue marks
Output the Arecibo Message
Deal with toxic manager when you can't quit
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?
Worn-tile Scrabble
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Loose spokes after only a few rides
Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?
How do I test for link to a link?
The 2019 Stack Overflow Developer Survey Results Are Indifference between non-builtin 'test' and '['unix test when to use eq vs = vs == in test commands?sym link for android-studio/bin/studio.shWhat does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?-n Vs !(exclamation mark) behaves differently with test command( test -n $st ) != ( test -z $st ) right?Trace route of a symbolic linkHow to test list of proxy servers?Bash test: what does “=~” do?How to correctly test file's extension in if statement?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:
ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2
Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):
ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink
But what I need is the full path to the file that hellolink2 points at:
/home/ralph/subdir1/hellolink
Right now I'm doing something like this:
if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then
echo hellolink2 is a link
fi
That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.
Is there an easier way?
test readlink
add a comment |
I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:
ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2
Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):
ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink
But what I need is the full path to the file that hellolink2 points at:
/home/ralph/subdir1/hellolink
Right now I'm doing something like this:
if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then
echo hellolink2 is a link
fi
That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.
Is there an easier way?
test readlink
1
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extradirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, likeC
,perl
,python
, etc ;-)
– mosvy
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday
add a comment |
I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:
ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2
Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):
ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink
But what I need is the full path to the file that hellolink2 points at:
/home/ralph/subdir1/hellolink
Right now I'm doing something like this:
if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then
echo hellolink2 is a link
fi
That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.
Is there an easier way?
test readlink
I want to test whether a file is a link to another link. I tried readlink but it doesn't work the way I need it:
ralph@bash4.4.12,1:~/subdir1 $ ll
lrwxrwxrwx 1 pi pi 13 Apr 10 14:34 hellolink -> subdir2/hello
lrwxrwxrwx 1 pi pi 9 Apr 10 14:34 hellolink2 -> hellolink
drwxr-xr-x 2 pi pi 4096 Apr 10 14:33 subdir2
Using readlink I now get either the canonicalized form of the ultimate target or the naked filename of the next link (hellolink):
ralph@bash4.4.12,1:~/subdir1 $ readlink -f hellolink2
/home/ralph/subdir1/subdir2/hello
ralph@bash4.4.12,1:~/subdir1 $ readlink hellolink2
hellolink
But what I need is the full path to the file that hellolink2 points at:
/home/ralph/subdir1/hellolink
Right now I'm doing something like this:
if [ -h "$(dirname hellolink2)/$(readlink hellolink2)" ] ; then
echo hellolink2 is a link
fi
That looks like a lot of overhead when I do it many times in a loop, using find to feed it the filenames.
Is there an easier way?
test readlink
test readlink
asked yesterday
ArjenArjen
1037
1037
1
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extradirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, likeC
,perl
,python
, etc ;-)
– mosvy
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday
add a comment |
1
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extradirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, likeC
,perl
,python
, etc ;-)
– mosvy
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday
1
1
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra
dirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C
, perl
, python
, etc ;-)– mosvy
yesterday
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra
dirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, like C
, perl
, python
, etc ;-)– mosvy
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday
add a comment |
2 Answers
2
active
oldest
votes
Use test -L (without readlink) to see if a file is a symbolic link.
if [ -L hellolink2 ]
Use realpath to get the absolute path of a symlink to a directory.
$ realpath hellolink2
/home/ralph/subdir1/hellolink
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
add a comment |
For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:
#!/bin/bash
function nextlinked ()
if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"
header=""
add=" "
count=0
find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done
The question still stands: Is there an existing Linux command that does the job of the function nextlinked?
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511600%2fhow-do-i-test-for-link-to-a-link%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use test -L (without readlink) to see if a file is a symbolic link.
if [ -L hellolink2 ]
Use realpath to get the absolute path of a symlink to a directory.
$ realpath hellolink2
/home/ralph/subdir1/hellolink
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
add a comment |
Use test -L (without readlink) to see if a file is a symbolic link.
if [ -L hellolink2 ]
Use realpath to get the absolute path of a symlink to a directory.
$ realpath hellolink2
/home/ralph/subdir1/hellolink
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
add a comment |
Use test -L (without readlink) to see if a file is a symbolic link.
if [ -L hellolink2 ]
Use realpath to get the absolute path of a symlink to a directory.
$ realpath hellolink2
/home/ralph/subdir1/hellolink
Use test -L (without readlink) to see if a file is a symbolic link.
if [ -L hellolink2 ]
Use realpath to get the absolute path of a symlink to a directory.
$ realpath hellolink2
/home/ralph/subdir1/hellolink
answered yesterday
SjoerdSjoerd
34328
34328
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
add a comment |
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
yesterday
add a comment |
For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:
#!/bin/bash
function nextlinked ()
if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"
header=""
add=" "
count=0
find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done
The question still stands: Is there an existing Linux command that does the job of the function nextlinked?
add a comment |
For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:
#!/bin/bash
function nextlinked ()
if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"
header=""
add=" "
count=0
find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done
The question still stands: Is there an existing Linux command that does the job of the function nextlinked?
add a comment |
For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:
#!/bin/bash
function nextlinked ()
if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"
header=""
add=" "
count=0
find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done
The question still stands: Is there an existing Linux command that does the job of the function nextlinked?
For what it's worth... following the suggestions in the comments above I rewrote the code that gets me the file that a link points to, even if that is another link, plus a few lines to test it:
#!/bin/bash
function nextlinked ()
if [ -h "$1" ]; then # we have a link
linked="$(readlink "$1")"
[ "$linked:0:1" == / ] && echo "$linked"
header=""
add=" "
count=0
find / -print0 | while read -rd '' FILENAME ; do
(( count++ ))
if [ -h "$FILENAME" ] ; then # is this a link?
filename="$FILENAME"
printf "%6d " $count ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename" )"
while [ "$filename" ] ; do
header=$header$add
printf "%6d %s" $count "$header" ; ls -ld "$filename" 2>&1
filename="$(nextlinked "$filename")"
done
header=""
fi
done
The question still stands: Is there an existing Linux command that does the job of the function nextlinked?
answered 11 hours ago
ArjenArjen
1037
1037
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511600%2fhow-do-i-test-for-link-to-a-link%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
-readlink, test
1
Your test won't work in the case where the target of the symlink is an absolute path, and you probably can get rid of the extra
dirname
command subst by (conditionally) using some "$var%/*" form. If you really want to make it more light-weight, you'll probably have to use another language, likeC
,perl
,python
, etc ;-)– mosvy
yesterday
Thanks @mosvy, the construct if [ -h "$FILENAME%/*"/"$(readlink "$FILENAME")" ] ; then ... does the job without the use of dirname. But the problem with readlink and the absolute path persists. Isn't there a command that does the job out of the box? Delivering the canonicalized form of the next linked file? It doesn't appear to be too much to ask.
– Arjen
yesterday