0

Say the folder structure looks like this:

.
|--folder1
   |--subfolder1
      |--subfolder2
   |--subfolder2
|--folder2
   |--subfolder1
      |--subfolder2
   |--subfolder2
|--folder3
   |--subfolder1
   |--subfolder2

I would like to find all files in subfolder2 only. I know I can just do this:

$ find . -type f |grep subfolder2

But was wondering if find comes with a option to include/exclude given directories?

1 Answer 1

1

Just name all the directories you want to search.

find */subfolder2 -type f

Or for a completely arbitrary directory structure, something like this...

find $(find . -name subfolder2 -type d) -type f
6
  • I edited the original folder structure. Your command only work for same subfolder depth. Is there any way to search subfolder2 no matter which hierarchy it's?
    – Stan
    Nov 3, 2013 at 5:11
  • @stan Yes, see my edit. Nov 3, 2013 at 5:16
  • Your 2nd command got an illegal variable name. This one find `find . -name subfolder2 -type d` -type f works for me. thanks.
    – Stan
    Nov 3, 2013 at 5:32
  • @Stan You probably aren't using a modern shell like bash, then. The old-style backticks should be fine. Nov 3, 2013 at 5:36
  • I am using tcsh in CentOS 5.7. Is that not a modern shell?
    – Stan
    Nov 3, 2013 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .