Substitute ./ and ../ directories by actual names
I have a script which finds files in the directory specified by user.
#!/bin/bash
# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi
find $DIR -type f -exec basename {} ;
fi
if I input
$ ./script_name.sh .
script gives me correct substitution ./ to $PWD and shows (for example)
$ Find files in the directory /root/scripts
But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input
$ ./script_name.sh ..
script gives me the output
$ Find files in the directory ..
Does anybody know how to substitute ../ to the actual name of the directory?
shell-script
add a comment |
I have a script which finds files in the directory specified by user.
#!/bin/bash
# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi
find $DIR -type f -exec basename {} ;
fi
if I input
$ ./script_name.sh .
script gives me correct substitution ./ to $PWD and shows (for example)
$ Find files in the directory /root/scripts
But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input
$ ./script_name.sh ..
script gives me the output
$ Find files in the directory ..
Does anybody know how to substitute ../ to the actual name of the directory?
shell-script
Try therealpath
command if it is available.
– Bodo
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago
add a comment |
I have a script which finds files in the directory specified by user.
#!/bin/bash
# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi
find $DIR -type f -exec basename {} ;
fi
if I input
$ ./script_name.sh .
script gives me correct substitution ./ to $PWD and shows (for example)
$ Find files in the directory /root/scripts
But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input
$ ./script_name.sh ..
script gives me the output
$ Find files in the directory ..
Does anybody know how to substitute ../ to the actual name of the directory?
shell-script
I have a script which finds files in the directory specified by user.
#!/bin/bash
# make sure about the correct input
if [ -z $1 ]
then
echo "Usage: ./script_name.sh path/to/directory"
else
DIR=$1
if [ $DIR = '.' ]
then
echo "Find files in the directory $PWD"
else
echo "Find files in the directory $DIR"
fi
find $DIR -type f -exec basename {} ;
fi
if I input
$ ./script_name.sh .
script gives me correct substitution ./ to $PWD and shows (for example)
$ Find files in the directory /root/scripts
But I can't make a decision how to substitute ../ to the name of the directory immediately above in the hierarchy. If I input
$ ./script_name.sh ..
script gives me the output
$ Find files in the directory ..
Does anybody know how to substitute ../ to the actual name of the directory?
shell-script
shell-script
edited 14 hours ago
Alex Kuchin
asked 15 hours ago
Alex KuchinAlex Kuchin
205
205
Try therealpath
command if it is available.
– Bodo
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago
add a comment |
Try therealpath
command if it is available.
– Bodo
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago
Try the
realpath
command if it is available.– Bodo
15 hours ago
Try the
realpath
command if it is available.– Bodo
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago
add a comment |
3 Answers
3
active
oldest
votes
GNU coreutils has the realpath
command that does just that.
/tmp/a$ realpath ..
/tmp
Though note that if the path contains symlinks, it will also resolve those:
/tmp/b/c$ realpath ..
/tmp/x/y
(Here, /tmp/b
was a symlink to /tmp/x/y/
)
This may be different from what the shell does with cd ..
. E.g. cd ../..
from /tmp/b/c
in Bash shows the new path as /tmp/
, not as /tmp/x
.
readlink
would be another alternative.
– twalberg
13 hours ago
@twalbergreadlink
only resolves symlinks and not entries like.
or..
, which were the point of the question.readlink .
results in an error (returns status code 1).
– JoL
11 hours ago
2
@JoL Tryreadlink -f .
– twalberg
11 hours ago
add a comment |
A few ideas:
parent="$(dirname "$(pwd)")"
parent="$(
cd ..
pwd
)"
add a comment |
You could first cd
to ..
and then use the $PWD
.
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%2f501602%2fsubstitute-and-directories-by-actual-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
GNU coreutils has the realpath
command that does just that.
/tmp/a$ realpath ..
/tmp
Though note that if the path contains symlinks, it will also resolve those:
/tmp/b/c$ realpath ..
/tmp/x/y
(Here, /tmp/b
was a symlink to /tmp/x/y/
)
This may be different from what the shell does with cd ..
. E.g. cd ../..
from /tmp/b/c
in Bash shows the new path as /tmp/
, not as /tmp/x
.
readlink
would be another alternative.
– twalberg
13 hours ago
@twalbergreadlink
only resolves symlinks and not entries like.
or..
, which were the point of the question.readlink .
results in an error (returns status code 1).
– JoL
11 hours ago
2
@JoL Tryreadlink -f .
– twalberg
11 hours ago
add a comment |
GNU coreutils has the realpath
command that does just that.
/tmp/a$ realpath ..
/tmp
Though note that if the path contains symlinks, it will also resolve those:
/tmp/b/c$ realpath ..
/tmp/x/y
(Here, /tmp/b
was a symlink to /tmp/x/y/
)
This may be different from what the shell does with cd ..
. E.g. cd ../..
from /tmp/b/c
in Bash shows the new path as /tmp/
, not as /tmp/x
.
readlink
would be another alternative.
– twalberg
13 hours ago
@twalbergreadlink
only resolves symlinks and not entries like.
or..
, which were the point of the question.readlink .
results in an error (returns status code 1).
– JoL
11 hours ago
2
@JoL Tryreadlink -f .
– twalberg
11 hours ago
add a comment |
GNU coreutils has the realpath
command that does just that.
/tmp/a$ realpath ..
/tmp
Though note that if the path contains symlinks, it will also resolve those:
/tmp/b/c$ realpath ..
/tmp/x/y
(Here, /tmp/b
was a symlink to /tmp/x/y/
)
This may be different from what the shell does with cd ..
. E.g. cd ../..
from /tmp/b/c
in Bash shows the new path as /tmp/
, not as /tmp/x
.
GNU coreutils has the realpath
command that does just that.
/tmp/a$ realpath ..
/tmp
Though note that if the path contains symlinks, it will also resolve those:
/tmp/b/c$ realpath ..
/tmp/x/y
(Here, /tmp/b
was a symlink to /tmp/x/y/
)
This may be different from what the shell does with cd ..
. E.g. cd ../..
from /tmp/b/c
in Bash shows the new path as /tmp/
, not as /tmp/x
.
edited 15 hours ago
answered 15 hours ago
ilkkachuilkkachu
59.4k894168
59.4k894168
readlink
would be another alternative.
– twalberg
13 hours ago
@twalbergreadlink
only resolves symlinks and not entries like.
or..
, which were the point of the question.readlink .
results in an error (returns status code 1).
– JoL
11 hours ago
2
@JoL Tryreadlink -f .
– twalberg
11 hours ago
add a comment |
readlink
would be another alternative.
– twalberg
13 hours ago
@twalbergreadlink
only resolves symlinks and not entries like.
or..
, which were the point of the question.readlink .
results in an error (returns status code 1).
– JoL
11 hours ago
2
@JoL Tryreadlink -f .
– twalberg
11 hours ago
readlink
would be another alternative.– twalberg
13 hours ago
readlink
would be another alternative.– twalberg
13 hours ago
@twalberg
readlink
only resolves symlinks and not entries like .
or ..
, which were the point of the question. readlink .
results in an error (returns status code 1).– JoL
11 hours ago
@twalberg
readlink
only resolves symlinks and not entries like .
or ..
, which were the point of the question. readlink .
results in an error (returns status code 1).– JoL
11 hours ago
2
2
@JoL Try
readlink -f .
– twalberg
11 hours ago
@JoL Try
readlink -f .
– twalberg
11 hours ago
add a comment |
A few ideas:
parent="$(dirname "$(pwd)")"
parent="$(
cd ..
pwd
)"
add a comment |
A few ideas:
parent="$(dirname "$(pwd)")"
parent="$(
cd ..
pwd
)"
add a comment |
A few ideas:
parent="$(dirname "$(pwd)")"
parent="$(
cd ..
pwd
)"
A few ideas:
parent="$(dirname "$(pwd)")"
parent="$(
cd ..
pwd
)"
answered 15 hours ago
ctrl-alt-delorctrl-alt-delor
11.6k42159
11.6k42159
add a comment |
add a comment |
You could first cd
to ..
and then use the $PWD
.
add a comment |
You could first cd
to ..
and then use the $PWD
.
add a comment |
You could first cd
to ..
and then use the $PWD
.
You could first cd
to ..
and then use the $PWD
.
answered 15 hours ago
TomaszTomasz
9,59652965
9,59652965
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%2f501602%2fsubstitute-and-directories-by-actual-names%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
Try the
realpath
command if it is available.– Bodo
15 hours ago
@Bodo realpath works good in this case.
– Alex Kuchin
15 hours ago