Linux show link target

Is it possible, executing a file symlinked in /usr/local/bin folder, to get the absolute path of original script? Well, .. I know where original file is, and I know it because I am linkging it. But, . I want this script working, even if I move original source code (and symlink).

coreutils isn’t installed by default on every system that includes bash. stat is more consistently available, but it too has different usage depending on your operating system.

3 Answers 3

readlink is not a standard command, but it’s common on Linux and BSD, including OS X, and it’s the most straightforward answer to your question. BSD and GNU readlink implementations are different, so read the documentation for the one you have.

If readlink is not available, or you need to write a cross-platform script that isn’t bound to a specific implementation:

If the symlink is also a directory, then

will get you into the dereferenced directory, so

echo "I am in $(cd -P "$symlinkdir" && pwd)" 

will echo the fully dereferenced directory. That said, cd -P dereferences the entire path, so if you have more than one symlink in the same path you can have unexpected results.

If the symlink is to a file, not a directory, you may not need to dereference the link. Most commands follow symlinks harmlessly. If you simply want to check if a file is a link, use test -L .

Источник

We are all familiar with symbolic links in Linux. Commonly known as symlinks or soft links, a symbolic link is a particular file that points to another file or directory within any Filesystem.

In this short tutorial, we will go over the basics of symbolic links in Linux and create a simple bash script to get the target of the symbolic link.

There are mainly two types of symbolic links, namely:

Hard links are direct pointers to a file or directory within a filesystem. Hard links are creatable only in the same filesystem as the target file.

Читайте также:  Изменить пароль учетки linux

On the other hand, Soft links are indirect shortcuts to a file or directory and can exist anywhere within a filesystem. Soft links can point to file in a different filesystem.

To create a symbolic link in Linux, we use the ln command. Executing the command with no options creates a hard link to the specified target file.

The general syntax for the ln command is:

As mentioned, the above command will create a hard link to the target file. To create a soft symbolic link, use the -s option as:

Example #1

Let us take the file auth.log in /var/log. We can create a link to the file in our home directory using the command as:

The above command will create a link pointing to the main file. We can verify this by using the ls command:

Example #2

You can also perform a similar operation on a directory. To create a link to /var/log, we use the command:

Similarly, a soft link is created pointing to the target /var/log directory:

To remove a symbolic link, we use the command unlink followed by the path to the symbolic link to remove.

NOTE: If you delete the target file or directory, remove the symbolic link because leaving it creates a broken link.

Every symbolic link points to a target file or directory (unless broken). To fetch the target file/directory of a symlink, we use this command that shows the target of a symlink.

For example, to get the target of the auth.log file we created in an earlier section, we can do:

Using the concepts above, we can assemble a simple bash script that accepts a path and lists all the symlinks and their target files or directory.

A simple script such as the one provided below should do the trick.

#!/bin/bash
echo «Provide the directory to evaluate:»
read target_dir
cd $target_dir
links =$ ( find . -maxdepth1 -type l -ls | awk » )
for link in links
do
echo » $link -> $(readlink $link) »
done

The script starts by asking the user for the directory to evaluate. Then, the script goes to the provided directory and finds all the symbolic links inside the directory, and passes the output to awk.

Awk parses the output and locates only the symbolic links, and saves them to a variable called links.

We then create a loop that grabs each link in the links and evaluates their target value using the readlink command.

Читайте также:  Teamviewer linux mint установка

Finally, we echo the symbolic link and the target directory. Below is an example output:

In the above example, we find all the symlinks in the /etc directory and print their target file or directory.

Conclusion

In this tutorial, we discussed the basics of using symbolic links in Linux. We then created a simple script to find symbolic links in a specified directory and show their source and target.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

is there a way to see the actual contents of a symlink?

it shows the contents of the real file, not what is within the symlink itself. Is there a way to see what’s actually in it?

The underlying system call is readlink(2) . As noted in one of the answers, the direct command to use is readlink(1) on some systems (GNU and relatives). On other systems — HP-UX, Solaris, AIX — the closest approach is likely to be ls -l , but be aware of problems if the path name in the link contains newlines or other weird characters.

5 Answers 5

The ls -l command will show you that:

$ ls -l foo lrwxrwxrwx 1 user group 11 2010-12-31 19:49 foo -> /etc/passwd 

So, the symbolic link foo points to the path /etc/passwd .

Just to make this clearer, the «contents» of the symlink are nothing more than the name it points too.

@Ben: meh, shell is a programming language in the right hands, so the distinction is not entirely clear-cut.

@ijw: True that. However we don’t have to pin that line down exactly in order to figure out that this isn’t on the programmer side of it. Besides, these aren’t even shell commands, ls and readlink are both separate applications.

@Ben: Again, meh. Most lines in scripts run separate programs, such is the nature of Unix. This line is a broad and grey one. It seems reasonably likely he wants it for a script rather than to use on the command-line, though, which makes is analogous to a ‘what’s the library function for doing xxx’ question in other languages.

@CMCDragonkai A hardlink doesn’t have contents per se. It just points to an inode. You can get the inode with stat or ls -di .

Читайте также:  Лучшие видеоплееры линукс минт

You can call the readlink(2) function, which will place the linked-to name into a buffer.

Note that the result has a length (stored in the return value) rather than being NUL-terminated. So if you want to use it as a string, append a NUL yourself.

Most higher-level/scripting languages, such as perl or python, will provide a readlink wrapper that converts to the usual language-appropriate string type, so you won’t be bothered by details such as NUL-termination.

Источник

I’m trying to find all of the symlinks within a directory tree for my website. I know that I can use find to do this but I can’t figure out how to recursively check the directories. I’ve tried this command:

it take a while to run, however I’m getting no matches. How do I get this to check subdirectories?

8 Answers 8

This will recursively traverse the /path/to/folder directory and list only the symbolic links:

ls -lR /path/to/folder | grep '^l' 

If your intention is to follow the symbolic links too, you should use your find command but you should include the -L option; in fact the find man page says:

 -L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the prop‐ erties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirec‐ tory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is bro‐ ken). Using -L causes the -lname and -ilname predicates always to return false. 

This will probably work: I found in the find man page this diamond: if you are using the -type option you have to change it to the -xtype option:

 l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype. 

Источник

Оцените статью
Adblock
detector