Find all files in directories named fooLimit POSIX find to specific depth?symbolic link to a directory and relative pathRecursive find that does not find hidden files or recurse into hidden dirsHow can I efficiently dereference all symlinks in `find` *output* filenames?Copying Only Directories With FilesIdentify sub-directories that do not contain a specific string in a specific fileFind files in globbed directories excluding some subpathsfind: exclude n different directories and m different files present at any level but include few files from some excluded directoriesHow to use 'find' and 'cpio' to exclude parent directoriesZipping all sub directories
What's the purpose of autocorrelation?
Is there an in-universe reason Harry says this or is this simply a Rowling mistake?
Removing rows containing NA in every column
Exam design: give maximum score per question or not?
Is the name of an interval between two notes unique and absolute?
How does one calculate the distribution of the Matt Colville way of rolling stats?
Why do things cool down?
Minimize taxes now that I earn more
Is there any actual security benefit to restricting foreign IPs?
The relationship of noch nicht and the passive voice
Dear Fellow PSE Users,
Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?
How could artificial intelligence harm us?
Why are two-stroke engines nearly unheard of in aviation?
Manager manipulates my leaves, what's in it for him?
Find all files in directories named foo
Are lay articles good enough to be the main source of information for PhD research?
How do rulers get rich from war?
Should I inform my future product owner that there is a good chance that a team member will leave the company soon?
4h 40m delay caused by aircraft inspection, Norwegian refuses EU 261/2004 compensation because it turned out there was nothing wrong with the aircraft
Can Brexit be undone in an emergency?
I reverse the source code, you negate the input!
Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?
Escape the labyrinth!
Find all files in directories named foo
Limit POSIX find to specific depth?symbolic link to a directory and relative pathRecursive find that does not find hidden files or recurse into hidden dirsHow can I efficiently dereference all symlinks in `find` *output* filenames?Copying Only Directories With FilesIdentify sub-directories that do not contain a specific string in a specific fileFind files in globbed directories excluding some subpathsfind: exclude n different directories and m different files present at any level but include few files from some excluded directoriesHow to use 'find' and 'cpio' to exclude parent directoriesZipping all sub directories
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to find all files which reside in any directory foo
. For example, consider the following files:
foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n
I would like to find the files w,x,y,z
and get them listed as above, i.e., with their relative paths. My attempt was something like
$ find . -path '**/foo' -type f
which doesn't work. The search should not include file n
, i.e., only files whose parent directory is named foo
we are interested in.
find directory
add a comment
|
I would like to find all files which reside in any directory foo
. For example, consider the following files:
foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n
I would like to find the files w,x,y,z
and get them listed as above, i.e., with their relative paths. My attempt was something like
$ find . -path '**/foo' -type f
which doesn't work. The search should not include file n
, i.e., only files whose parent directory is named foo
we are interested in.
find directory
add a comment
|
I would like to find all files which reside in any directory foo
. For example, consider the following files:
foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n
I would like to find the files w,x,y,z
and get them listed as above, i.e., with their relative paths. My attempt was something like
$ find . -path '**/foo' -type f
which doesn't work. The search should not include file n
, i.e., only files whose parent directory is named foo
we are interested in.
find directory
I would like to find all files which reside in any directory foo
. For example, consider the following files:
foo/w
a/foo/x
a/b/foo/y
a/b/c/foo/z
a/b/c/foo/bar/n
I would like to find the files w,x,y,z
and get them listed as above, i.e., with their relative paths. My attempt was something like
$ find . -path '**/foo' -type f
which doesn't work. The search should not include file n
, i.e., only files whose parent directory is named foo
we are interested in.
find directory
find directory
edited 8 hours ago
Max Maier
asked 9 hours ago
Max MaierMax Maier
1183 bronze badges
1183 bronze badges
add a comment
|
add a comment
|
6 Answers
6
active
oldest
votes
Not using find
, but globbing in the zsh
shell:
$ printf '%sn' **/foo/*(^/)
a/b/c/foo/z
a/b/foo/y
a/foo/x
foo/w
This uses the **
glob, which matches "recursively" down into directories, to match any directory named foo
in the current directory or below, and then *(^/)
to match any file in those foo
directories. The (^/)
at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.)
to match regular files only, or (-.)
to also include symbolic links to regular files).
In bash
:
shopt -s globstar nullglob
for pathname in **/foo/*; do
if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
printf '%sn' "$pathname"
fi
done
I set the nullglob
option in bash
to remove the pattern completely in case it does not match anything. The globstar
shell option enables the use of **
in bash
(this is enabled by default in zsh
).
Since bash
does not have the glob qualifiers of zsh
, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h
" test to a "-f && ! -h
" test to instead pick out only regular files, or just a single "-f
" test to also print symbolic links to regular files.
Note that[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be(-.)
.
– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
add a comment
|
I'm on RHEL 7. This works for me:
find . -path "*/foo/*" ! -path '*/foo/*/*' -type f
Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)
The DOT says "Start looking in the current directory"
the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".
The -type f says give me only the files in a matching directory.
Looks like
-path "*/foo/*" ! -path '*/foo/*/*'
Doesn't get everything. Not elegant, but it works:
find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0'
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory namedfoo
. However, I'm interested only in files whose parent is namedfoo
.
– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
Note:a/foo/c/foo/z
should be found.
– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
add a comment
|
One method is to find all files, then use grep
to match only the desired results:
find . -type f | grep '/foo/[^/]*$'
add a comment
|
**
has no special meaning in find
patterns. -path '*/foo/*'
would find all files under a directory called foo
, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*'
would exclude files like a/foo/b/foo/c
. I don't think you can do this with just one invocation of POSIX find
.
With find
implementations that support -regex
(GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single /
after foo
.
find . -regex '.*/foo/[^/]*' -type f
Alternatively, you can use find
to locate the foo
directories and a shell to enumerate files in this directory.
Another potential approach would be to invoke find
again, but I can't find a pure POSIX find
solution that actually works. With any POSIX find
, invoked again for each foo
directory:
find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;
Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune
to avoid recursing into subdirectories, but with just -type d -prune
, find would stop at the foo
directory. ! -name foo
does not prune foo/foo
, so a file like foo/foo/bar
will be reported twice. You can't use -exec
in the inner find
because its would be interpreted by the outer
find
. If your find
has -maxdepth
(which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec
:
find . -name foo -type d -exec find -maxdepth 1 -type f ;
With any POSIX find and sh:
find . -name foo -type d -exec sh -c '
for x in "$0/"* "$0/".*; do
if [ -f "$x" ] && ! [ -L "$x" ]; then
…;
fi;
done
' ;
Substitute the code you want to run on the file names for …
.
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has+
. Here you could use*
instead.
– Stéphane Chazelas
6 hours ago
add a comment
|
The first thing that comes to mind is:
find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f
New contributor
Unfortunately, this also finds the filen
, which should be excluded since it lives inbar
, not infoo
.
– Kusalananda♦
8 hours ago
add a comment
|
Tried with Below approach
find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'
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/4.0/"u003ecc by-sa 4.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%2f542304%2ffind-all-files-in-directories-named-foo%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
Not using find
, but globbing in the zsh
shell:
$ printf '%sn' **/foo/*(^/)
a/b/c/foo/z
a/b/foo/y
a/foo/x
foo/w
This uses the **
glob, which matches "recursively" down into directories, to match any directory named foo
in the current directory or below, and then *(^/)
to match any file in those foo
directories. The (^/)
at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.)
to match regular files only, or (-.)
to also include symbolic links to regular files).
In bash
:
shopt -s globstar nullglob
for pathname in **/foo/*; do
if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
printf '%sn' "$pathname"
fi
done
I set the nullglob
option in bash
to remove the pattern completely in case it does not match anything. The globstar
shell option enables the use of **
in bash
(this is enabled by default in zsh
).
Since bash
does not have the glob qualifiers of zsh
, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h
" test to a "-f && ! -h
" test to instead pick out only regular files, or just a single "-f
" test to also print symbolic links to regular files.
Note that[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be(-.)
.
– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
add a comment
|
Not using find
, but globbing in the zsh
shell:
$ printf '%sn' **/foo/*(^/)
a/b/c/foo/z
a/b/foo/y
a/foo/x
foo/w
This uses the **
glob, which matches "recursively" down into directories, to match any directory named foo
in the current directory or below, and then *(^/)
to match any file in those foo
directories. The (^/)
at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.)
to match regular files only, or (-.)
to also include symbolic links to regular files).
In bash
:
shopt -s globstar nullglob
for pathname in **/foo/*; do
if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
printf '%sn' "$pathname"
fi
done
I set the nullglob
option in bash
to remove the pattern completely in case it does not match anything. The globstar
shell option enables the use of **
in bash
(this is enabled by default in zsh
).
Since bash
does not have the glob qualifiers of zsh
, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h
" test to a "-f && ! -h
" test to instead pick out only regular files, or just a single "-f
" test to also print symbolic links to regular files.
Note that[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be(-.)
.
– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
add a comment
|
Not using find
, but globbing in the zsh
shell:
$ printf '%sn' **/foo/*(^/)
a/b/c/foo/z
a/b/foo/y
a/foo/x
foo/w
This uses the **
glob, which matches "recursively" down into directories, to match any directory named foo
in the current directory or below, and then *(^/)
to match any file in those foo
directories. The (^/)
at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.)
to match regular files only, or (-.)
to also include symbolic links to regular files).
In bash
:
shopt -s globstar nullglob
for pathname in **/foo/*; do
if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
printf '%sn' "$pathname"
fi
done
I set the nullglob
option in bash
to remove the pattern completely in case it does not match anything. The globstar
shell option enables the use of **
in bash
(this is enabled by default in zsh
).
Since bash
does not have the glob qualifiers of zsh
, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h
" test to a "-f && ! -h
" test to instead pick out only regular files, or just a single "-f
" test to also print symbolic links to regular files.
Not using find
, but globbing in the zsh
shell:
$ printf '%sn' **/foo/*(^/)
a/b/c/foo/z
a/b/foo/y
a/foo/x
foo/w
This uses the **
glob, which matches "recursively" down into directories, to match any directory named foo
in the current directory or below, and then *(^/)
to match any file in those foo
directories. The (^/)
at the end of that is a glob qualifier that restricts the pattern from matching directories (use (.)
to match regular files only, or (-.)
to also include symbolic links to regular files).
In bash
:
shopt -s globstar nullglob
for pathname in **/foo/*; do
if [[ ! -d "$pathname" ]] || [[ -h "$pathname" ]]; then
printf '%sn' "$pathname"
fi
done
I set the nullglob
option in bash
to remove the pattern completely in case it does not match anything. The globstar
shell option enables the use of **
in bash
(this is enabled by default in zsh
).
Since bash
does not have the glob qualifiers of zsh
, I'm looping over the pathnames that the pattern matches and test each match to make sure it's not a directory before printing it. Change the "! -d || -h
" test to a "-f && ! -h
" test to instead pick out only regular files, or just a single "-f
" test to also print symbolic links to regular files.
edited 5 hours ago
answered 8 hours ago
Kusalananda♦Kusalananda
165k19 gold badges322 silver badges511 bronze badges
165k19 gold badges322 silver badges511 bronze badges
Note that[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be(-.)
.
– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
add a comment
|
Note that[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be(-.)
.
– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
Note that
[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.)
.– Stéphane Chazelas
6 hours ago
Note that
[[ -f file ]]
matches on regular files and symlinks to regular files. The zsh qualifier equivalent would be (-.)
.– Stéphane Chazelas
6 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
@StéphaneChazelas Thanks! I just added a note about symbolic links (maybe it would be even better to look for non-directories), and I agree that getting a "no matches found" error may be more informative than getting an empty line.
– Kusalananda♦
5 hours ago
add a comment
|
I'm on RHEL 7. This works for me:
find . -path "*/foo/*" ! -path '*/foo/*/*' -type f
Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)
The DOT says "Start looking in the current directory"
the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".
The -type f says give me only the files in a matching directory.
Looks like
-path "*/foo/*" ! -path '*/foo/*/*'
Doesn't get everything. Not elegant, but it works:
find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0'
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory namedfoo
. However, I'm interested only in files whose parent is namedfoo
.
– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
Note:a/foo/c/foo/z
should be found.
– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
add a comment
|
I'm on RHEL 7. This works for me:
find . -path "*/foo/*" ! -path '*/foo/*/*' -type f
Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)
The DOT says "Start looking in the current directory"
the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".
The -type f says give me only the files in a matching directory.
Looks like
-path "*/foo/*" ! -path '*/foo/*/*'
Doesn't get everything. Not elegant, but it works:
find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0'
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory namedfoo
. However, I'm interested only in files whose parent is namedfoo
.
– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
Note:a/foo/c/foo/z
should be found.
– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
add a comment
|
I'm on RHEL 7. This works for me:
find . -path "*/foo/*" ! -path '*/foo/*/*' -type f
Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)
The DOT says "Start looking in the current directory"
the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".
The -type f says give me only the files in a matching directory.
Looks like
-path "*/foo/*" ! -path '*/foo/*/*'
Doesn't get everything. Not elegant, but it works:
find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0'
I'm on RHEL 7. This works for me:
find . -path "*/foo/*" ! -path '*/foo/*/*' -type f
Note the DOT before the -path. (Or substitute your path there, such as /home/$USER)
The DOT says "Start looking in the current directory"
the -path says "Look for anything, followed by a sub-directory named foo, followed by anything" except for directories nested under "foo".
The -type f says give me only the files in a matching directory.
Looks like
-path "*/foo/*" ! -path '*/foo/*/*'
Doesn't get everything. Not elegant, but it works:
find . -path "*/foo/*" -type f | awk -F'/' 'if (match$(NF-1),"foo")) print $0'
edited 5 hours ago
answered 9 hours ago
Scottie HScottie H
1336 bronze badges
1336 bronze badges
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory namedfoo
. However, I'm interested only in files whose parent is namedfoo
.
– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
Note:a/foo/c/foo/z
should be found.
– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
add a comment
|
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory namedfoo
. However, I'm interested only in files whose parent is namedfoo
.
– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
Note:a/foo/c/foo/z
should be found.
– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named
foo
. However, I'm interested only in files whose parent is named foo
.– Max Maier
8 hours ago
That is pretty close to what I'm locking for. I updated my question in order to be more precise. This solution finds any file whose path contains a directory named
foo
. However, I'm interested only in files whose parent is named foo
.– Max Maier
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
Ah, sorry I missed that. I'll work on an update.
– Scottie H
8 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
I think @Jeff added that. Thanks for that!
– Scottie H
7 hours ago
3
3
Note:
a/foo/c/foo/z
should be found.– Kamil Maciorowski
7 hours ago
Note:
a/foo/c/foo/z
should be found.– Kamil Maciorowski
7 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
Just updated the answer to show that.
– Scottie H
5 hours ago
add a comment
|
One method is to find all files, then use grep
to match only the desired results:
find . -type f | grep '/foo/[^/]*$'
add a comment
|
One method is to find all files, then use grep
to match only the desired results:
find . -type f | grep '/foo/[^/]*$'
add a comment
|
One method is to find all files, then use grep
to match only the desired results:
find . -type f | grep '/foo/[^/]*$'
One method is to find all files, then use grep
to match only the desired results:
find . -type f | grep '/foo/[^/]*$'
answered 8 hours ago
Jim L.Jim L.
2,2331 gold badge4 silver badges12 bronze badges
2,2331 gold badge4 silver badges12 bronze badges
add a comment
|
add a comment
|
**
has no special meaning in find
patterns. -path '*/foo/*'
would find all files under a directory called foo
, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*'
would exclude files like a/foo/b/foo/c
. I don't think you can do this with just one invocation of POSIX find
.
With find
implementations that support -regex
(GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single /
after foo
.
find . -regex '.*/foo/[^/]*' -type f
Alternatively, you can use find
to locate the foo
directories and a shell to enumerate files in this directory.
Another potential approach would be to invoke find
again, but I can't find a pure POSIX find
solution that actually works. With any POSIX find
, invoked again for each foo
directory:
find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;
Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune
to avoid recursing into subdirectories, but with just -type d -prune
, find would stop at the foo
directory. ! -name foo
does not prune foo/foo
, so a file like foo/foo/bar
will be reported twice. You can't use -exec
in the inner find
because its would be interpreted by the outer
find
. If your find
has -maxdepth
(which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec
:
find . -name foo -type d -exec find -maxdepth 1 -type f ;
With any POSIX find and sh:
find . -name foo -type d -exec sh -c '
for x in "$0/"* "$0/".*; do
if [ -f "$x" ] && ! [ -L "$x" ]; then
…;
fi;
done
' ;
Substitute the code you want to run on the file names for …
.
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has+
. Here you could use*
instead.
– Stéphane Chazelas
6 hours ago
add a comment
|
**
has no special meaning in find
patterns. -path '*/foo/*'
would find all files under a directory called foo
, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*'
would exclude files like a/foo/b/foo/c
. I don't think you can do this with just one invocation of POSIX find
.
With find
implementations that support -regex
(GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single /
after foo
.
find . -regex '.*/foo/[^/]*' -type f
Alternatively, you can use find
to locate the foo
directories and a shell to enumerate files in this directory.
Another potential approach would be to invoke find
again, but I can't find a pure POSIX find
solution that actually works. With any POSIX find
, invoked again for each foo
directory:
find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;
Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune
to avoid recursing into subdirectories, but with just -type d -prune
, find would stop at the foo
directory. ! -name foo
does not prune foo/foo
, so a file like foo/foo/bar
will be reported twice. You can't use -exec
in the inner find
because its would be interpreted by the outer
find
. If your find
has -maxdepth
(which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec
:
find . -name foo -type d -exec find -maxdepth 1 -type f ;
With any POSIX find and sh:
find . -name foo -type d -exec sh -c '
for x in "$0/"* "$0/".*; do
if [ -f "$x" ] && ! [ -L "$x" ]; then
…;
fi;
done
' ;
Substitute the code you want to run on the file names for …
.
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has+
. Here you could use*
instead.
– Stéphane Chazelas
6 hours ago
add a comment
|
**
has no special meaning in find
patterns. -path '*/foo/*'
would find all files under a directory called foo
, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*'
would exclude files like a/foo/b/foo/c
. I don't think you can do this with just one invocation of POSIX find
.
With find
implementations that support -regex
(GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single /
after foo
.
find . -regex '.*/foo/[^/]*' -type f
Alternatively, you can use find
to locate the foo
directories and a shell to enumerate files in this directory.
Another potential approach would be to invoke find
again, but I can't find a pure POSIX find
solution that actually works. With any POSIX find
, invoked again for each foo
directory:
find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;
Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune
to avoid recursing into subdirectories, but with just -type d -prune
, find would stop at the foo
directory. ! -name foo
does not prune foo/foo
, so a file like foo/foo/bar
will be reported twice. You can't use -exec
in the inner find
because its would be interpreted by the outer
find
. If your find
has -maxdepth
(which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec
:
find . -name foo -type d -exec find -maxdepth 1 -type f ;
With any POSIX find and sh:
find . -name foo -type d -exec sh -c '
for x in "$0/"* "$0/".*; do
if [ -f "$x" ] && ! [ -L "$x" ]; then
…;
fi;
done
' ;
Substitute the code you want to run on the file names for …
.
**
has no special meaning in find
patterns. -path '*/foo/*'
would find all files under a directory called foo
, including files in subdirectories. -path '*/foo/*' ! -path '*/foo/*/*'
would exclude files like a/foo/b/foo/c
. I don't think you can do this with just one invocation of POSIX find
.
With find
implementations that support -regex
(GNU, BusyBox, FreeBSD, NetBSD), you can use that to ensure that there's a single /
after foo
.
find . -regex '.*/foo/[^/]*' -type f
Alternatively, you can use find
to locate the foo
directories and a shell to enumerate files in this directory.
Another potential approach would be to invoke find
again, but I can't find a pure POSIX find
solution that actually works. With any POSIX find
, invoked again for each foo
directory:
find . -name foo -type d -exec find -type d ! -name foo -prune -o -type f ;
Beware that this mostly works, but not quite, and it's a little fragile. You need -type d -prune
to avoid recursing into subdirectories, but with just -type d -prune
, find would stop at the foo
directory. ! -name foo
does not prune foo/foo
, so a file like foo/foo/bar
will be reported twice. You can't use -exec
in the inner find
because its would be interpreted by the outer
find
. If your find
has -maxdepth
(which is being considered for inclusion in the next version of POSIX), you can make this reliable, but there's still this limitation against -exec
:
find . -name foo -type d -exec find -maxdepth 1 -type f ;
With any POSIX find and sh:
find . -name foo -type d -exec sh -c '
for x in "$0/"* "$0/".*; do
if [ -f "$x" ] && ! [ -L "$x" ]; then
…;
fi;
done
' ;
Substitute the code you want to run on the file names for …
.
edited 5 hours ago
answered 7 hours ago
GillesGilles
575k140 gold badges1188 silver badges1700 bronze badges
575k140 gold badges1188 silver badges1700 bronze badges
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has+
. Here you could use*
instead.
– Stéphane Chazelas
6 hours ago
add a comment
|
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has+
. Here you could use*
instead.
– Stéphane Chazelas
6 hours ago
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has
+
. Here you could use *
instead.– Stéphane Chazelas
6 hours ago
In BSD find, -regex does BRE unless you use -E. GNU find does emacs RE by default which has
+
. Here you could use *
instead.– Stéphane Chazelas
6 hours ago
add a comment
|
The first thing that comes to mind is:
find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f
New contributor
Unfortunately, this also finds the filen
, which should be excluded since it lives inbar
, not infoo
.
– Kusalananda♦
8 hours ago
add a comment
|
The first thing that comes to mind is:
find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f
New contributor
Unfortunately, this also finds the filen
, which should be excluded since it lives inbar
, not infoo
.
– Kusalananda♦
8 hours ago
add a comment
|
The first thing that comes to mind is:
find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f
New contributor
The first thing that comes to mind is:
find $ROOT_PATH -type d -name foo | xargs -n1 -I find -type f
New contributor
edited 8 hours ago
Jeff Schaller♦
49.8k11 gold badges73 silver badges165 bronze badges
49.8k11 gold badges73 silver badges165 bronze badges
New contributor
answered 9 hours ago
erikjwaxxerikjwaxx
11 bronze badge
11 bronze badge
New contributor
New contributor
Unfortunately, this also finds the filen
, which should be excluded since it lives inbar
, not infoo
.
– Kusalananda♦
8 hours ago
add a comment
|
Unfortunately, this also finds the filen
, which should be excluded since it lives inbar
, not infoo
.
– Kusalananda♦
8 hours ago
Unfortunately, this also finds the file
n
, which should be excluded since it lives in bar
, not in foo
.– Kusalananda♦
8 hours ago
Unfortunately, this also finds the file
n
, which should be excluded since it lives in bar
, not in foo
.– Kusalananda♦
8 hours ago
add a comment
|
Tried with Below approach
find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'
add a comment
|
Tried with Below approach
find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'
add a comment
|
Tried with Below approach
find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'
Tried with Below approach
find . -type d -iname "*foo* -exec ls -ltr ; 2>/dev/null| awk '!/^d/||/^l/print $0'
answered 22 mins ago
Praveen Kumar BSPraveen Kumar BS
2,3962 gold badges3 silver badges11 bronze badges
2,3962 gold badges3 silver badges11 bronze badges
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%2f542304%2ffind-all-files-in-directories-named-foo%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