I have these results from find:

$ find subprojects -mindepth 1 -maxdepth 1


I want to map these results to:


(All I am doing in this case is appending "/test.sh" to the results... I am sure a good solution is something like:

$ find subprojects -mindepth 1 -maxdepth 1 | something (?)

but I don't know what it would be! Pretty newby here. Probably more than one way to do it, looking for simplest most robust solution I guess.

Note that since the test.sh files already exist in these paths, I could just to do this:

find subprojects -mindepth 2 -maxdepth 2 -name "test.sh"

But I guess I am looking for away to do that, assuming these test.sh files don't exist yet on the filesystem.

  • 1
    Since you don't appear to want to search subdirectories, you don't really need find (at least, not unless you are doing something more complicated than printing the strings). For example, you could just do printf '%s/test.sh\n' subprojects/* – steeldriver Dec 23 '16 at 13:33
  • "Probably more than one way to do it, looking for simplest most robust solution I guess." What is your goal here ? What are you actually trying to do ? – don_crissti Dec 23 '16 at 13:50
  • @don_crissti my use case is simple here, just trying to get the path to a particular file, but pretending that file doesn't exist yet on the filesystem. – Alexander Mills Dec 23 '16 at 18:52
  • @don_crissti, the two answers already present are on the right track if that helps, I am open to any solution that works, this is mostly educational – Alexander Mills Dec 23 '16 at 19:14
  • Since you're using gnu find you don't need any post-processing with sed, echo and whatnot; you can simply do find subprojects -mindepth 1 -maxdepth 1 -printf '%p/test.sh\n' (or, if you want them separated by a single space replace the \n with a space). Other than that I don't know what to say because I really don't understand the purpose of this exercise. – don_crissti Dec 23 '16 at 19:18

You can use sed to "edit" each line by appending your required suffix:

find subprojects -mindepth 1 -maxdepth 1 | sed 's!$!/test.sh!'

The normal separator for sed is / but I've used ! because the substitution already contains / and I didn't want to end up with \/ visual constructs in the result (sed 's/$/\/test.sh/').

If you're looking for files that might exist, or don't yet but will, you probably should constrain your find to pick out only directories, with find subprojects -mindepth 1 -maxdepth 1 -type d

In the comments you also then ask how to put this into a single line. Provided none of your arguments contains whitespace (subprojects/some where/test.sh, for example) you can pipe the result into xargs.

  • thanks, that seems to work...also an aside - how can take the find results and change them from newline separated to just single space separated? – Alexander Mills Dec 23 '16 at 9:55

To add soemthing in the end a simple echo could also work:

find subprojects -mindepth 1 -maxdepth 1 -exec echo {}/test.sh \;
#OR to have them in variables
IFS=$'\n';ff=($(find subprojects -mindepth 1 -maxdepth 1 -exec echo {}/test.sh \;));echo ${ff[@]}

If you prefer using GNU Parallel:

find subprojects -mindepth 1 -maxdepth 1 | parallel echo {}/test.sh

This will also make it easy to execute them:

find subprojects -mindepth 1 -maxdepth 1 | parallel {}/test.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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