Conditional statements: finding folders that don't contain a particular file
I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.
find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print
However, I don't understand some of its features:
- What does the exclamation mark do here? Does it somehow negate the condition statement that follows?
- Why is it necessary to open a new shell using
sh
rather than directly piping tols
?
files find directory ls
add a comment |
I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.
find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print
However, I don't understand some of its features:
- What does the exclamation mark do here? Does it somehow negate the condition statement that follows?
- Why is it necessary to open a new shell using
sh
rather than directly piping tols
?
files find directory ls
add a comment |
I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.
find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print
However, I don't understand some of its features:
- What does the exclamation mark do here? Does it somehow negate the condition statement that follows?
- Why is it necessary to open a new shell using
sh
rather than directly piping tols
?
files find directory ls
I needed to find all folders, whose names have a digit in them, that don't contain a file ending with .counts.txt. By combining several suggestions I found online, I was able to come up with the following command (for bash), which does what I want.
find . maxdepth 1 -type d -name "*[0-9]*" ! -exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ; -print
However, I don't understand some of its features:
- What does the exclamation mark do here? Does it somehow negate the condition statement that follows?
- Why is it necessary to open a new shell using
sh
rather than directly piping tols
?
files find directory ls
files find directory ls
edited Dec 16 at 4:24
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Jul 15 '17 at 10:19
GingerBadger
1083
1083
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- Yes the
!
negates the next condition, that is the-exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;
. As this is an-exec
condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0). - The arguments after
-exec
are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.
See man find
for the docs on the !
and -exec
args for find.
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%2f378624%2fconditional-statements-finding-folders-that-dont-contain-a-particular-file%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
- Yes the
!
negates the next condition, that is the-exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;
. As this is an-exec
condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0). - The arguments after
-exec
are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.
See man find
for the docs on the !
and -exec
args for find.
add a comment |
- Yes the
!
negates the next condition, that is the-exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;
. As this is an-exec
condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0). - The arguments after
-exec
are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.
See man find
for the docs on the !
and -exec
args for find.
add a comment |
- Yes the
!
negates the next condition, that is the-exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;
. As this is an-exec
condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0). - The arguments after
-exec
are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.
See man find
for the docs on the !
and -exec
args for find.
- Yes the
!
negates the next condition, that is the-exec sh -c 'ls -1 "{}" | egrep -q "*.counts.txt"' ;
. As this is an-exec
condition that is negated we have to remeber that it is true if the command it executes returns with status 0 (and negated the condition is true if the status is not 0). - The arguments after
-exec
are not passed to a shell but instead are executed directly so there is no concept of a pipe available. If you would put a pipe character there the outer shell (which started find itself) would use that and all the output from find would go to egrep.
See man find
for the docs on the !
and -exec
args for find.
answered Jul 15 '17 at 11:23
Lucas
1,988717
1,988717
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f378624%2fconditional-statements-finding-folders-that-dont-contain-a-particular-file%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