Remove symlinks originating from specific directory
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have an "install.sh" that installs my personal scripts:
find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +
I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:
for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done
but it removes all symlinks, not just ones where the target is under
"/home/Steven".
shell find symlink rm ln
add a comment |Â
up vote
2
down vote
favorite
I have an "install.sh" that installs my personal scripts:
find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +
I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:
for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done
but it removes all symlinks, not just ones where the target is under
"/home/Steven".
shell find symlink rm ln
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have an "install.sh" that installs my personal scripts:
find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +
I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:
for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done
but it removes all symlinks, not just ones where the target is under
"/home/Steven".
shell find symlink rm ln
I have an "install.sh" that installs my personal scripts:
find /home/Steven -name '*.sh' -exec ln -s -t /usr/local/bin +
I would like to make an "uninstall.sh" that removes the symbolic links created
by "install.sh". I wrote this:
for z in /usr/local/bin/*
do
if [ -h "$z" ]
then rm "$z"
fi
done
but it removes all symlinks, not just ones where the target is under
"/home/Steven".
shell find symlink rm ln
asked Oct 28 '17 at 16:42
Steven Penny
2,30921635
2,30921635
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50
add a comment |Â
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
If you have GNU or BSD find
, this should do it:
find -lname '/home/Steven/*' -delete
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to usefind
to answer the question "does this symbolic link point somewhere under this directory?"
â Tavian Barnes
Oct 28 '17 at 17:36
add a comment |Â
up vote
1
down vote
You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.
add a comment |Â
up vote
1
down vote
To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven
, with zsh
:
rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')
Where:
@
glob qualifier that matches on symlinks:ecode
glob qualifier that matches based on the evaluation of the code (where$REPLY
contains the path of the file to check)$REPLY:A
expands to the absolute path of$REPLY
.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
If you have GNU or BSD find
, this should do it:
find -lname '/home/Steven/*' -delete
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to usefind
to answer the question "does this symbolic link point somewhere under this directory?"
â Tavian Barnes
Oct 28 '17 at 17:36
add a comment |Â
up vote
3
down vote
accepted
If you have GNU or BSD find
, this should do it:
find -lname '/home/Steven/*' -delete
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to usefind
to answer the question "does this symbolic link point somewhere under this directory?"
â Tavian Barnes
Oct 28 '17 at 17:36
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
If you have GNU or BSD find
, this should do it:
find -lname '/home/Steven/*' -delete
If you have GNU or BSD find
, this should do it:
find -lname '/home/Steven/*' -delete
answered Oct 28 '17 at 16:52
Tavian Barnes
267210
267210
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to usefind
to answer the question "does this symbolic link point somewhere under this directory?"
â Tavian Barnes
Oct 28 '17 at 17:36
add a comment |Â
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to usefind
to answer the question "does this symbolic link point somewhere under this directory?"
â Tavian Barnes
Oct 28 '17 at 17:36
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
@TavianBarnes This seems like a better solution. But this won't work if the symbolic links use relative paths, will it?
â igal
Oct 28 '17 at 17:21
2
2
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use
find
to answer the question "does this symbolic link point somewhere under this directory?"â Tavian Barnes
Oct 28 '17 at 17:36
@igal Right. That shouldn't be a problem in this specific case, because the links were created with absolute paths as seen in the question. In general it would be hard to use
find
to answer the question "does this symbolic link point somewhere under this directory?"â Tavian Barnes
Oct 28 '17 at 17:36
add a comment |Â
up vote
1
down vote
You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.
add a comment |Â
up vote
1
down vote
You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.
You could modify your script and use grep
and either readlink
or realpath
to extract the files in the desired subdirectory, e.g.:
for z in /usr/local/bin/*
do
if [ -h "$z" ] && readlink -f "$z" | grep -q '^/home/Steven'
then rm "$z"
fi
done
The readlink -f
command returns the full path to the file pointed to by the symbolic link. The grep -q '^/home/Steven'
command returns true if the path begins with the substring '/home/Steven' and returns false otherwise.
A word of caution: there is some ambiguity related to symbolic links that could affect the outcome here. The readlink -f
command will resolve the link recursively, so the above command would fail if the file in the /home/Steven
directory were itself a symbolic link point outside of this directory. If this isn't the desired behavior then you might want to use the realpath
command instead.
edited Oct 28 '17 at 17:20
answered Oct 28 '17 at 16:55
igal
4,830930
4,830930
add a comment |Â
add a comment |Â
up vote
1
down vote
To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven
, with zsh
:
rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')
Where:
@
glob qualifier that matches on symlinks:ecode
glob qualifier that matches based on the evaluation of the code (where$REPLY
contains the path of the file to check)$REPLY:A
expands to the absolute path of$REPLY
.
add a comment |Â
up vote
1
down vote
To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven
, with zsh
:
rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')
Where:
@
glob qualifier that matches on symlinks:ecode
glob qualifier that matches based on the evaluation of the code (where$REPLY
contains the path of the file to check)$REPLY:A
expands to the absolute path of$REPLY
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven
, with zsh
:
rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')
Where:
@
glob qualifier that matches on symlinks:ecode
glob qualifier that matches based on the evaluation of the code (where$REPLY
contains the path of the file to check)$REPLY:A
expands to the absolute path of$REPLY
.
To cover for the case where the links may be relative and to check that their canonical path is within /home/Steven
, with zsh
:
rm /usr/local/bin/*(@e'[[ $REPLY:A = /home/Steven/* ]]')
Where:
@
glob qualifier that matches on symlinks:ecode
glob qualifier that matches based on the evaluation of the code (where$REPLY
contains the path of the file to check)$REPLY:A
expands to the absolute path of$REPLY
.
answered Oct 28 '17 at 21:24
Stéphane Chazelas
283k53521855
283k53521855
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401093%2fremove-symlinks-originating-from-specific-directory%23new-answer', 'question_page');
);
Post as a guest
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
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
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
@StevePenny It occurred to me that there are a couple of minor possible ambiguities in the question related to symbolic links. The first is whether or not you're using absolute or relative paths when creating the symbolic links. The second is whether or not the targets of the symbolic links are links themselves. I suspect that neither of these will be issues in your specific case (as described in the question body), but it might be something to keep in mind.
â igal
Oct 28 '17 at 17:30
@igal you can see my command - so that answers at least one of the questions
â Steven Penny
Oct 28 '17 at 17:47
@StevePenny That's right. If you're only ever using absolute paths then that's not going to be an issue.
â igal
Oct 28 '17 at 17:50