Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a symbolic link in a bash script in a different folder

Tags:

bash

My bash script is getting two arguments with folders (that exist and everything).

Inside the first one I want to create a link to the second

Suppose I have the folders /home/matt/a and /home/matt/b, I call the script like this :

/home/matt # ./my_script ./a ./b

I want to see a symbolic link inside a that points to b

And of course, just doing

ln -s $2 $1/link

in the script does not work... (it will create a link that looks for a ./b inside a)

This is just a very simple example, I am looking for a script that will be generic enough to take different arguments (absolute or relative path...etc...)

like image 646
Matthieu Avatar asked Oct 05 '10 08:10

Matthieu


2 Answers

Give this a try:

ln -s "$(readlink -e "$2")" "$1/link"

if you have readlink.

Or perhaps this variation on the answer by larsmans:

cd "$2"
dir=$(pwd)
cd -
ln -s "$dir" "$1/link"
like image 95
Dennis Williamson Avatar answered Oct 19 '22 04:10

Dennis Williamson


#!/bin/sh
cd $2
ln -s "`pwd`" $1/link
like image 35
Fred Foo Avatar answered Oct 19 '22 05:10

Fred Foo