8

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 ?

9

launching a subshell will do what you want:

find /home/A/bin/* -print |
xargs -I {} sh -c 'ln -sf "$1" /tmp/B/$(basename "$1")' - {}
3
  • 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. Feb 7 '14 at 20:41
  • Do any of the $s need to be escaped?
    – Nic
    Dec 20 '18 at 3:18
  • No, because they all appear within single quotes. They will not be expanded until sh is executing it. Dec 20 '18 at 16:38
5
mkdir A ; touch A/file1 ; touch A/file2
mkdir B
for i in `ls A`; do ln -sf $PWD/A/$i B/; done
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
1
  • thnx for the answer. That worked like a charm. but how can i execute any sub command inside the xargs ??
    – JohnG
    Nov 24 '12 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy