Renaming bash script's source directory from the script

Multi tool use
I have the following section in my bash script:
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
mv "$pwd" ~/.cfg
fi
The script is a setup script for a git repository, to be run right after cloning it. The idea is to move the entire repository to ~/.cfg
. However, I get an error saying
mv: cannot move '/home/user/config' to '/home/user/.cfg': Permission denied
Permissions are set appropriately and invoking the same mv
from the command line works without problems.
I'm guessing the problem is that I'm renaming a directory, while the script inside it is still running, and simply moving out of it via cd
(as in the above snippet) isn't enough. Is there a way to work around that?
In the end it turned out I could do something simpler than the accepted answer below, though in the same spirit of running the removal at the very end.
I simply changed the mv
command to a cp -r
, then added a rm -rf "$pwd"
at the very end of the script. Apparently rm
's -f
flag ignores the fact that the script is running. The script now works as intended.
linux bash windows-subsystem-for-linux
add a comment |
I have the following section in my bash script:
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
mv "$pwd" ~/.cfg
fi
The script is a setup script for a git repository, to be run right after cloning it. The idea is to move the entire repository to ~/.cfg
. However, I get an error saying
mv: cannot move '/home/user/config' to '/home/user/.cfg': Permission denied
Permissions are set appropriately and invoking the same mv
from the command line works without problems.
I'm guessing the problem is that I'm renaming a directory, while the script inside it is still running, and simply moving out of it via cd
(as in the above snippet) isn't enough. Is there a way to work around that?
In the end it turned out I could do something simpler than the accepted answer below, though in the same spirit of running the removal at the very end.
I simply changed the mv
command to a cp -r
, then added a rm -rf "$pwd"
at the very end of the script. Apparently rm
's -f
flag ignores the fact that the script is running. The script now works as intended.
linux bash windows-subsystem-for-linux
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07
add a comment |
I have the following section in my bash script:
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
mv "$pwd" ~/.cfg
fi
The script is a setup script for a git repository, to be run right after cloning it. The idea is to move the entire repository to ~/.cfg
. However, I get an error saying
mv: cannot move '/home/user/config' to '/home/user/.cfg': Permission denied
Permissions are set appropriately and invoking the same mv
from the command line works without problems.
I'm guessing the problem is that I'm renaming a directory, while the script inside it is still running, and simply moving out of it via cd
(as in the above snippet) isn't enough. Is there a way to work around that?
In the end it turned out I could do something simpler than the accepted answer below, though in the same spirit of running the removal at the very end.
I simply changed the mv
command to a cp -r
, then added a rm -rf "$pwd"
at the very end of the script. Apparently rm
's -f
flag ignores the fact that the script is running. The script now works as intended.
linux bash windows-subsystem-for-linux
I have the following section in my bash script:
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
mv "$pwd" ~/.cfg
fi
The script is a setup script for a git repository, to be run right after cloning it. The idea is to move the entire repository to ~/.cfg
. However, I get an error saying
mv: cannot move '/home/user/config' to '/home/user/.cfg': Permission denied
Permissions are set appropriately and invoking the same mv
from the command line works without problems.
I'm guessing the problem is that I'm renaming a directory, while the script inside it is still running, and simply moving out of it via cd
(as in the above snippet) isn't enough. Is there a way to work around that?
In the end it turned out I could do something simpler than the accepted answer below, though in the same spirit of running the removal at the very end.
I simply changed the mv
command to a cp -r
, then added a rm -rf "$pwd"
at the very end of the script. Apparently rm
's -f
flag ignores the fact that the script is running. The script now works as intended.
linux bash windows-subsystem-for-linux
linux bash windows-subsystem-for-linux
edited Feb 18 at 14:16
Mate de Vita
asked Feb 15 at 16:59
Mate de VitaMate de Vita
184
184
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07
add a comment |
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07
add a comment |
1 Answer
1
active
oldest
votes
I suggest having two scripts: One that moves everything except the first script, and one that just moves the first script and removes the folder.
At the end of the first script, exec
the second script from the new location. That will replace the process and close the handle on the first script. That second script can then move the first script and remove the now-empty original folder.
Something like
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
for i in "$pwd"; do
if [ "$i" != "${BASH_SOURCE[0]}" ]; then mv "$i" ~/.cfg; fi
done
exec ~/.cfg/script2.sh "${BASH_SOURCE[0]}"
fi
Then script 2:
#!/bin/bash
# first arg is path to script you call it from.
base=basename "$1"
dir=dirname "$1"
mv "$1" ~/.cfg/$base
rmdir $dir
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f1406178%2frenaming-bash-scripts-source-directory-from-the-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I suggest having two scripts: One that moves everything except the first script, and one that just moves the first script and removes the folder.
At the end of the first script, exec
the second script from the new location. That will replace the process and close the handle on the first script. That second script can then move the first script and remove the now-empty original folder.
Something like
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
for i in "$pwd"; do
if [ "$i" != "${BASH_SOURCE[0]}" ]; then mv "$i" ~/.cfg; fi
done
exec ~/.cfg/script2.sh "${BASH_SOURCE[0]}"
fi
Then script 2:
#!/bin/bash
# first arg is path to script you call it from.
base=basename "$1"
dir=dirname "$1"
mv "$1" ~/.cfg/$base
rmdir $dir
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
add a comment |
I suggest having two scripts: One that moves everything except the first script, and one that just moves the first script and removes the folder.
At the end of the first script, exec
the second script from the new location. That will replace the process and close the handle on the first script. That second script can then move the first script and remove the now-empty original folder.
Something like
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
for i in "$pwd"; do
if [ "$i" != "${BASH_SOURCE[0]}" ]; then mv "$i" ~/.cfg; fi
done
exec ~/.cfg/script2.sh "${BASH_SOURCE[0]}"
fi
Then script 2:
#!/bin/bash
# first arg is path to script you call it from.
base=basename "$1"
dir=dirname "$1"
mv "$1" ~/.cfg/$base
rmdir $dir
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
add a comment |
I suggest having two scripts: One that moves everything except the first script, and one that just moves the first script and removes the folder.
At the end of the first script, exec
the second script from the new location. That will replace the process and close the handle on the first script. That second script can then move the first script and remove the now-empty original folder.
Something like
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
for i in "$pwd"; do
if [ "$i" != "${BASH_SOURCE[0]}" ]; then mv "$i" ~/.cfg; fi
done
exec ~/.cfg/script2.sh "${BASH_SOURCE[0]}"
fi
Then script 2:
#!/bin/bash
# first arg is path to script you call it from.
base=basename "$1"
dir=dirname "$1"
mv "$1" ~/.cfg/$base
rmdir $dir
I suggest having two scripts: One that moves everything except the first script, and one that just moves the first script and removes the folder.
At the end of the first script, exec
the second script from the new location. That will replace the process and close the handle on the first script. That second script can then move the first script and remove the now-empty original folder.
Something like
# Move to script's source directory (in case it's being called from somewhere else)
cd $(dirname "${BASH_SOURCE[0]}")
# Save script's source directory
pwd=$(pwd)
# If it's not already ~/.cfg
if [ ! "$pwd" -ef ~/.cfg ]; then
echo "Renaming directory"
# Move up one level (since you can't rename directory while in it)
cd ..
# And rename it
for i in "$pwd"; do
if [ "$i" != "${BASH_SOURCE[0]}" ]; then mv "$i" ~/.cfg; fi
done
exec ~/.cfg/script2.sh "${BASH_SOURCE[0]}"
fi
Then script 2:
#!/bin/bash
# first arg is path to script you call it from.
base=basename "$1"
dir=dirname "$1"
mv "$1" ~/.cfg/$base
rmdir $dir
edited Feb 15 at 21:01
answered Feb 15 at 20:54
BlueDrink9BlueDrink9
336111
336111
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
add a comment |
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
Oh I like that, that should work fine.
– Mate de Vita
Feb 16 at 22:21
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1406178%2frenaming-bash-scripts-source-directory-from-the-script%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,zHKYfwcSbbIY
The easiest workaround I can think of is to put the script in ~/bin/ and add that directory to your path. Have you considered that, and if so, why is that not an easy, workable solution?
– Jim L.
Feb 16 at 1:07