Linux list all links

The symbolic link, also known as symlink or soft link, is the file type that can hold the location of a file or directory in any Linux file system. You have created a couple of Symbolic links in your Linux filesystem, and sometimes there comes a need to list all the symbolic links. This post provides you with a step-by-step guide on how to list all symlinks in a Linux filesystem or a specific Linux directory.

From a couple of ways to list all the symbolic links in a Linux directory, we will follow the reliable and best way using the find command.

Find command comes in handy when finding any type of file or folder in a Linux operating system.

Syntax

To find the symbolic links in any Linux operating system, the syntax is as follows:

In the above command,

is the location or directory name in which you want to search for the symbolic link,

-type is referencing the file type,

while l is representing the link file type.

Alright, let’s have a look at the examples and see how can we get the symbolic links listed in different ways by going through a couple of examples:

Examples

Using the find command, we can list the symlinks from the entire filesystem or in a specific directory. Let’s take a look at each example:

To list all the symlinks from the entire filesystem, you can execute the following find command by providing the “/” as path:

The “/” in the above command represents the entire file system, and the find command will search for the symbolic links from all over the system and list them out in the terminal.

Similarly, if you want to find and list all the symlinks in the current working directory, then simply provide the “.” as a path to the find command as shown below:

In the above command, the “.” tells the find command to find the symlinks in the current working directory.

To list all the symlinks in any directory, just provide the directory path to the find command as shown below:

The find command will look for the symbolic links in the /var/www/ directory only and list out all the symbolic links in that directory.

You might have noticed that all the above commands displayed the symbolic links in the desired directory and showed all the symbolic links from the subdirectories, as well.

So, what if you do not want to go into this much depth? You just want to have the symbolic links in the specified directory. The solution to that problem is not rocket science, and we can quickly mention the depth using the maxdepth flag.

Читайте также:  Загрузочный диск для принтера canon lbp 2900

For example, to set the search depth to level one, the find command would go like this:

You can witness the output shown in the screenshot given above. The find command has shown only the symbolic links of the current working directory instead of all the subdirectories.

Conclusion

This post has provided multiple ways and gives a brief explanation on how to list all the symbolic links in the Linux filesystem or a specific Linux directory. Using the find command, we have learned to find and list down all the symbolic links and set the maximum depth level using the maxdepth flag. If you want to learn and explore more about the find command, feel free to read the man page of find using the “man find” command.

Источник

On this question or on this one (for example) you will get solutions on how to look for symlinks pointing to a given directory (let’s call it /dir1 ), while I am interested to symbolic links possibly pointing to any file/folder inside /dir1 .

I want to delete such directory but I am not sure that I am safe to do so, as on an other directory (let’s call it /dir2 ), I may have symlinks pointing to inner parts of /dir1 .

Further, I may have created these symlinks using absolute or relative paths. My only help is that I know the symlinks I want to check are on a mounted filesystem, on /dir2 .

4 Answers 4

You can find all the symbolic links using:

you might want to run this as root in order to get to every place on the disc.

You can expand these using readlink -f to get the full path of the link and you should be able to grep the output against the target directory that you are considering for deletion:

Using find / -type l -printf ‘%l\n’ doesn’t work as you get relative links like ../tmp/xyz which might be pointing to your target dir, but are not matched because they are not fully expanded.

In my case, the accepted answer wasn’t useful (because it didn’t output the link source). Here is what worked for me.

