Find all directories which has only one type of file
up vote
-1
down vote
favorite
Given a file type .txt, find all directories that has only that type of file.
For example
a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt
The command should output only b not c.
linux operating-systems
add a comment |
up vote
-1
down vote
favorite
Given a file type .txt, find all directories that has only that type of file.
For example
a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt
The command should output only b not c.
linux operating-systems
Please add an OS tag. For Linux: I'd probably runfind, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
– dirkt
Nov 26 at 11:32
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Given a file type .txt, find all directories that has only that type of file.
For example
a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt
The command should output only b not c.
linux operating-systems
Given a file type .txt, find all directories that has only that type of file.
For example
a--
b-- 1.txt
c--
|
---- 2.jpg
---- 3.txt
The command should output only b not c.
linux operating-systems
linux operating-systems
edited Nov 27 at 8:50
asked Nov 26 at 10:20
Jibin
1155
1155
Please add an OS tag. For Linux: I'd probably runfind, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
– dirkt
Nov 26 at 11:32
add a comment |
Please add an OS tag. For Linux: I'd probably runfind, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.
– dirkt
Nov 26 at 11:32
Please add an OS tag. For Linux: I'd probably run
find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.– dirkt
Nov 26 at 11:32
Please add an OS tag. For Linux: I'd probably run
find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.– dirkt
Nov 26 at 11:32
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print
The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.
Notes:
-maxdepthis not required by POSIX. For POSIX approach see this question.
-quitis not required by POSIX. This action makesfindquit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result withwc -land[ … ], so quitting early saves time. Without-quitthe whole command will work, it will just be slower when there are many files. Alternatively you can usefind … | head -n 1 | wc -l; in this caseheadwill terminate the pipe after the first file found,wcwill yield the result right away, butfindwill only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off:headmay save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.- Paths with newlines (if any) will fool
wc -lbut it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print
The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.
Notes:
-maxdepthis not required by POSIX. For POSIX approach see this question.
-quitis not required by POSIX. This action makesfindquit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result withwc -land[ … ], so quitting early saves time. Without-quitthe whole command will work, it will just be slower when there are many files. Alternatively you can usefind … | head -n 1 | wc -l; in this caseheadwill terminate the pipe after the first file found,wcwill yield the result right away, butfindwill only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off:headmay save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.- Paths with newlines (if any) will fool
wc -lbut it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.
add a comment |
up vote
1
down vote
accepted
find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print
The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.
Notes:
-maxdepthis not required by POSIX. For POSIX approach see this question.
-quitis not required by POSIX. This action makesfindquit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result withwc -land[ … ], so quitting early saves time. Without-quitthe whole command will work, it will just be slower when there are many files. Alternatively you can usefind … | head -n 1 | wc -l; in this caseheadwill terminate the pipe after the first file found,wcwill yield the result right away, butfindwill only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off:headmay save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.- Paths with newlines (if any) will fool
wc -lbut it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print
The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.
Notes:
-maxdepthis not required by POSIX. For POSIX approach see this question.
-quitis not required by POSIX. This action makesfindquit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result withwc -land[ … ], so quitting early saves time. Without-quitthe whole command will work, it will just be slower when there are many files. Alternatively you can usefind … | head -n 1 | wc -l; in this caseheadwill terminate the pipe after the first file found,wcwill yield the result right away, butfindwill only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off:headmay save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.- Paths with newlines (if any) will fool
wc -lbut it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.
find . -type d -execdir sh -c '
[ "$(find "$1" -maxdepth 1 -type f -name "*.txt" -print -quit | wc -l)" -gt 0 ] &&
[ "$(find "$1" -maxdepth 1 -type f ! -name "*.txt" -print -quit | wc -l)" -eq 0 ]
' find-sh {} ; -print
The outer find supplies directories to be examined. Two inner find-s check if there is at least one .txt file and no non-.txt files in the directory. sh shell implements logic with [ … ] and &&.
Notes:
-maxdepthis not required by POSIX. For POSIX approach see this question.
-quitis not required by POSIX. This action makesfindquit as soon as any matching file is reported. It's useful because we need at most one matching file to obtain the result withwc -land[ … ], so quitting early saves time. Without-quitthe whole command will work, it will just be slower when there are many files. Alternatively you can usefind … | head -n 1 | wc -l; in this caseheadwill terminate the pipe after the first file found,wcwill yield the result right away, butfindwill only notice the broken pipe when (if) it tries to write yet another line. And it's a trade-off:headmay save you some time and resources but (as a separate process) it needs time and resources to be spawned twice in every directory.- Paths with newlines (if any) will fool
wc -lbut it doesn't matter because these extra newlines may add to the count only if the "right" count is non-zero anyway, and we only need to know whether the result is zero or not.
answered Nov 27 at 10:14
Kamil Maciorowski
22.9k155072
22.9k155072
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1378433%2ffind-all-directories-which-has-only-one-type-of-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
Please add an OS tag. For Linux: I'd probably run
find, get candidate directories which at least contain this type of file, and post-process the output via a script that checks the "only" condition.– dirkt
Nov 26 at 11:32