Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers

up vote 1 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 at 6:21
use ./dir instead of dir – harish.venkat Jan 10 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 at 6:28
edited the answer. – harish.venkat Jan 10 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

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

 
discard

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.