Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to list all the files from dir1, dir2, dir3 and dir4 which might be anywhere in as a sub directory of my cwd using the find command. I tried the following with no success:

find . -type f -regextype posix-egrep -regex 'dir1/.+|dir2/.+|dir3/.+|dir4/.+'

I tried posix-extended as well. How can I list these files?

share|improve this question
up vote 15 down vote accepted

And if you want to search three folders named foo, bar, and baz for all *.py files, use this command:

find foo bar baz -name "*.py"

so if you want to display files from dir1 dir2 dir3 use find dir1 dir2 dir3 -type f

try this find . \( -name "dir1" -o -name "dir2" \) -exec ls '{}' \;

share|improve this answer
I should have clarified. I might not have the path to the files I wish to search. I only know they are sub directories of the current dir. – Aaron Jan 10 '13 at 6:21
use ./dir instead of dir – harish.venkat Jan 10 '13 at 6:24
The dir would still need to be in the cwd though, it may be two levels lower (e.g. ./path/to/dir1), in which case ./dir1 would not exist. – Aaron Jan 10 '13 at 6:28
edited the answer. – harish.venkat Jan 10 '13 at 6:46

Just to let everyone know. Adding .*/ before each dir solved the problem since the regex is matching against the full path it seems.

share|improve this answer

It's best to use the -path directive:

find .  \( -type f -and -path '*/dir1/*' -or -path '*/dir2/*' -or -path '*/dir3/*' -or -path '*/dir4/*' \)

Which means: find all files under current directory where there's 'dir1' or 'dir2' or 'dir3' or 'dir4' in the path.

share|improve this answer

Sample of fast(!) finding particular files (access_log) in multiple locations defined by wildcard (home directory) and general apache2 log directory applying also name filtering to exclude ssl logs and gzipped old logs. Does not directly answer the question here but might be userful for someone who found these instructions (like me).

find / \( -path "*var/log/apache2*" -o -path "*home/*/logs*" \) -type f  -name "*access_log" ! -name "*ssl*"

Be careful with spaces, specially near to \( and \).

By the way, I used it for apachetop, to find most active websites in my server.

apachetop -d1 -s9 -p $(find / \( -path "*var/log/apache2*" -o -path "*home/*/logs*" \) -type f  -name "*access_log" ! -name "*ssl*" -print | sed 's/^/-f '/)
share|improve this answer

This is my first idea after reading the previous answers:

find . -type f -regextype posix-egrep -regex ".*/(dir1|dir2|dir3|dir4)/.+"

This takes into account, that the regex must match the whole filename, and it is easier to understand.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.