execute sub command within xargs

7

1

I want to make soft link of all the binary files of folder A to folder B like,

find /home/A/bin/* -print | xargs -I {} ln -sf {} /tmp/B/$(basename {})

the problem is that I cant execute sub command inside the xargs.

what should I do ?

JohnG

Posted 2012-11-24T08:16:27.377

Reputation:

Answers

8

launching a subshell will do what you want:

find /home/A/bin/* -print |
xargs -I {} sh -c 'ln -sf "$1" /tmp/B/$(basename "$1")' - {}

glenn jackman

Posted 2012-11-24T08:16:27.377

Reputation: 18 546

Do any of the $s need to be escaped? – Fund Monica's Lawsuit – 2018-12-20T03:18:21.020

No, because they all appear within single quotes. They will not be expanded until sh is executing it. – glenn jackman – 2018-12-20T16:38:38.047

This is the real answer to the question. @alinsoar may have showed a better way to do what the OP wanted to do, but this is how you do what he asked for. – ACK_stoverflow – 2014-02-07T20:41:55.613

5

mkdir A ; touch A/file1 ; touch A/file2
mkdir B
for i in `ls A`; do ln -sf $PWD/A/$i B/; done

Amir Naghizadeh

Posted 2012-11-24T08:16:27.377

Reputation: 153

3

You can execute directly ln -sf /h/a/bin/* /tmp .

Or, you can go to /tmp, and do so:

cd /tmp
ln -sf /home/A/bin/*

Using xargs:

cd /tmp
find /home/A/bin/* -print0 | xargs -0 ln

alinsoar

Posted 2012-11-24T08:16:27.377

Reputation:

thnx for the answer. That worked like a charm. but how can i execute any sub command inside the xargs ?? – None – 2012-11-24T08:25:44.030