- How to find and list all the symbolic links created for a particular file?
- Example:
- 4 Answers 4
- How to find all hard links to a given file? [duplicate]
- 1 Answer 1
- Documentation
- Differences between find and ls
- symbolic link: find all files that link to this file
- How do I find all of the symlinks in a directory tree?
- 8 Answers 8
- How to find all hardlinks in a folder?
- 4 Answers 4
- Search for hard links
- Other causes why different amount of drive space is used
How to find and list all the symbolic links created for a particular file?
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.
How to find all hard links to a given file? [duplicate]
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:
find . -samefile /path/to/file
The above was tested on GNU find. Although -samefile is not POSIX, it is also supported by Mac OSX find and FreeBSD find.
Documentation
-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.
symbolic link: find all files that link to this file
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 )
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.
How do I find all of the symlinks in a directory tree?
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.
How to find all hardlinks in a folder?
I have two external disks that has the same files. One is encrypted, the other is not. The encrypted one has a lot less space left than the non encrypted, I now assume that it is because of hardlinks on the non encrypted disks. So I would like to check, if there are any hardlinked files that might be doubled on the encrypted disk. How can I identify a hardlink? If you have any other ideas what the reason for the free space issue could be, I’am open to ideas. Is it possible that the files need more space because of the encryption?
Please edit your original question, when the two external disks are connected and their partitions mounted, in order to show the output of the following commands for the two file systems (the encrypted one and the un-encrypted one), df ; sudo lsblk -f ; sudo lsblk -m ; Indent each line 4 spaces to render the output as code .
4 Answers 4
That will show all regular files that have more than one link (name) to them. It will not tell you which names are linked to the same file, for that you could use -samefile or -inum , e.g. find -samefile «$somefile»
In the technical sense, all files (file names) are (hard) links, it’s just that files with more than one link pointing to them are interesting in this sense. But even in those cases, there’s no way to say that one of them is the «proper» file, and the other a link, the links are equal.
$ touch a b c $ ln b b2 ; ln c c2 $ find -type f -links +1 ./c2 ./b ./b2 ./c $ find -samefile b ./b ./b2
-links +1 is a GNU extension. For better portability (POSIX compliance), use the equivalent \! -links 1 as in: find . -type f \! -links 1 . Also, -samefile is likewise a GNU extension for which there is no simple POSIX equivalent (at least not within find ).
Ah, that is true. I frequent Unix & Linux SE and tend to forget that portability isn’t as much of a concern over here. 🙂
Search for hard links
@ilkkachu’s and @barrycarter’s answers are good. This answer is an alternative, that describes some results with more details.
- If the linked in the same directory tree, you will find them directly.
- Otherwise you can search in the whole file system from the mount point, but only within the same file system using -xdev , which is important if you search the root partition / and there are other mounted partitions.
$ sudo find / -xdev -type f -links +1 -ls | sort -n > hard-links-in-root.txt
The following is an example, where one hard linked pair is found in the current directory, and two hard linked matches are found in another directory by searching from the mount point /media/multimed-2 of the data partition.
$ sudo find . -xdev -type f -links +1 -ls | sort -n 5242881 648 -rw-rw-r-- 2 olle nio 657936 jun 30 2015 ./like-this.png 5242882 940 -rw-rw-r-- 2 olle nio 957688 jun 30 2015 ./from-here.png 14843905 1620 -rw-r--r-- 2 olle nio 1652803 jun 30 2015 ./img_4810.jpg 14843905 1620 -rw-r--r-- 2 olle nio 1652803 jun 30 2015 ./mid-sommer-night_4810.jpg $ find /media/multimed-2/ -samefile ./like-this.png /media/multimed-2/Photos/2015/06/30/like-this.png /media/multimed-2/Bilder/kartor/like-this.png $ find /media/multimed-2/ -samefile ./from-here.png /media/multimed-2/Photos/2015/06/30/from-here.png /media/multimed-2/Bilder/kartor/from-here.png
Other causes why different amount of drive space is used
- Different file systems (ext4, NTFS, FAT32 . )
- Different partition size, which causes differences in the overhead (meta-data).
- Different sector size on the drive (maybe?)