Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I derefence symbolic links in bash? [duplicate]

Tags:

bash

symlink

How can I take any given path in bash and convert it to it's canonical form, dereferencing any symbolic links that may be contained within the path?

For example:

~$ mkdir /tmp/symtest ~$ cd /tmp/symtest/ /tmp/symtest$ mkdir -p foo/bar cat/dog /tmp/symtest$ cd foo/bar/ /tmp/symtest/foo/bar$ ln -s ../../cat cat /tmp/symtest/foo/bat$ cd ../../ /tmp/symtest$ tree . |-- cat |   `-- dog `-- foo     `-- bar        `-- cat -> ../../cat  6 directories, 0 files 

How can I get the full canonical path of /tmp/symtest/foo/bar/cat (i.e: /tmp/symtest/cat)?

like image 249
David Dean Avatar asked Jul 29 '09 01:07

David Dean


People also ask

How do you dereference a symbolic link?

To dereference a symbolic link means to follow the link to the target file rather than work with the link itself. When you dereference a symbolic link, you end up with a pointer to the file (the filename of the target file). The term no-dereference is a double negative: It means reference.

What happens if you copy a symlink?

Copy symbolic links as files '-L' ,'--dereference' - Follow symbolic links when copying from them. With this option, cp cannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree.

Can you copy symlinks?

3. Using the -l Option. We can use the -l option of rsync for copying symlinks. rsync copies the symlinks in the source directory as symlinks to the destination directory using this option.

How do you remove a symbolic link?

To remove a symbolic link, use either the rm or unlink command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.


1 Answers

Thanks to Andy Skelton, it appears the answer is readlink -f:

$:/tmp/symtest$ readlink -f /tmp/symtest/foo/bar/cat /tmp/symtest/cat 
like image 153
David Dean Avatar answered Sep 23 '22 23:09

David Dean