How to store the full-path of a directory from a bash script?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I want to get the full-path director from a script, even if the user enters the './' or '/~'
#!/bin/bash
echo "Enter the directory"
read DIRECTORY #how to store the full-path instead of something like ./
echo "$DIRECTORY"
linux bash scripting
add a comment |Â
up vote
2
down vote
favorite
I want to get the full-path director from a script, even if the user enters the './' or '/~'
#!/bin/bash
echo "Enter the directory"
read DIRECTORY #how to store the full-path instead of something like ./
echo "$DIRECTORY"
linux bash scripting
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to get the full-path director from a script, even if the user enters the './' or '/~'
#!/bin/bash
echo "Enter the directory"
read DIRECTORY #how to store the full-path instead of something like ./
echo "$DIRECTORY"
linux bash scripting
I want to get the full-path director from a script, even if the user enters the './' or '/~'
#!/bin/bash
echo "Enter the directory"
read DIRECTORY #how to store the full-path instead of something like ./
echo "$DIRECTORY"
linux bash scripting
linux bash scripting
edited 4 hours ago
asked 4 hours ago
Bionix1441
17111
17111
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
0
down vote
accepted
Use realpath:
echo "$(realpath $DIRECTORY)"
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦
â Gilles
3 hours ago
add a comment |Â
up vote
2
down vote
For a directory, you can use the command pwd
or the variable $PWD
.
Example
$ DIRECTORY=.
$ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
$ echo $TRUEDIR
/home/steve
$
add a comment |Â
up vote
1
down vote
An absolute path begins with /
, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in
/*) DIRECTORY="$PWD/$DIRECTORY";;
esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
add a comment |Â
up vote
0
down vote
You may add a test for an absolute path:
read DIRECTORY
[ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
echo "$DIRECTORY"
add a comment |Â
up vote
0
down vote
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
New contributor
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Use realpath:
echo "$(realpath $DIRECTORY)"
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦
â Gilles
3 hours ago
add a comment |Â
up vote
0
down vote
accepted
Use realpath:
echo "$(realpath $DIRECTORY)"
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦
â Gilles
3 hours ago
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Use realpath:
echo "$(realpath $DIRECTORY)"
Use realpath:
echo "$(realpath $DIRECTORY)"
answered 4 hours ago
Ipor Sircer
9,5011920
9,5011920
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦
â Gilles
3 hours ago
add a comment |Â
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦
â Gilles
3 hours ago
1
1
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and
--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦â Gilles
3 hours ago
This removes symbolic links, which is usually not desirable. Also you left out required double quotes and
--
so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/â¦â Gilles
3 hours ago
add a comment |Â
up vote
2
down vote
For a directory, you can use the command pwd
or the variable $PWD
.
Example
$ DIRECTORY=.
$ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
$ echo $TRUEDIR
/home/steve
$
add a comment |Â
up vote
2
down vote
For a directory, you can use the command pwd
or the variable $PWD
.
Example
$ DIRECTORY=.
$ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
$ echo $TRUEDIR
/home/steve
$
add a comment |Â
up vote
2
down vote
up vote
2
down vote
For a directory, you can use the command pwd
or the variable $PWD
.
Example
$ DIRECTORY=.
$ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
$ echo $TRUEDIR
/home/steve
$
For a directory, you can use the command pwd
or the variable $PWD
.
Example
$ DIRECTORY=.
$ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
$ echo $TRUEDIR
/home/steve
$
edited 2 hours ago
answered 4 hours ago
rusty shackleford
1,275116
1,275116
add a comment |Â
add a comment |Â
up vote
1
down vote
An absolute path begins with /
, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in
/*) DIRECTORY="$PWD/$DIRECTORY";;
esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
add a comment |Â
up vote
1
down vote
An absolute path begins with /
, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in
/*) DIRECTORY="$PWD/$DIRECTORY";;
esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
add a comment |Â
up vote
1
down vote
up vote
1
down vote
An absolute path begins with /
, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in
/*) DIRECTORY="$PWD/$DIRECTORY";;
esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
An absolute path begins with /
, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.
case $DIRECTORY in
/*) DIRECTORY="$PWD/$DIRECTORY";;
esac
This is portable code: it works in plain sh, not just in bash.
The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link
answered 3 hours ago
Gilles
514k12110191549
514k12110191549
add a comment |Â
add a comment |Â
up vote
0
down vote
You may add a test for an absolute path:
read DIRECTORY
[ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
echo "$DIRECTORY"
add a comment |Â
up vote
0
down vote
You may add a test for an absolute path:
read DIRECTORY
[ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
echo "$DIRECTORY"
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You may add a test for an absolute path:
read DIRECTORY
[ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
echo "$DIRECTORY"
You may add a test for an absolute path:
read DIRECTORY
[ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
echo "$DIRECTORY"
answered 4 hours ago
RudiC
1,91219
1,91219
add a comment |Â
add a comment |Â
up vote
0
down vote
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
New contributor
add a comment |Â
up vote
0
down vote
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
New contributor
add a comment |Â
up vote
0
down vote
up vote
0
down vote
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
New contributor
realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:
readlink -f "$DIRECTORY"
It too handles ~ and ./ and will follow symbolic links.
New contributor
New contributor
answered 24 mins ago
Glenn
1
1
New contributor
New contributor
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%2f475030%2fhow-to-store-the-full-path-of-a-directory-from-a-bash-script%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