ThaMe90
ThaMe90

Reputation: 4296

How to create symlinks in a specific directory

I'm working on an automated installation of a openSUSE system using AutoYAST, and I'm stumped on a small detail. In order to setup relevant applications in the user's environment, I try to symlink to all applications located in /usr/local/bin in ~/bin (so say /usr/local/bin has the addr2line utility, then I want to have a symlink to that in ~/bin).

I've tried to execute the following snipped to accomplish this:

su -c "for program in `ls /usr/local/bin`; do ln -s /usr/local/bin/$program ~/bin/$program; done" <user>

This snippet executes in the post-script phase of the automatic installation, which is executed as root (and seeing as I want the owner of the symlinks to be the user, this command is executed using su).

However, this does not work, and gives the following output:

++ ls /usr/local/bin
+ su -c 'for program in addr2line
ar
as
c++
c++filt
cpp
elfedit
g++
gcc
gcc-ar
gcc-nm
gcc-ranlib
gcov
gprof
i686-pc-linux-gnu-c++
i686-pc-linux-gnu-g++
i686-pc-linux-gnu-gcc
i686-pc-linux-gnu-gcc-4.9.3
i686-pc-linux-gnu-gcc-ar
i686-pc-linux-gnu-gcc-nm
i686-pc-linux-gnu-gcc-ranlib
ld
ld.bfd
nm
objcopy
objdump
ranlib
readelf
size
strings
strip; do ln -s /usr/local/bin/ ~/bin/; done' <user>
bash: -c: line 1: syntax error near unexpected token `ar'
bash: -c: line 1: `ar'

I've tried several variations of the command, but all seem to not exactly do what I want.

For example, I've also tried:

su -c "for program in /usr/local/bin/*; do ln -s $program ~/bin/; done" <user>

But this only created a symlink to /usr/local/bin in ~/bin. So I'm a bit stuck on this one... Does anybody have an idea?

Upvotes: 1

Views: 146

Answers (3)

dannysauer
dannysauer

Reputation: 3867

You can also use cp -s to create symlinks on a system with GNU cp (like your suse system), which gives you the ability to use recursion and the other fun options of cp.

Upvotes: 1

ThaMe90
ThaMe90

Reputation: 4296

In the end, I decided to go with the command posted by pacholik to fix this, as my original attempt was over-engineered and thus not necessary.

ln -s /usr/local/bin/* ~/bin

Upvotes: 0

Steve Vinoski
Steve Vinoski

Reputation: 20014

You're using double quotes to define your su command, so $program is being evaluated immediately. You want it evaluated when su executes the command. Use single quotes instead:

su -c 'for program in `ls /usr/local/bin`; do ln -s /usr/local/bin/$program ~/bin/$program; done' <user>

Upvotes: 2

Related Questions