Given the folder structure
/recipes/foo/.env
/recipes/bar/.env
Running the following command:
find ./recipes -type f -name '.env' -print0 | xargs -0 dirname | xargs -0 basename
Output:
foo
Trimming the command down to:
find ./recipes -type f -name '.env' -print0 | xargs -0 dirname
Output:
./recipes/bar
./recipes/foo
So for some reason piping dirname to basename causes me to lose some of the found directories.
>Solution :
There are 2 issues here:
-
The
-0option ofxargsindicates that the inputs are NUL-separated, not the outputs. In order to pipe the output ofxargs -0 dirnametoxargs -0 somethingyou must use the-zoption ofdirname. Else, its output is newline-separated. -
basenamedoes not support multiple arguments by default. If yours supports it you can try-aor--multiple.
find ./recipes -type f -name '.env' -print0 |
xargs -0 dirname -z | xargs -0 basename -a