Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The example below shows what I want to do, namely take the result from my last pipe and put it into the middle of the next command (where I wrote $RESULT_FROM_FIND_COMMAND).

$ find . -name "*0.01*txt" | cp $RESULT_FROM_FIND_COMMAND $(awk '{{split($1,a,"/")} {print a[3]"_"a[4]"_"a[5]}}')

What variable or command must I use to achieve this?

I'm using bash if that matters.

Ps. note that awk should also take the result from find as an input.

share|improve this question
Could probably do this with a for i in $(find . -name "*0.01*txt") do ..., but I would like a version that follows the format above. – The Unfun Cat Jun 23 '13 at 7:52
What about find . -name "*0.01*txt" | awk '{split($1,a,"/"); print "echo cp "$1" "a[3]"_"a[4]"_"a[5]}' | sh? (If satisfied by the output, remove the “echo” to perform copying.) – manatwork Jun 23 '13 at 9:18
What's wrong with -exec on the find? – Mel Boyce Jun 23 '13 at 9:19
up vote 3 down vote accepted

In this case, use the shell to iterate

find . -name "*0.01*txt" | while IFS= read -r filename; do
    newname=$(awk '{split($1,a,"/"), print a[3]"_"a[4]"_"a[5]}' <<< "$filename")"
    cp "$filename" "$newname"

In general, xargs is helpful:

find .... -print0 | xargs -o -I FILE someCommand arg1 FILE arg3 FILE
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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