How do I test for link to a link? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Resultsdifference 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?
Semisimplicity of the category of coherent sheaves?
Did the new image of black hole confirm the general theory of relativity?
How can I define good in a religion that claims no moral authority?
How to pronounce 1ターン?
Format single node in tikzcd
Take groceries in checked luggage
What's the point in a preamp?
ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?
Didn't get enough time to take a Coding Test - what to do now?
Windows 10: How to Lock (not sleep) laptop on lid close?
How to delete random line from file using Unix command?
Did God make two great lights or did He make the great light two?
Was credit for the black hole image misattributed?
How do I add random spotting to the same face in cycles?
Why can't wing-mounted spoilers be used to steepen approaches?
Cooking pasta in a water boiler
He got a vote 80% that of Emmanuel Macron’s
How to test the equality of two Pearson correlation coefficients computed from the same sample?
Can the DM override racial traits?
Can smartphones with the same camera sensor have different image quality?
What can I do if neighbor is blocking my solar panels intentionally?
Road tyres vs "Street" tyres for charity ride on MTB Tandem
Sort a list of pairs representing an acyclic, partial automorphism
I could not break this equation. Please help me
How do I test for link to a link?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Resultsdifference 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
Apr 10 at 9:06
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
Apr 10 at 10:59
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 Apr 10 at 7:07
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
Apr 10 at 9:06
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
Apr 10 at 10:59
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
Apr 10 at 9:06
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
Apr 10 at 10:59
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
Apr 10 at 9:06
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
Apr 10 at 9:06
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
Apr 10 at 10:59
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
Apr 10 at 10:59
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
Apr 10 at 7:52
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 ()
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
Apr 10 at 7:52
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
Apr 10 at 7:52
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 Apr 10 at 7:27
SjoerdSjoerd
34328
34328
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
Apr 10 at 7:52
add a comment |
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
Apr 10 at 7:52
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
Apr 10 at 7:52
realpath for me gives the same result as readlink -f: ralph:~/subdir1 $ realpath hellolink2 /home/ralph/subdir1/subdir2/hello
– Arjen
Apr 10 at 7:52
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 ()
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 ()
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 ()
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 ()
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 2 days 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
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
Apr 10 at 9:06
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
Apr 10 at 10:59