Checking for the existence of multiple directories
I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.
I have the following
if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
echo True
else
echo False
fi
But I suspect there is a more elegant way of doing this. Do not assume that there is a pattern in the names of the directories.
The goal is to check for the existence of a few directories and for the nonexistence of others.
I'm using Bash, but portable code is preferred.
shell-script shell files directory control-flow
add a comment |
I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.
I have the following
if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
echo True
else
echo False
fi
But I suspect there is a more elegant way of doing this. Do not assume that there is a pattern in the names of the directories.
The goal is to check for the existence of a few directories and for the nonexistence of others.
I'm using Bash, but portable code is preferred.
shell-script shell files directory control-flow
1
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
1
You don't need the$PWD, by the way.[ -d "$PWD/dir1"]is equivalent to[ -d "dir1" ].
– terdon♦
Mar 2 at 14:43
add a comment |
I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.
I have the following
if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
echo True
else
echo False
fi
But I suspect there is a more elegant way of doing this. Do not assume that there is a pattern in the names of the directories.
The goal is to check for the existence of a few directories and for the nonexistence of others.
I'm using Bash, but portable code is preferred.
shell-script shell files directory control-flow
I want to check for the existence of multiple directories, say, dir1, dir2 and dir3, in the working directory.
I have the following
if [ -d "$PWD/dir1" ] && [ -d "$PWD/dir2" ] && [ -d "$PWD/dir3" ]; then
echo True
else
echo False
fi
But I suspect there is a more elegant way of doing this. Do not assume that there is a pattern in the names of the directories.
The goal is to check for the existence of a few directories and for the nonexistence of others.
I'm using Bash, but portable code is preferred.
shell-script shell files directory control-flow
shell-script shell files directory control-flow
edited Mar 2 at 15:13
G-Man
13.4k93667
13.4k93667
asked Mar 1 at 17:26
EleganceElegance
333
333
1
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
1
You don't need the$PWD, by the way.[ -d "$PWD/dir1"]is equivalent to[ -d "dir1" ].
– terdon♦
Mar 2 at 14:43
add a comment |
1
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
1
You don't need the$PWD, by the way.[ -d "$PWD/dir1"]is equivalent to[ -d "dir1" ].
– terdon♦
Mar 2 at 14:43
1
1
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
1
1
You don't need the
$PWD, by the way. [ -d "$PWD/dir1"] is equivalent to [ -d "dir1" ].– terdon♦
Mar 2 at 14:43
You don't need the
$PWD, by the way. [ -d "$PWD/dir1"] is equivalent to [ -d "dir1" ].– terdon♦
Mar 2 at 14:43
add a comment |
6 Answers
6
active
oldest
votes
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears — it is not written to your terminal.
Can you help me understand this command? I know what file descriptors are. I know1is stdout,2is stderr and I know what redirecting is. I don't understand the significance of/dev/null, and I do not know how to parse the command.
– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
(1) You should probably use the-doption (a) solsneeds only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use"$PWD/"except to guard against directories whose names begin with-(and, of course, there are better ways to do that).
– G-Man
Mar 2 at 3:40
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
|
show 4 more comments
I would loop:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break causes the loop to short-circuit, just like your chain of &&
add a comment |
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@".
add a comment |
Why not just:
if [ -d "dir1" -a -d "dir2" -a -d "dir3" ]; then
echo True
else
echo False
fi
This is essentially what the OP started with,But I suspect there is a more elegant way of doing this
– Jeff Schaller
Mar 1 at 20:52
5
Also POSIX discourages the use of-a: "-aand-obinary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799
– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
it's not at all the same, the original invokestest(aka[) three times this invokes it once,
– Jasen
Mar 2 at 6:17
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
|
show 1 more comment
As per the question, two portable shell functions that test for the existence and nonexistence of multiple directories:
# Returns success if all given arguments exists and are directories.
ck_dir_exists () {
for dir do
[ -d "$dir" ] || return 1
done
}
# Returns success if none of the given arguments are existing directories.
ck_dir_notexists () {
for dir do
[ ! -d "$dir" ] || return 1
done
}
Example:
$ mkdir dir1 dir2
$ ck_dir_exists dir1 dir2; echo $?
0
$ ck_dir_exists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir3 dir4; echo $?
0
add a comment |
The goal is to check for the existence of a few directories
and for the nonexistence of others.
[Emphasis added]
Building on glenn jackman’s answer,
we can test for the nonexistence of other names like this:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
for dir in
"$PWD/dir4"
"$PWD/dir5"
"$PWD/dir6"
do
if [ -e "$dir" ]; then # Note: no “!”
result=False
break
fi
done
echo "$result"
I used
[ -e "$dir" ] to test whether "$dir" exists;i.e., if
dir5 exists but is a file, the result is False. If you want only to test whether the names in the second group are directories,
use
[ -d "$dir" ], like in the first loop.Since we’re talking about checking for the existence of things
in the current working directory,
it’s probably not necessary to specify $PWD/ on the names; just do
for dir in
"dir1"
"dir2"
"dir3"
do
︙
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%2f503830%2fchecking-for-the-existence-of-multiple-directories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears — it is not written to your terminal.
Can you help me understand this command? I know what file descriptors are. I know1is stdout,2is stderr and I know what redirecting is. I don't understand the significance of/dev/null, and I do not know how to parse the command.
– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
(1) You should probably use the-doption (a) solsneeds only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use"$PWD/"except to guard against directories whose names begin with-(and, of course, there are better ways to do that).
– G-Man
Mar 2 at 3:40
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
|
show 4 more comments
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears — it is not written to your terminal.
Can you help me understand this command? I know what file descriptors are. I know1is stdout,2is stderr and I know what redirecting is. I don't understand the significance of/dev/null, and I do not know how to parse the command.
– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
(1) You should probably use the-doption (a) solsneeds only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use"$PWD/"except to guard against directories whose names begin with-(and, of course, there are better ways to do that).
– G-Man
Mar 2 at 3:40
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
|
show 4 more comments
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears — it is not written to your terminal.
If you already expect them to be directories and are just checking whether they all exist, you could use the exit code from the ls utility to determine whether one or more "errors occurred":
ls "$PWD/dir1" "$PWD/dir2" "$PWD/dir3" >/dev/null 2>&1 && echo All there
I redirect the output and stderr to /dev/null in order to make it disappear, since we only care about the exit code from ls, not its output. Anything that's written to /dev/null disappears — it is not written to your terminal.
edited Mar 2 at 3:39
G-Man
13.4k93667
13.4k93667
answered Mar 1 at 17:38
Jeff SchallerJeff Schaller
43.2k1159138
43.2k1159138
Can you help me understand this command? I know what file descriptors are. I know1is stdout,2is stderr and I know what redirecting is. I don't understand the significance of/dev/null, and I do not know how to parse the command.
– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
(1) You should probably use the-doption (a) solsneeds only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use"$PWD/"except to guard against directories whose names begin with-(and, of course, there are better ways to do that).
– G-Man
Mar 2 at 3:40
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
|
show 4 more comments
Can you help me understand this command? I know what file descriptors are. I know1is stdout,2is stderr and I know what redirecting is. I don't understand the significance of/dev/null, and I do not know how to parse the command.
– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
(1) You should probably use the-doption (a) solsneeds only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use"$PWD/"except to guard against directories whose names begin with-(and, of course, there are better ways to do that).
– G-Man
Mar 2 at 3:40
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
Can you help me understand this command? I know what file descriptors are. I know
1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.– Elegance
Mar 1 at 18:46
Can you help me understand this command? I know what file descriptors are. I know
1 is stdout, 2 is stderr and I know what redirecting is. I don't understand the significance of /dev/null, and I do not know how to parse the command.– Elegance
Mar 1 at 18:46
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
@Elegance I added a little explanation. For more in-depth answers regarding /dev/null, see unix.stackexchange.com/questions/163352/… and unix.stackexchange.com/questions/438130/…
– Jeff Schaller
Mar 1 at 18:51
1
1
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
I got it finally. Thanks.
– Elegance
Mar 1 at 19:27
3
3
(1) You should probably use the
-d option (a) so ls needs only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use "$PWD/" except to guard against directories whose names begin with - (and, of course, there are better ways to do that).– G-Man
Mar 2 at 3:40
(1) You should probably use the
-d option (a) so ls needs only to stat the directories, and not read them, and (b) so the command will succeed even if the user doesn’t have read access to the directories. (2) I don’t see any reason to use "$PWD/" except to guard against directories whose names begin with - (and, of course, there are better ways to do that).– G-Man
Mar 2 at 3:40
1
1
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
this command could take much longer to run than the test in the original question. also it doesn't check for directories.
– Jasen
Mar 2 at 6:16
|
show 4 more comments
I would loop:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break causes the loop to short-circuit, just like your chain of &&
add a comment |
I would loop:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break causes the loop to short-circuit, just like your chain of &&
add a comment |
I would loop:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break causes the loop to short-circuit, just like your chain of &&
I would loop:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
echo "$result"
The break causes the loop to short-circuit, just like your chain of &&
answered Mar 1 at 17:35
glenn jackmanglenn jackman
52.2k572112
52.2k572112
add a comment |
add a comment |
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@".
add a comment |
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@".
add a comment |
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@".
A loop might be more elegant:
arr=("$PWD/dir1" "$PWD/dir2" "$PWD/dir2")
for d in "${arr[@]}"; do
if [ -d "$d"]; then
echo True
else
echo False
fi
done
This is Bash. A more portable one is Sh. There you can use the positional array:
set -- "$PWD/dir1" "$PWD/dir2" "$PWD/dir2"
Then to loop over it use "$@".
edited Mar 1 at 17:41
answered Mar 1 at 17:35
TomaszTomasz
10.1k53067
10.1k53067
add a comment |
add a comment |
Why not just:
if [ -d "dir1" -a -d "dir2" -a -d "dir3" ]; then
echo True
else
echo False
fi
This is essentially what the OP started with,But I suspect there is a more elegant way of doing this
– Jeff Schaller
Mar 1 at 20:52
5
Also POSIX discourages the use of-a: "-aand-obinary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799
– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
it's not at all the same, the original invokestest(aka[) three times this invokes it once,
– Jasen
Mar 2 at 6:17
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
|
show 1 more comment
Why not just:
if [ -d "dir1" -a -d "dir2" -a -d "dir3" ]; then
echo True
else
echo False
fi
This is essentially what the OP started with,But I suspect there is a more elegant way of doing this
– Jeff Schaller
Mar 1 at 20:52
5
Also POSIX discourages the use of-a: "-aand-obinary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799
– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
it's not at all the same, the original invokestest(aka[) three times this invokes it once,
– Jasen
Mar 2 at 6:17
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
|
show 1 more comment
Why not just:
if [ -d "dir1" -a -d "dir2" -a -d "dir3" ]; then
echo True
else
echo False
fi
Why not just:
if [ -d "dir1" -a -d "dir2" -a -d "dir3" ]; then
echo True
else
echo False
fi
answered Mar 1 at 20:02
David ConradDavid Conrad
1894
1894
This is essentially what the OP started with,But I suspect there is a more elegant way of doing this
– Jeff Schaller
Mar 1 at 20:52
5
Also POSIX discourages the use of-a: "-aand-obinary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799
– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
it's not at all the same, the original invokestest(aka[) three times this invokes it once,
– Jasen
Mar 2 at 6:17
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
|
show 1 more comment
This is essentially what the OP started with,But I suspect there is a more elegant way of doing this
– Jeff Schaller
Mar 1 at 20:52
5
Also POSIX discourages the use of-a: "-aand-obinary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799
– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
it's not at all the same, the original invokestest(aka[) three times this invokes it once,
– Jasen
Mar 2 at 6:17
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
This is essentially what the OP started with,
But I suspect there is a more elegant way of doing this– Jeff Schaller
Mar 1 at 20:52
This is essentially what the OP started with,
But I suspect there is a more elegant way of doing this– Jeff Schaller
Mar 1 at 20:52
5
5
Also POSIX discourages the use of
-a: "-a and -o binary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799– Elegance
Mar 1 at 20:55
Also POSIX discourages the use of
-a: "-a and -o binary primaries (...) operators have been marked obsolescent": pubs.opengroup.org/onlinepubs/9699919799– Elegance
Mar 1 at 20:55
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
@JeffSchaller It's more terse since it does it all in one call to test.
– David Conrad
Mar 2 at 0:27
2
2
it's not at all the same, the original invokes
test (aka [) three times this invokes it once,– Jasen
Mar 2 at 6:17
it's not at all the same, the original invokes
test (aka [) three times this invokes it once,– Jasen
Mar 2 at 6:17
1
1
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
pubs.opengroup.org/onlinepubs/9699919799/utilities/… is a more precise reference to the location of the POSIX statement.
– G-Man
Mar 2 at 15:45
|
show 1 more comment
As per the question, two portable shell functions that test for the existence and nonexistence of multiple directories:
# Returns success if all given arguments exists and are directories.
ck_dir_exists () {
for dir do
[ -d "$dir" ] || return 1
done
}
# Returns success if none of the given arguments are existing directories.
ck_dir_notexists () {
for dir do
[ ! -d "$dir" ] || return 1
done
}
Example:
$ mkdir dir1 dir2
$ ck_dir_exists dir1 dir2; echo $?
0
$ ck_dir_exists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir3 dir4; echo $?
0
add a comment |
As per the question, two portable shell functions that test for the existence and nonexistence of multiple directories:
# Returns success if all given arguments exists and are directories.
ck_dir_exists () {
for dir do
[ -d "$dir" ] || return 1
done
}
# Returns success if none of the given arguments are existing directories.
ck_dir_notexists () {
for dir do
[ ! -d "$dir" ] || return 1
done
}
Example:
$ mkdir dir1 dir2
$ ck_dir_exists dir1 dir2; echo $?
0
$ ck_dir_exists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir3 dir4; echo $?
0
add a comment |
As per the question, two portable shell functions that test for the existence and nonexistence of multiple directories:
# Returns success if all given arguments exists and are directories.
ck_dir_exists () {
for dir do
[ -d "$dir" ] || return 1
done
}
# Returns success if none of the given arguments are existing directories.
ck_dir_notexists () {
for dir do
[ ! -d "$dir" ] || return 1
done
}
Example:
$ mkdir dir1 dir2
$ ck_dir_exists dir1 dir2; echo $?
0
$ ck_dir_exists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir3 dir4; echo $?
0
As per the question, two portable shell functions that test for the existence and nonexistence of multiple directories:
# Returns success if all given arguments exists and are directories.
ck_dir_exists () {
for dir do
[ -d "$dir" ] || return 1
done
}
# Returns success if none of the given arguments are existing directories.
ck_dir_notexists () {
for dir do
[ ! -d "$dir" ] || return 1
done
}
Example:
$ mkdir dir1 dir2
$ ck_dir_exists dir1 dir2; echo $?
0
$ ck_dir_exists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir1 dir2 dir3; echo $?
1
$ ck_dir_notexists dir3 dir4; echo $?
0
answered Mar 2 at 8:39
KusalanandaKusalananda
134k17255418
134k17255418
add a comment |
add a comment |
The goal is to check for the existence of a few directories
and for the nonexistence of others.
[Emphasis added]
Building on glenn jackman’s answer,
we can test for the nonexistence of other names like this:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
for dir in
"$PWD/dir4"
"$PWD/dir5"
"$PWD/dir6"
do
if [ -e "$dir" ]; then # Note: no “!”
result=False
break
fi
done
echo "$result"
I used
[ -e "$dir" ] to test whether "$dir" exists;i.e., if
dir5 exists but is a file, the result is False. If you want only to test whether the names in the second group are directories,
use
[ -d "$dir" ], like in the first loop.Since we’re talking about checking for the existence of things
in the current working directory,
it’s probably not necessary to specify $PWD/ on the names; just do
for dir in
"dir1"
"dir2"
"dir3"
do
︙
add a comment |
The goal is to check for the existence of a few directories
and for the nonexistence of others.
[Emphasis added]
Building on glenn jackman’s answer,
we can test for the nonexistence of other names like this:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
for dir in
"$PWD/dir4"
"$PWD/dir5"
"$PWD/dir6"
do
if [ -e "$dir" ]; then # Note: no “!”
result=False
break
fi
done
echo "$result"
I used
[ -e "$dir" ] to test whether "$dir" exists;i.e., if
dir5 exists but is a file, the result is False. If you want only to test whether the names in the second group are directories,
use
[ -d "$dir" ], like in the first loop.Since we’re talking about checking for the existence of things
in the current working directory,
it’s probably not necessary to specify $PWD/ on the names; just do
for dir in
"dir1"
"dir2"
"dir3"
do
︙
add a comment |
The goal is to check for the existence of a few directories
and for the nonexistence of others.
[Emphasis added]
Building on glenn jackman’s answer,
we can test for the nonexistence of other names like this:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
for dir in
"$PWD/dir4"
"$PWD/dir5"
"$PWD/dir6"
do
if [ -e "$dir" ]; then # Note: no “!”
result=False
break
fi
done
echo "$result"
I used
[ -e "$dir" ] to test whether "$dir" exists;i.e., if
dir5 exists but is a file, the result is False. If you want only to test whether the names in the second group are directories,
use
[ -d "$dir" ], like in the first loop.Since we’re talking about checking for the existence of things
in the current working directory,
it’s probably not necessary to specify $PWD/ on the names; just do
for dir in
"dir1"
"dir2"
"dir3"
do
︙
The goal is to check for the existence of a few directories
and for the nonexistence of others.
[Emphasis added]
Building on glenn jackman’s answer,
we can test for the nonexistence of other names like this:
result=True
for dir in
"$PWD/dir1"
"$PWD/dir2"
"$PWD/dir3"
do
if ! [ -d "$dir" ]; then
result=False
break
fi
done
for dir in
"$PWD/dir4"
"$PWD/dir5"
"$PWD/dir6"
do
if [ -e "$dir" ]; then # Note: no “!”
result=False
break
fi
done
echo "$result"
I used
[ -e "$dir" ] to test whether "$dir" exists;i.e., if
dir5 exists but is a file, the result is False. If you want only to test whether the names in the second group are directories,
use
[ -d "$dir" ], like in the first loop.Since we’re talking about checking for the existence of things
in the current working directory,
it’s probably not necessary to specify $PWD/ on the names; just do
for dir in
"dir1"
"dir2"
"dir3"
do
︙
answered Mar 2 at 4:04
G-ManG-Man
13.4k93667
13.4k93667
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%2f503830%2fchecking-for-the-existence-of-multiple-directories%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
“The goal is to check for the existence of a few directories and for the nonexistence of others.” Well, your example checks for existence of all listed directories. Can you elaborate on this part ? Is it all listed or any listed ? Do you need a check that passed values are in fact directories and not other types of files ?
– Sergiy Kolodyazhnyy
Mar 2 at 5:28
1
You don't need the
$PWD, by the way.[ -d "$PWD/dir1"]is equivalent to[ -d "dir1" ].– terdon♦
Mar 2 at 14:43