Linux find follow links

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. 

Источник

It depends, if you are trying to find links to a specific file that is called foo.txt, then this is the only good way:

find -L / -samefile path/to/foo.txt 

On the other hand, if you are just trying to find links to any file that happens to be named foo.txt , then something like

find . -lname \*foo.txt # ignore leading pathname components 

you may want to recurse, as any of the files pointing to foo.txt may themselves be pointed-at by some other links. ex: A->B->foo.txt, /tmp/C->B->foo.txt, etc.

this can work too if your name component is a parent directory named in the link, by searching find . -lname ‘*foo.dir*’ (matches e.g. file.txt -> ../foo.dir/file.txt )

Читайте также:  Объем дискового пространства linux команда

This answer is really helpful, thanks so much. By the way, if we want to get more info about the results of «find», we can pass the output of «find» to «ls»: find -L /usr -samefile /usr/share/pyshared/lsb_release.py 2>/dev/null | xargs ls -al

Find the inode number of the file and then search for all files with the same inode number:

$ ls -i foo.txt 41525360 foo.txt $ find . -follow -inum 41525360 

Alternatively, try the lname option of find , but this won’t work if you have relative symlinks e.g. a -> ../foo.txt

If using a recent version of GNU find, you can also use the -samefile option with -L for the same effect, without having to look up the inode yourself

If foo is a directory, use ln -di , in one line: find . -follow -inum $(ls -di foo.txt |cut -d» » -f1)

This answer is wrong. A symlink doesn’t have the same inode as its target. This will only work for a file inside a symlinked folder. But this is not what was asked.

I prefer to use the symlinks utility, which also is handy when searching for broken symlinks. Install by:

sudo apt install symlinks 

Show all symlinks in current folder and subfolders:

To find a specific symlink, just grep :

symlinks -rv . | grep foo.txt 

Never heard of symlinks before (repo). ‘A useful utility for maintainers of FTP sites, CDROMs, and Linux software distributions. It scans directories for symbolic links and lists them on stdout, often revealing flaws in the filesystem tree.’ Great little ancient tool by ancient kernel hacker Mark Lord, the ‘original developer and maintainer of the IDE Performance Package for linux, the Linux IDE Driver subsystem, hdparm’, now maintained by J. Brandt Buckley.

Works great and is fast. Note, however, that you’ll need to do a search in each discrete partition of interest (as symlinks does not reliably (AFAIK) search across what is calls «different filesystems»). Also, symlinks -rv . 2>/dev/null | grep foo.txt may result in «cleaner» output.

Источник

You got a symbolic link and wondering about the actual source file? Here’s how to follow symlinks in Linux.

A symbolic link (also known as soft link) is a kind of shortcut to another file. It’s heavily used in Linux for shared libraries.

But how do you know to which original file the link points to?

You can use the ls command for this purpose. Surprised? Don’t be. The long listing ls -l displays where a symbolic link points:

For example, I’ve created a soft link named MyTorrents that targets another disk so my command will be:

ls -l /home/sagar/Symbolics/MyTorrents

However, this is not a foolproof way to follow the symbolic link to the original file because if it’s a multilayer link (a link pointing to another link that points to a file), the ls command won’t display the source file.

It’s a no-brainer that with enough skills, you do have multiple ways of accomplishing the same thing, especially if we consider Linux.

So I’ll be utilizing the following command line utilities to follow symbolic links:

You can use the ln command to create links and practice while you follow this tutorial.

Читайте также:  Linux mount всех пользователей

A specific utility that is just made to accomplish our goal. Yes, that’s readlink.

It is quite easy to use and available by default on every Linux distro. So just give a path of symbolic link with readlink command and that’s it.

readlink /path/to/symbolic/link

My symbolic link is placed at /home/sagar/Symbolics/MyTorrents so my command would be:

readlink /home/sagar/Symbolics/MyTorrents

use readlink to find source of symbolic link

But what if your symbolic link involves multiple layers such as one link indicted to another? Well, in that case, you’d have to use -f option.

For this example, I’ve created a new symbolic link located at /home/sagar/Documents/NewLink and maps to the other link to have a better idea of how to deal with such scenarios:

readlink -f /home/sagar/Documents/NewLink

use readlink to get source of symbolic links when dealing with multiple latyers

2. Using realpath command

As its name suggests, the realpath utility is used to get the path of files and directories but the interesting thing is when used without any option, it can get us to the source of the symbolic link.

Using realpath even without any options is equivalent to using readlink -f so don’t worry about being mapped to another symbolic link.

The syntax of realpath to follow symbolic link to the source file is:

