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?


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.

  • yea. i gave the same answer here – Prince John Wesley May 18 '12 at 15:02
  • Or find X -name 'inc' -type d > list – MaQleod May 18 '12 at 15:06
  • @MaQleod: No, that is not the question. – Daniel Andersson May 18 '12 at 15:11

Here's a handy one-liner:

find X -type d -name "inc" -exec sh -c 'find {} -type d' \; > list

It runs find on each of the first find results. The exec option can also take a minimal shell command, in which – as I said – {} is replaced with each directory of the first find.

The second find will, per your request, "list all subdirectories" of the first results, including the inc directory. If you don't want that itself in the output, let the second find at least output folders of depth 1.

find X -type d -name "inc" -exec sh -c 'find {} -mindepth 1 -type d' \; > list

We'll then just redirect the command's stdout into list.

  • a variation on that answer was helpful to me where I only wanted depth 1 sub directories of the original match find path -name inc -type d -exec sh -c 'find {} -mindepth 1 -maxdepth 1 -type d' \; – Core Feb 13 '18 at 0:08

Alright I have found the answer to simulate this nested find:

find X/ -type d | grep "/inc/" > list
  • Nice, that'll work, too! However, in Unix/Linux shells, there's really no such thing as the answer. It's a mixture of personal preferences and what's easy to follow. – Vishal Kotcherlakota May 18 '12 at 6:47
  • To take care of unconventional (but existing) file names with e.g. newlines: find X/ -type d -print0 | awk 'BEGIN{RS="\0"}/\/inc\//' > list (grep -z also delimits on the null character, but its output seems to be null separated as well, so then one would need to pipe it through e.g. xargs -0L1 echo to get it line by line). In this case, since you are saving the list to file with every entry separated by newline, newlines in file names will still be ambiguously represented. Perhaps you do not even need the intermediate file, depending on what you want to do with the list. – Daniel Andersson May 18 '12 at 10:55

Try this:

   find path-of-x -path '*/inc/*' -type d > list
  • i don't have linux machine to test it now... – Prince John Wesley May 18 '12 at 6:36
  • This doesn't work. But thanks for suggesting me "\inc\" :) – iammilind May 18 '12 at 6:40
  • @iammilind: try with -path flag – Prince John Wesley May 18 '12 at 6:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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