Why is my symbolic link creating a file and not a folder? - linux

I want a create a symbolic link to a folder. The follow command will create a file with the link name but I'm trying to link to the source folder. What am I doing wrong?
ln -s /Users/me/somefolder somefolder
This creates a file "somefolder" in my current directory. How do I create a symbolic link to the folder and it's contents?
Thanks!

You need to use absolute path names to create the links.
For example, I'm now at
$ pwd
/home/alex/my_folder
And I'm creating a symbolic link to the folder "directoryA" in a sub-directory under my pwd (present working directory):
$ ln -s $PWD/directoryA $PWD/temp/link_to_directoryA
In this case variable $PWD holds absolute path to my working directory.
You can surely use your absolute path without any variables like this:
$ ln -s /home/alex/my_folder/directoryA /home/alex/my_folder/temp/link_to_directoryA

You need to be inside the same directory where you create the symbolic link
For instance:
cd /Users/me
ln -s somefolder somefolderNewName

Not creating a directory is an expected behavior.
When you do
ls -ali
It should show something beginning with;
lrwxrwxrwx
In which "l" represents symlink and allows you to traverse using cd.
NOTICE: ln command will not complain when you provide an invalid source path. And this will result with an error message when you try cd in to that.

Late for the party..
This is what worked for me..
if you want to create a symbolic link from sourceFolder to destinationFolder
you should be inside the parent of the destinationFolder "parentOfDestinationFolder" while doing so.

I think you have what you want, you just don't know it. A link has an entry in the directory, just like data files or directories do. You can see this most clearly if you run ls -l in the directory where you're creating the link.
You can use your link as if it were a directory, e.g.:
$ cd somefolder
You might also like to know that if you change directory this way, the parent of somefolder will be the directory that contains the link. If you don't want that, use:
$ cd -P somefolder

Related

in git bash in windows ln -s /destinations/path/ link_name just copies the destination rather than creates a link

In Git bash i have run the following:
ln -s "//server/path/Resource/" test
When I check if it's worked:
ls -l
It appears as a folder rather than a symlink.
I am using windows and trying to create a symbolic link to a network location.
This is probably an easy fix but i just want a shortcut rather than copying a massive folder.
make sure your git config points symbolic links as true.
core.symlinks=false
make this as true as by default it is

creating symbolic link of file in a different directory in linux

trying to create a symbolic link to an xml file in a different directory. I am using the command in the current file directory.
ln -s file_name.xml /home/path_to_desired_symbolic_link_location/file_name.xml
The symbolic link is created but it is empty.
It's safe to use the absolute address if you want to link to the file in another directory. Like this:
ln -s /path a/file_name.xml /path b/file_name.xml

How do I make symbolic link but cd should show physical directory name

I have a folder in my home directory called ~/bestphotosever
So I make a symlink as follows:
cd ~
ln -s bestphotosever bpe
cd bpe
pwd
#-- here is what pwd shows
--> /home/myuser/bpe
#What I would like it to show is the physical name
--> /home/myuser/bestphotosever
Any ideas how to do this using 'ln' command.
You can use "pwd -P" instead, avoids all symlinks and prints the "real" path
edit: I just realized i might not have answered your question.
Are you sure "you want" a hardlink?
Some info on hard link vs symlink.
https://askubuntu.com/questions/210741/why-are-hard-links-not-allowed-for-directories
If you just want to get into the folder with a shorter name, either tabcompletion or aliases are your friends.

Following symlink to run from current directory

I have a script run.sh located somewhere on read only directory /install/app/release_1.0.0/ and a symbolic link to that script in the fully accessed directory /packages/app/. This script operates with files using relative paths. When I'm running this script using symbolic link it's not able to locate files because it's looking in the current directory of symbolic link. How can I force it to look into the current directory of link's target? Changing the script is not prefered.
Don't use a symlink, use a wrapper instead. Remove /packages/app/run.sh and create a new file at that location, with these contents:
#!/bin/sh
cd /install/app/release_1.0.0/
./run.sh
Mark it executable (chmod +x run.sh) and that should do it.

Behavior of cd/bash on symbolic links

Assume I have the folders ~/a/b in my home folder, and the folder b contains
a symbolic link to '..' named 'symlink'. Then I perform the following actions in
bash:
hm#mach:~$ cd a/b/symlink
hm#mach:~/a/b/symlink$ pwd -P
/home/hm/a
hm#mach:~/a/b/symlink$ cd ..
hm#mach:~/a/b$ pwd -P
/home/hm/a/b
pwd -P prints the current working directory, dereferencing all symbolic links.
Why is the working directory /home/hm/a/b at the end, and not /home/hm?
According to help cd,
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
In other words, -L means using the logical structure, whereas -P uses the actually physical directory structure.
The logical structure is like this,
$ tree a
a
└── b
└── symlink -> ..
The actual physical structure when you go to a/b/symlink is,
a
If you want to use the real .., then you must also use cd -P:
The -P option says to use the physical directory
structure instead of following symbolic links (see
also the -P option to the set builtin command);
the -L option forces symbolic links to be followed.
An example,
$ cd
$ cd a/b/symlink # physical location is at a/
$ cd .. # now is at a/b
$ cd symlink # goes back to a/b/symlink
$ cd -P .. # follow physical path (resolve all symlinks)
$ pwd -P # -P is optional here to show effect of cd ..
/home/sarnold
$
bash keeps track of the logical current directory path, as shown in your prompt, and interprets things like cd .. according to that. This makes things a little more consistent if you only use such paths in cd (or pushd), at the cost of unexpected things happening if you then expect the same thing to happen with paths in command arguments (or inside commands; emacs and vim have their own configurable rules for symlink handling, but most commands rely on the kernel to deal with it).

Resources