Linux – Finding subdirectories inside all directories with the same name

command linefindlinux

I want to run a command to:

  1. Find all directories named "inc" under a folder "X".
  2. List all the subdirectories under each "X/.../inc/".
  3. Redirect the output to a file named "list"

I tried various combinations of the below command, without success:

$ find X/ -name "inc" -print | xargs find {} -type d > list
find: path must precede expression

How can I do this?

Best Answer

find can do this all by itself:

find X -path '*/inc/*' -type d > list

Read the -path part of man find for more info.

As I mentioned quickly in a comment: if you store the directories line separated in a text file, directory names containing newlines won't be unambiguously representable. If you are certain that directories don't contain newlines, that's OK. Just a general remark.