Julep
Julep

Reputation: 790

How to make explicit the arguments returned by pipe | to use them in subsequent command

I have a directory tree looking like this:

dir1
  |---dir2-1
  |---dir2-2
  |---dir2-3
  |--- ...

I simply want to create a new directory say dir3-1 under each of the current leaves dir2-X, resulting in:

dir1
  |---dir2-1
        |---dir3-1
  |---dir2-2
        |---dir3-1
  |---dir2-3
        |---dir3-1
  |--- ...

Can I do that with pipe | ? I naively tried to make explicit the arguments returned by the pipe by using $1 to subsequently use them with mkdir but it doesn't seem to be the right syntax.

The spirit of what I want to do is: find . -name dir2-* | xargs -n1 mkdir $1/dir3-1.

Upvotes: 0

Views: 93

Answers (2)

wjandrea
wjandrea

Reputation: 32921

A pipe doesn't pass any parameters; it takes the stdout from the first command and connects it to the stdin of the second command.

With xargs, you can specify a replacement field by using the -I option (see KamilCuk's answer), but it's easier to use find -exec, which uses {} as the replacement field. Something like this:

find . -name 'dir2-*' -exec mkdir {}/dir3-1 \;

Upvotes: 5

KamilCuk
KamilCuk

Reputation: 140880

Sure, why not:

find ./dir1 -maxdepth 2 -type d -name 'dir2-*' | xargs -d '\n' -I {} mkdir {}/dir3-1

Upvotes: 1

Related Questions