Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all members of a symlink-chain?

Finding the java binaries can be painful:

  • which java gives /usr/bin/java
  • lh $(which java) gives /usr/bin/java -> /etc/alternatives/java
  • lh /etc/alternatives/java gives /etc/alternatives/java -> /usr/lib/jvm/jdk1.8.0_66/bin/java

Is there a way to automatically follow the symlink-chain and print all members? e.g. whichfollow or follow /usr/bin/java could give:

/usr/bin/java
-> /etc/alternatives/java
-> /usr/lib/jvm/jdk1.8.0_66/bin/java
like image 397
Edward Avatar asked Oct 21 '15 09:10

Edward


People also ask

How to list all symlinks in Linux?

List Symlinks On Linux. To list all of the symlinks or symbolic links or soft links in a Linux system, run: $ sudo find / -type l. Here, / - represents the entire filesystem.-type - refers the file type. l - refers the symlink. This command will search for all available symbolic links in the entire filesystem. It will take a while depending ...

Is there a way to list all symbolic links in a directory?

Is there a way to list all of the symbolic links that are in a directory? Credits: How do I make the shell to recognize the file names returned by a `ls -A` command, and these names contain spaces? You can use grep with ls command to list all the symbolic links present in the current directory.

Why do I need to find the real path of a symlink?

because the symlink might resolve into a relative or full path. On scripts I need to find the real path, so that I might reference configuration or other scripts installed together with it, I use this:

How to list all symbolic links in Linux Mint 20?

If you want to list down all the symbolic links of your current file system in Linux Mint 20, then you can do this by executing the command shown below: This variation of the “find” command will take a reasonable time to execute since it has to traverse through your whole file system for finding all the symbolic links that it has.


1 Answers

In addition to the readlink command, GNU/Linux users can use the namei command from the util-linux package. According to its man page:

namei uses its arguments as pathnames to any type of Unix file (symlinks, files, directories, and so forth). namei then follows each pathname until an endpoint is found (a file, a directory, a device node, etc). If it finds a symbolic link, it shows the link, and starts following it, indenting the output to show the context.

Its output isn’t as pretty as you would like but it shows each path components and shows if its a directory, symbolic link, socket, block device, character device, FIFO (named pipe) or regular file.

Example usage:

$ namei  /usr/bin/java

f: /usr/bin/java
 d /
 d usr
 d bin
 l java -> /etc/alternatives/java
   d /
   d etc
   d alternatives
   l java -> /usr/lib/jvm/jre-1.7.0-openjdk/bin/java
     d /
     d usr
     d lib
     d jvm
     l jre-1.7.0-openjdk -> java-1.7.0-openjdk-1.7.0.85/jre
       d java-1.7.0-openjdk-1.7.0.85
       d jre
     d bin
     - java
like image 80
Anthony Geoghegan Avatar answered Nov 15 '22 06:11

Anthony Geoghegan