I have a tree of parent folder and subfolders. The subdirectories have similar names. I would like to search for files stored in the sub-directories. How can I do that?

For example:

  Sub-Directory 1
    input save bad
  Sub-Directory 2
    input save bad
  Sub-Directory 3 
    input save bad

Each of the sub-directories contain three folders named (input, save and bad). I only would like to search for files within the bad subdirectories.

  • 2
    Please update the question with information about what you want to search for in those directories.
    – Kusalananda
    Commented Sep 19, 2018 at 12:08
  • 2
    Hi @James Hete. The question was not clear. I made few edits. Do you approve the revisions that I had made on the question? meaning are the edits correct and it really stands for the objective of the question? Kindly, what do you mean by "Main directory"? I assume it is the parent folder that include three sub folders? What are "input save bad" stands for ? are these files or sub-sub folders?
    – user88036
    Commented Sep 19, 2018 at 13:11
  • Does ls */*/bad/ work to your satisfaction?
    – DopeGhoti
    Commented Sep 19, 2018 at 16:13

2 Answers 2


Assuming you want to do a recursive search using find in each of the bad directories that are a level below directory called Main Directory:

find 'Main Directory'/*/bad ...rest of find options...

I've left the rest of the find options out because you never say what you want to search for.

find is able to take more than one directory as the starting point for its search. Here, we give it a filename globbing pattern that will be expanded by the shell to the paths of the various bad directories.

Assuming you just want to print the pathnames of all the (regular) files in or beneath those directories:

find 'Main Directory'/*/bad -type f

If the bad directories do not have any subdirectories that you need to look inside, and if the number of files that you need to process is not many thousands, you would be able to just use

'Main Directory'/*/bad/*

(where the final * is assumed to match the filenames of the files you need to process) with whatever utility you need to use. For example, with ls:

ls 'Main Directory'/*/bad/*

Your question is not clear, but I assume you would like to find files in the subdirectory "BAD", you can run this command in the main directory:

find . -maxdepth 2 -type d -name "BAD" 

or use

find . -name "*BAD*" -type f -o -name "*BAD*" -type d


the -o command ors the arguments after the filepath completely, such that find . -name "string" -type f -o -type d computes find . (-name "BAD" -type f) -o (-type d).

Most users prefer something that looks like:

find . -name  "*BAD*" -type f -o -name "*BAD*" -type d

which computes as

find . (-name  "*BAD*" -type f) -o (-name "*BAD*" -type d)

-name "BAD" searches for names that contain the string string anywhere in them

For simplicity:

find . -maxdepth 2 -o -name "*BAD*"
  • 1
    but what if "input" and "save" were also subdirectories? Nothing in the current answer limits the search to subdirectories named "BAD"
    – Jeff Schaller
    Commented Sep 19, 2018 at 12:34
  • updadte the answer
    – user311543
    Commented Sep 19, 2018 at 12:38
  • 2
    Just a separate side note: if you feel a question isn't yet clear, the right thing to do is to ask for clarification. I know you're a few points short of the ability to comment on any post, so next-best would be to wait for others to request the clarification (or, more specifically, for the OP to edit in such clarification).
    – Jeff Schaller
    Commented Sep 19, 2018 at 12:40
  • 1
    Understood! There's a lot to learn -- and I'm still learning! It just saves time and effort when we (ask) and answer questions that are clear and answerable.
    – Jeff Schaller
    Commented Sep 19, 2018 at 12:43
  • 1
    I think your answer is on its way to being usable; it looks like the last sentence got cut off? I'd suggest using "BAD" instead of "string" so that it directly answers the OP. Also, it seems you're trying to find "files -or directories" whose name contains the string, in which case I'd suggest simplifying it by removing the -type tests.
    – Jeff Schaller
    Commented Sep 19, 2018 at 12:56

You must log in to answer this question.

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