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
)?
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.
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.
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.
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.
Thanks to Andy Skelton, it appears the answer is readlink -f
:
$:/tmp/symtest$ readlink -f /tmp/symtest/foo/bar/cat /tmp/symtest/cat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With