user2271842
user2271842

Reputation: 1

When creating symbolic links on ubuntu I sometimes get an odd result

I'm trying to create a bunch of symbolic links for all the files in a directory. It seems like, when I type this command in the shell manually, it works just fine, but when I run it in a shell script, or even use the up arrow to re-run it, I get the following problem.

$ sudo ln -s /path/to/my/files/* /the/target/directory/

This should create a bunch of sym links in /path/to/my/files and if I type the command in manuall, it indeed does, however, when I run the command from a shell script, or use the up arrow to re-run it I get a single symbolic link in /the/target/directory/ called * as in the link name is actually '*' and I then have to run

$ sudo rm * 

To delete it, which just seems insane to me.

Upvotes: 0

Views: 91

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

When you run that command in the script, are there any files in /path/to/my/files? If not, then by default the wildcard has nothing to expand to, and it is not replaced. You end up with the literal "*". You might want to check out shopt -s nullglob and run the ln command like this:

shopt -s nullglob
sudo ln -s -t /the/target/directory /path/to/my/files/* 

Upvotes: 1

Mark
Mark

Reputation: 7615

Maybe the script uses sh and your using bash when executing the command.

You may try something like this:

for file in $(ls /path/to/my/files/*) do ln -s "${file}" "/the/target/directory/"${file}" done

Upvotes: 0

Related Questions