Substitute ./ and ../ directories by actual names
Clash Royale CLAN TAG#URR8PPP
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
Feb 19 at 14:33
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59
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 Feb 19 at 15:30
Alex Kuchin
asked Feb 19 at 14:30
Alex KuchinAlex Kuchin
256
256
Try therealpath
command if it is available.
– Bodo
Feb 19 at 14:33
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59
add a comment |
Try therealpath
command if it is available.
– Bodo
Feb 19 at 14:33
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59
Try the
realpath
command if it is available.– Bodo
Feb 19 at 14:33
Try the
realpath
command if it is available.– Bodo
Feb 19 at 14:33
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59
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
Feb 19 at 16:55
@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
Feb 19 at 18:43
4
@JoL Tryreadlink -f .
– twalberg
Feb 19 at 18:44
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
Feb 19 at 16:55
@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
Feb 19 at 18:43
4
@JoL Tryreadlink -f .
– twalberg
Feb 19 at 18:44
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
Feb 19 at 16:55
@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
Feb 19 at 18:43
4
@JoL Tryreadlink -f .
– twalberg
Feb 19 at 18:44
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 Feb 19 at 14:43
answered Feb 19 at 14:40
ilkkachuilkkachu
61.5k10100177
61.5k10100177
readlink
would be another alternative.
– twalberg
Feb 19 at 16:55
@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
Feb 19 at 18:43
4
@JoL Tryreadlink -f .
– twalberg
Feb 19 at 18:44
add a comment |
readlink
would be another alternative.
– twalberg
Feb 19 at 16:55
@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
Feb 19 at 18:43
4
@JoL Tryreadlink -f .
– twalberg
Feb 19 at 18:44
readlink
would be another alternative.– twalberg
Feb 19 at 16:55
readlink
would be another alternative.– twalberg
Feb 19 at 16:55
@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
Feb 19 at 18:43
@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
Feb 19 at 18:43
4
4
@JoL Try
readlink -f .
– twalberg
Feb 19 at 18:44
@JoL Try
readlink -f .
– twalberg
Feb 19 at 18:44
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 Feb 19 at 14:44
ctrl-alt-delorctrl-alt-delor
12k42360
12k42360
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 Feb 19 at 14:42
TomaszTomasz
10.2k53068
10.2k53068
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
Feb 19 at 14:33
@Bodo realpath works good in this case.
– Alex Kuchin
Feb 19 at 14:59