The easiest way to link to the current directory as an absolute path, without typing the whole path string would be
ln -s "$(pwd)/foo" ~/bin/foo_link
The target
(first) argument for the ln -s
command works relative to the symbolic link's location, not your current directory. It helps to know that, essentially, the created symlink (the second argument) simply holds the text you provide for the first argument.
Therefore, if you do the following:
cd some_directory
ln -s foo foo_link
and then move that link around
mv foo_link ../some_other_directory
ls -l ../some_other_directory
you will see that foo_link
tries to point to foo
in the directory it is residing in. This also works with symbolic links pointing to relative paths. If you do the following:
ln -s ../foo yet_another_link
and then move yet_another_link
to another directory and check where it points to, you'll see that it always points to ../foo
. This is the intended behaviour, since many times symbolic links might be part of a directory structure that can reside in various absolute paths.
In your case, when you create the link by typing
ln -s foo ~/bin/foo_link
foo_link
just holds a link to foo
, relative to its location. Putting $(pwd)
in front of the target argument's name simply adds the current working directory's absolute path, so that the link is created with an absolute target.
I created a script that will do this. The script converts all hard-links it finds in a source directory (first argument) that are the same as in the working directory (optional second argument) into symbolic links:
https://gist.github.com/rubo77/7a9a83695a28412abbcd
It has an option -n for a dry-run, that doesn't do anything but shows what would be done.
Main part:
$WORKING_DIR=./
#relative source directory from working directory:
$SOURCE_DIR=../otherdir/with/hard-links/with-the-same-inodes
# find all files in WORKING_DIR
cd "$WORKING_DIR"
find "." -type f -links +1 -printf "%i %p\n" | \
while read working_inode working_on
do
find "$SOURCE_DIR" -type f -links +1 -printf "%i %p\n" | sort -nk1 | \
while read inode file
do
if [[ $inode == $working_inode ]]; then
ln -vsf "$file" "$working_on"
fi
done
done
The -links +1 --> Will find all files that have MORE than 1 link. Hardlinked files have a link count of at least two.
Best Answer
With any POSIX implementation of
cd
, you can use the-P
option to do this.You can see it in action here:
If you want this to be the default behaviour, you can either create an alias for
cd
, like so:...or use
set -o physical
. For tcsh, the equivalent command isset symlinks=chase
.