I worked around it using two -exec clauses:

  • print -rC1 : prints its arguments r aw on 1 C olumn. Here, the arguments will be those generated from the following glob. Replace with ls -ld to get more information about each symlink¹
  • **/ : any level of subdirectories (recursive globbing)
  • * : file with any name (made of any number of characters, though zsh’s * like that of most shells will also allow non-characters).
  • (. ) : glob qualifiers to further qualify the matching on other criteria than just the name of the files
  • N : enable nullglob for that one glob (won’t fail if there’s no match, just pass an empty list to print which will print nothing).
  • D : enable dotglob : also consider hidden files.
  • @ : restrict to files of type symlink.
  • e[‘code’] : select files for which the code evaluates to true. Inside the code , the file being considered is stored in $REPLY .
  • $REPLY:P : gets the absolute and canonical (symlink free) path to the file (similar to what the realpath() standard function does).
  • [[ string = pattern ]] returns true if the string matches the pattern (from ksh).
  • /dir1(/*|) as a pattern matches on /dir1 alone or /dir1/ followed by anything.

¹ with the caveat that if there’s not matching file, that will list the current working directory. With ls , it would be better to remove the N glob qualifier

Depending on your circumstances, you could delete the directory, then delete any resultant invalid symlinks with the following:

Читайте также:  Linux get fqdn by ip

The xtype test returns ‘l’ if the symlink is broken.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2022.11.3.43003

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Consider the following command line snippet:

At this point, I can use readlink to see what is the ‘original’ (well, I guess the usual term here is either ‘target’ or ‘source’, but those in my mind can be opposite concepts as well, so I’ll just call it ‘original’) file of the symlinks, i.e.

. However, what I’d like to know is — is there a command I could run on the ‘original’ file, and find all the symlinks that point to it? In other words, something like (pseudo):

Thanks in advance for any comments,

6 Answers 6

Using GNU find , this will find the files that are hard linked or symlinked to a file:

I’ve not seen a command for this and it’s not an easy task, since the target file contains zero information on what source files point to it.

This is similar to «hard» links but at least those are always on the same file system so you can do a find -inode to list them. Soft links are more problematic since they can cross file systems.

I think what you’re going to have to do is basically perform an ls -al on every file in your entire hierarchy and use grep to search for -> /path/to/target/file .

For example, here’s one I ran on my system (formatted for readability — those last two lines are actually on one line in the real output):

Inspired by Gordon Davisson’s comment. This is similar to another answer, but I got the desired results using exec. I needed something that could find symbolic links without knowing where the original file was located.

Symlinks do not track what is pointing to a given destination, so you cannot do better than checking each symlink to see if it points to the desired destination, such as

Here’s what I came up with. I’m doing this on OS X, which doesn’t have readlink -f , so I had to use a helper function to replace it. If you have it a proper readlink -f you can use that instead. Also, the use of while . done is not strictly needed in this case, a simple find . | while . done would work; but if you ever wanted to do something like set a variable inside the loop (like a count of matching files), the pipe version would fail because the while loop would run in a subshell. Finally, note that I use find . -type l so the loop only executes on symlinks, not other types of files.

This may be too simplistic for what you want to do, but I find it useful. it does Not answer your question literally, as it’s not ‘run on the original file’, but it accomplishes the task. But, a lot more HDD access. And, it only works for ‘soft’ linked files which is majority of user linked files.

from the root of you data storage directory or users data directories, wherever symlinked ‘files’ to the orig.file may reside, run the find command:

I would Normally use part of the name eg, ‘*orig*’ to start, because we know users will rename (prefix) a simply named file with a more descriptive one like » Jan report from London _ orig.file.2015.01.21 » or something.

Читайте также:  Astra linux radeon driver

Note: I’ve Never gotten the -samefile option to work for me.

Источник

How can we find all hard links to a given file? I.e., find all other hard links to the same file, given a hard link?

Does filesystem keep track of the hard links to a file?

The inode of a file only stores the number of hard links to the file, but not the hard links, right?

1 Answer 1

If the given file is called /path/to/file and you want to find all hard links to it that exist under the current directory, then use:

The above was tested on GNU find. Although -samefile is not POSIX, it is also supported by Mac OSX find and FreeBSD find.

Documentation

From GNU man find :

-samefile name
File refers to the same inode as name. When -L is in effect, this can include symbolic links.

Differences between find and ls

ls -l lists the number of hard links to a file or directory. For directories, this number is larger than the number of results shown by find . -samefile . The reason for this is explained in the GNU find manual:

A directory normally has at least two hard links: the entry named in its parent directory, and the . entry inside of the directory. If a directory has subdirectories, each of those also has a hard link called .. to its parent directory.

The . and .. directory entries are not normally searched unless they are mentioned on the find command line.

In sum, ls -l counts the . and .. directories as separate hard links but find . -samefile does not.

Источник

I want, only using «basic» commands (for maximum portability) (i.e., something that would work on AIX / Linux / etc., not just something using a recent nicety ^^), to find all the files (symlinks, hardlinks and combinations thereof) pointing to a specific file/dir.

Be careful to not rush to answer find / -ls | grep . : it will miss many cases. See my links below, mixing hardlinks, symlinks, relative-and-absolute paths (and also play with symlinks «././././.» possibilities)

  • hardlinks and symlinks can be «nested», ad-inifinitum.
  • some symlinks could be with the full path, others with a relative path,
  • those paths (relative or absolute) could be very complex (ex: /tmp/././../etc/file )
  • a symlink could lead to a file, which hardlink to another, which is a symlink to a third, which ends up [after some more iteration] to the final destination.

In the end, I «just» need to find out a way to know what is the «final destination» of any file/link (ie, which inode will be accessed in the end?). But it’s really tough (unless some magical function will tell me «the final destination inode is : . «. That’s what I need!)

I thought I could simply use ‘-H’ or ‘-L’ options of find, but (I’m probably dumb. ) it didn’t work. yet.

Any info welcomed (but please, using find/ls/etc, not some «nice utility only available on linux»)

Try to create some different links to the «/tmp/A» directory, and find a way to find and list them all:

and then some tries:

I expected to see the final inode (123456) in front of all the paths, in one of those invocation (I also added ‘-follow’ to both), but I always see the inodes of the links, not of the «final destination» (i.e., /tmp/A)

What could I use to find out the «final inode» I end up accessing? [the OS manages it, but can a «simple» command tell me beforehand «through that file, you will open that final inode!»?]

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector