Understanding find commandHow to exclude a list of full directory paths in find command on SolarisLinux: Does...
Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."
What is the chance of getting a Red Cabbage in year 1?
Private Domain and SAP
Cheap oscilloscope showing 16 MHz square wave
Heuristic argument for the Riemann Hypothesis
Why 50 Ω termination results in less noise than 1 MΩ termination on the scope reading?
How to run a command 1 out of N times in Bash
When you have to wait for a short time
A word for the urge to do the opposite
Does using composite keys violate 2NF
Who declared the Last Alliance to be the "last" and why?
Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?
Can UV radiation be safe for the skin?
Four day weekend?
How can I store milk for long periods of time?
Why haven't the British protested Brexit as ardently as the Hong Kong protesters?
In what language did Túrin converse with Mím?
Does FERPA require parental notification of disability assessment?
How do I get my neighbour to stop disturbing with loud music?
How smart contract transactions work?
How to load files as a quickfix window at start-up
How is the anglicism "jackpot" commonly expressed in French?
How can I improve my formal definitions?
Can two aircraft be allowed to stay on the same runway at the same time?
Understanding find command
How to exclude a list of full directory paths in find command on SolarisLinux: Does find | xargs grep have limitations?find command with regex quantifier e.g. {1,2}-not -name does not work with findExclude directory in (native Solaris) find commandExclude from wildcard (*) using findFind command with multiple conditionsExclude a list of directories from unix find commandZipping all sub directories
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to find any file named lets say dork, but I want to exclude the files that are dork.zip. I'm also removing all the errors from permissions to directories.
So I have composed the command as such:
find / -name "dork*" -and !"zip" 2>&1 | grep -iv "not"
This did not work as I expected.
linux find
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to find any file named lets say dork, but I want to exclude the files that are dork.zip. I'm also removing all the errors from permissions to directories.
So I have composed the command as such:
find / -name "dork*" -and !"zip" 2>&1 | grep -iv "not"
This did not work as I expected.
linux find
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago
add a comment |
I am trying to find any file named lets say dork, but I want to exclude the files that are dork.zip. I'm also removing all the errors from permissions to directories.
So I have composed the command as such:
find / -name "dork*" -and !"zip" 2>&1 | grep -iv "not"
This did not work as I expected.
linux find
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to find any file named lets say dork, but I want to exclude the files that are dork.zip. I'm also removing all the errors from permissions to directories.
So I have composed the command as such:
find / -name "dork*" -and !"zip" 2>&1 | grep -iv "not"
This did not work as I expected.
linux find
linux find
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
Rui F Ribeiro
41.4k16 gold badges95 silver badges158 bronze badges
41.4k16 gold badges95 silver badges158 bronze badges
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago
ConfuseDConfuseD
11 bronze badge
11 bronze badge
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ConfuseD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago
add a comment |
2
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago
2
2
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago
add a comment |
2 Answers
2
active
oldest
votes
find / -name 'dork*' ! -name '*.zip'
You need to use two -name tests. One for matching dork* and one for matching *.zip. The second of these should be inverted (the !) so that the found names do not match it. There is always an implicit AND between the tests.
Making the implicit ANDs visible:
find / -name 'dork*' -a ! -name '*.zip'
Also using non-standard GNU syntax:
find / -name 'dork*' -and -not -name '*.zip'
To ignore errors produced by this, redirect the standard error stream to /dev/null with 2>/dev/null at the end.
With GNU find you could instead choose to not enter directories that are not readable:
find / ( -type d -not -readable -prune ) -or
( -name 'dork*' -not -name '*.zip' -print )
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
add a comment |
You're misunderstanding the syntax for the find expression. I think you're expecting the -name test to treat "dork*" -and !"zip" as the pattern it looks for, but -name expects a single pattern string, not some sort of expression. The way find is parsing it is: (the name matches dork*) and (!zip), with !zip being a separate subexpression from the -name primary. But !zip isn't a valid expression, so you get an error.
You want something more like (the name matches dork*) and (the name does not match *.zip).
find / -name "dork*" -and '!' -name "*.zip"
(The -and is not really needed; I left it in here for clarity.)
I've made a few other corrections here: The pattern is *.zip, because zip would only match a file named exactly "zip". The ! is separated by a space so that find will recognize it as a separate element, rather than part of something else, and I single-quoted it so if you use the command interactively, the shell won't try to interpret it as a history reference.
BTW, what's going on with the 2>&1 andgrep -iv "not"`? If you're trying to filter error messages, and are using bash (not some other shell), you can use command substitution to avoid mixing the output and error streams:
find / -name "dork*" -and '!' -name "*.zip" 2> >(grep -iv "not" >&2)
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,2>/dev/nullwill do the trick.
– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
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
});
}
});
ConfuseD is a new contributor. Be nice, and check out our Code of Conduct.
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%2f538501%2funderstanding-find-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
find / -name 'dork*' ! -name '*.zip'
You need to use two -name tests. One for matching dork* and one for matching *.zip. The second of these should be inverted (the !) so that the found names do not match it. There is always an implicit AND between the tests.
Making the implicit ANDs visible:
find / -name 'dork*' -a ! -name '*.zip'
Also using non-standard GNU syntax:
find / -name 'dork*' -and -not -name '*.zip'
To ignore errors produced by this, redirect the standard error stream to /dev/null with 2>/dev/null at the end.
With GNU find you could instead choose to not enter directories that are not readable:
find / ( -type d -not -readable -prune ) -or
( -name 'dork*' -not -name '*.zip' -print )
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
add a comment |
find / -name 'dork*' ! -name '*.zip'
You need to use two -name tests. One for matching dork* and one for matching *.zip. The second of these should be inverted (the !) so that the found names do not match it. There is always an implicit AND between the tests.
Making the implicit ANDs visible:
find / -name 'dork*' -a ! -name '*.zip'
Also using non-standard GNU syntax:
find / -name 'dork*' -and -not -name '*.zip'
To ignore errors produced by this, redirect the standard error stream to /dev/null with 2>/dev/null at the end.
With GNU find you could instead choose to not enter directories that are not readable:
find / ( -type d -not -readable -prune ) -or
( -name 'dork*' -not -name '*.zip' -print )
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
add a comment |
find / -name 'dork*' ! -name '*.zip'
You need to use two -name tests. One for matching dork* and one for matching *.zip. The second of these should be inverted (the !) so that the found names do not match it. There is always an implicit AND between the tests.
Making the implicit ANDs visible:
find / -name 'dork*' -a ! -name '*.zip'
Also using non-standard GNU syntax:
find / -name 'dork*' -and -not -name '*.zip'
To ignore errors produced by this, redirect the standard error stream to /dev/null with 2>/dev/null at the end.
With GNU find you could instead choose to not enter directories that are not readable:
find / ( -type d -not -readable -prune ) -or
( -name 'dork*' -not -name '*.zip' -print )
find / -name 'dork*' ! -name '*.zip'
You need to use two -name tests. One for matching dork* and one for matching *.zip. The second of these should be inverted (the !) so that the found names do not match it. There is always an implicit AND between the tests.
Making the implicit ANDs visible:
find / -name 'dork*' -a ! -name '*.zip'
Also using non-standard GNU syntax:
find / -name 'dork*' -and -not -name '*.zip'
To ignore errors produced by this, redirect the standard error stream to /dev/null with 2>/dev/null at the end.
With GNU find you could instead choose to not enter directories that are not readable:
find / ( -type d -not -readable -prune ) -or
( -name 'dork*' -not -name '*.zip' -print )
edited 3 hours ago
answered 3 hours ago
Kusalananda♦Kusalananda
162k19 gold badges320 silver badges507 bronze badges
162k19 gold badges320 silver badges507 bronze badges
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
add a comment |
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
Thanks! I spent hours trying to figure this out and within 30 mins I have my answer. Thank you and to everyone for monitoring and helping us newbs out!
– ConfuseD
3 hours ago
add a comment |
You're misunderstanding the syntax for the find expression. I think you're expecting the -name test to treat "dork*" -and !"zip" as the pattern it looks for, but -name expects a single pattern string, not some sort of expression. The way find is parsing it is: (the name matches dork*) and (!zip), with !zip being a separate subexpression from the -name primary. But !zip isn't a valid expression, so you get an error.
You want something more like (the name matches dork*) and (the name does not match *.zip).
find / -name "dork*" -and '!' -name "*.zip"
(The -and is not really needed; I left it in here for clarity.)
I've made a few other corrections here: The pattern is *.zip, because zip would only match a file named exactly "zip". The ! is separated by a space so that find will recognize it as a separate element, rather than part of something else, and I single-quoted it so if you use the command interactively, the shell won't try to interpret it as a history reference.
BTW, what's going on with the 2>&1 andgrep -iv "not"`? If you're trying to filter error messages, and are using bash (not some other shell), you can use command substitution to avoid mixing the output and error streams:
find / -name "dork*" -and '!' -name "*.zip" 2> >(grep -iv "not" >&2)
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,2>/dev/nullwill do the trick.
– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
add a comment |
You're misunderstanding the syntax for the find expression. I think you're expecting the -name test to treat "dork*" -and !"zip" as the pattern it looks for, but -name expects a single pattern string, not some sort of expression. The way find is parsing it is: (the name matches dork*) and (!zip), with !zip being a separate subexpression from the -name primary. But !zip isn't a valid expression, so you get an error.
You want something more like (the name matches dork*) and (the name does not match *.zip).
find / -name "dork*" -and '!' -name "*.zip"
(The -and is not really needed; I left it in here for clarity.)
I've made a few other corrections here: The pattern is *.zip, because zip would only match a file named exactly "zip". The ! is separated by a space so that find will recognize it as a separate element, rather than part of something else, and I single-quoted it so if you use the command interactively, the shell won't try to interpret it as a history reference.
BTW, what's going on with the 2>&1 andgrep -iv "not"`? If you're trying to filter error messages, and are using bash (not some other shell), you can use command substitution to avoid mixing the output and error streams:
find / -name "dork*" -and '!' -name "*.zip" 2> >(grep -iv "not" >&2)
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,2>/dev/nullwill do the trick.
– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
add a comment |
You're misunderstanding the syntax for the find expression. I think you're expecting the -name test to treat "dork*" -and !"zip" as the pattern it looks for, but -name expects a single pattern string, not some sort of expression. The way find is parsing it is: (the name matches dork*) and (!zip), with !zip being a separate subexpression from the -name primary. But !zip isn't a valid expression, so you get an error.
You want something more like (the name matches dork*) and (the name does not match *.zip).
find / -name "dork*" -and '!' -name "*.zip"
(The -and is not really needed; I left it in here for clarity.)
I've made a few other corrections here: The pattern is *.zip, because zip would only match a file named exactly "zip". The ! is separated by a space so that find will recognize it as a separate element, rather than part of something else, and I single-quoted it so if you use the command interactively, the shell won't try to interpret it as a history reference.
BTW, what's going on with the 2>&1 andgrep -iv "not"`? If you're trying to filter error messages, and are using bash (not some other shell), you can use command substitution to avoid mixing the output and error streams:
find / -name "dork*" -and '!' -name "*.zip" 2> >(grep -iv "not" >&2)
You're misunderstanding the syntax for the find expression. I think you're expecting the -name test to treat "dork*" -and !"zip" as the pattern it looks for, but -name expects a single pattern string, not some sort of expression. The way find is parsing it is: (the name matches dork*) and (!zip), with !zip being a separate subexpression from the -name primary. But !zip isn't a valid expression, so you get an error.
You want something more like (the name matches dork*) and (the name does not match *.zip).
find / -name "dork*" -and '!' -name "*.zip"
(The -and is not really needed; I left it in here for clarity.)
I've made a few other corrections here: The pattern is *.zip, because zip would only match a file named exactly "zip". The ! is separated by a space so that find will recognize it as a separate element, rather than part of something else, and I single-quoted it so if you use the command interactively, the shell won't try to interpret it as a history reference.
BTW, what's going on with the 2>&1 andgrep -iv "not"`? If you're trying to filter error messages, and are using bash (not some other shell), you can use command substitution to avoid mixing the output and error streams:
find / -name "dork*" -and '!' -name "*.zip" 2> >(grep -iv "not" >&2)
answered 3 hours ago
Gordon DavissonGordon Davisson
2,12512 silver badges10 bronze badges
2,12512 silver badges10 bronze badges
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,2>/dev/nullwill do the trick.
– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
add a comment |
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,2>/dev/nullwill do the trick.
– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
I was just trying to filter out stderr for things like this /Users/xxxxxxxx/Library/Caches/CloudKit/com.apple.Safari: Operation not permitted find: /Users/xxxxxxxx/Library/Caches/com.apple.Safari: Operation not permitted find: /dev/fd/3: Not a directory find: /dev/fd/4: Not a directory
– ConfuseD
3 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,
2>/dev/null will do the trick.– Gordon Davisson
2 hours ago
@ConfuseD Do you want to suppress errors entirely? If so,
2>/dev/null will do the trick.– Gordon Davisson
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
yes. Thanks, that works slick!!
– ConfuseD
2 hours ago
add a comment |
ConfuseD is a new contributor. Be nice, and check out our Code of Conduct.
ConfuseD is a new contributor. Be nice, and check out our Code of Conduct.
ConfuseD is a new contributor. Be nice, and check out our Code of Conduct.
ConfuseD is a new contributor. Be nice, and check out our Code of Conduct.
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%2f538501%2funderstanding-find-command%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
2
Please explain how it is not working as you expect.
– Jesse_b
4 hours ago
Sure. I have strategically placed files dork0.log dork1.log and dork3.zip in different directories. Without the -and operator it finds all the files. but with the -and it bombs out.
– ConfuseD
3 hours ago