realpath /path/to/symbolic/link

And after specifying the path, end result should look like this:

use realpath command to find source of symbolic link

3. Using stat command

The stat utility is used to get the status of files and can also be utilized to find the original source of the symbolic link.

Just give a path of the symbolic link to the stat command and that’s it.

use stat command to find source of symbolic link

And if you find the other details unnecessary, you can use the -c%N option to filter them out. Not the easiest option to remember and hence use the man or help command to recall it.

stat -c%N /path/to/symbolic/link

stat command with less clutter showing original source of symbolic link

4. Using file command

Well, using the file command is quite easy and you’re required to follow the same syntax that you saw earlier with other examples.

A file command with a path to a symbolic link. That’s all you’d need!

use file command to find source of symbolic link

Final Words

If you’re dealing with multilayer soft link layers, I recommend using the first two ways of following symbolic links.

These utilities are quite basic and do not require any complex syntax but if you’re still confused, let me know in the comments.

Источник

How do I make find follow most, but not all, symbolic links?

My OS and home directory are on an SSD. I store some large files on a hard disk, and symlink from my home directory to the hard disk (eg. ~/Videos/Films is a symlink to /mnt/hdd/Films). I also have a number of Wine prefixes, each of which has dosdevices/z: symlinked to /. So, if I used find without -L, it will miss everything that’s on the hard disk. But if I use find with -L, it ends up in a loop due to Wine’s symlink to /. Is there any sensible way of resolving this, so that find searches where I want it to? Intuitively, I’m wanting something along the lines of «follow symlinks, unless they’re under a directory called dosdevices». «Follow symlinks that are to something on the hard disk» would work too.

4 Answers 4

The -prune primary tells find not to recurse under a directory.

find -L -path ~/.wine/dosdevices -prune -o -type f -name 'My Favorite Movie.*' -print 

If you want to use -lname in your condition, you can’t use the -L option, because -L causes most predicates to act on the target of the link, including -lname . My recommendation in that case would be to use both your home directory and the hard disk root as the roots of your search.

find ~ /mnt/hdd -xtype f -name 'My Favorite Movie.*' 

You might run find ~ -type l … to gather a list of symbolic links and use them as additional roots.

( IFS=$'\n'; set -f; find ~ $(find ~ -type l -lname '/mnt/hdd/*') \ -xtype f -name 'My Favorite Movie.*' ) 

If you really want to recurse under symbolic links, you can exclude specific symbolic links by target instead of by name. However you can only exclude a finite list, not a pattern or subtree this way.

find -L \( -samefile /exclude/this -o -samefile ~/and/that \) -prune -o \ -type f -name 'My Favorite Movie.*' -print 

You can use other criteria such as ! -writable (files that you don’t have write permission to), but I don’t think GNU find has a way to recurse under symbolic links only if their target text matches a certain expression.

Читайте также:  Wifiphisher kali linux установка

You could build a find command that does almost what you want except for not excluding enough symbolic links, run find2perl to convert the query to a Perl script, and tweak the Perl script a bit. There are many switches of GNU find that find2perl doesn’t recognize, however.

Источник

I had created many symbolic links on various paths for a particular file or a directory. I want the whole list of created symbolic links paths (location).

Example:

I created symbolic links for ~/Pictures directory on many directories. How do I list all the symlinks to that ~/Pictures directory? Is that possible? If yes, then how?

You need to search exhaustive, there is no count stored like there is for hard-links. See one of the answers using find.

4 Answers 4

find -L /dir/to/start -xtype l -samefile ~/Pictures 
find -L /dir/to/start -xtype l -samefile ~/Pictures 2>/dev/null 

to get rid of some errors like Permission denied , Too many levels of symbolic links , or File system loop detected which find throws them when doesn’t have the right permissions or other situations.

  • -L — Follow symbolic links.
  • -xtype l — File is symbolic link
  • -samefile name — File refers to the same inode as name . When -L is in effect, this can include symbolic links.

Could the command be modified to find symbolic link that contains a path? e.g. files across the system that may link to ~/Pictures/A, ~/Pictures/A/B/C, or any files in the subdirectories of ~/Pictures>

Very simple, use option -lname :

find / -lname /path/to/original/dir 
-lname pattern File is a symbolic link whose contents match shell pattern pattern. The metacharacters do not treat `/' or `.' specially. If the -L option or the -follow option is in effect, this test returns false unless the symbolic link is broken. 

Note: Remember that symbolic links could be anywhere, which includes a remote system (if you’re sharing files), so you may not be able to locate them all.

Источник

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