Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Directory goes missing when piping basename from dirname

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  1. The -0 option of xargs indicates that the inputs are NUL-separated, not the outputs. In order to pipe the output of xargs -0 dirname to xargs -0 something you must use the -z option of dirname. Else, its output is newline-separated.

  2. basename does not support multiple arguments by default. If yours supports it you can try -a or --multiple.

find ./recipes -type f -name '.env' -print0 |
  xargs -0 dirname -z | xargs -0 basename -a
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading