When I executed the command both commands gave the same output. I created a soft link and a hard link for a file but still both commands gave the same output. Is there a difference between find -H and find -L?

  • Please edit your question to show us a minimal working example that demonstrates the problem. (It should be possible to run from an empty directory and illustrate what you are finding confusing.) Also, what research have you done in order to answer this yourself? At least on my system, the man page for find describes -P, -L and -H very nearly at the top. Your question is receiving downvotes because it doesn't contain the information necessary to provide a good answer nor any evidence of effort in trying to find the answer yourself before asking others. – a CVn Sep 1 '16 at 11:12
  • I believe that this question should never have been closed, and (at the risk of ruffling some feathers) that the people who voted to close it might not have fully grokked it. (1) @aCVn says “Please edit your question to show us a minimal working example that demonstrates the problem.” The next clause, “It should be possible to run from an empty directory …”, is the height of either irony or sarcastic snarkiness. The question literally, explicitly asks «What is the difference between “find -H” and “find -L” …?» … (Cont’d) – G-Man Says 'Reinstate Monica' May 29 '19 at 19:53
  • (Cont’d) … In other words, «I do “find -H” and “find -L” and I get identical results. When do they ever do anything different?» Trivially, “find -H” and “find -L” will produce identical results in an empty directory, as will (for example) ls, ls -A, ls -b, ls -c, ls -C, ls -F, ls -G, ls -i, ls -m, ls -q, ls -r, ls -t and about a dozen others — including (spoiler alert) ls -H and ls -L. The trick — the challenge — what the question asks — is how to create a minimal working example that demonstrates that the options do, in fact, do different things. … (Cont’d) – G-Man Says 'Reinstate Monica' May 29 '19 at 19:53
  • (Cont’d) …  (2) aCVn doesn’t exactly say “RTFM”, but they come close.  Well, imagine that you’re just learning Unix / Linux.  You have a basic knowledge of some basic commands (e.g., cat / more / less, an editor, cp, ln, mv, rm, mkdir, rmdir, maybe chmod, etc.) and somebody tells you that ls is the command to list files and directories, and that almost every letter in the alphabet is a valid option to ls — but they don’t tell you any details, and you don’t have access to any relevant documentation. … (Cont’d) – G-Man Says 'Reinstate Monica' May 29 '19 at 19:53
  • (Cont’d) …  So you try to discover what they all mean by experimenting. If you create file1, and then file2, and then file3, you’re going to have a hard time discovering that -r and -t do different things. If you don’t know what . and .. are, you’re in for a surprise. If you don’t know the lore of “dot-files”, it’s going to take you while to figure out -A. … … … … How long is it going to take you to figure out -H and -L? How long is it going to take you to think of creating symbolic links and seeing how ls treats them when you specify different options? … (Cont’d) – G-Man Says 'Reinstate Monica' May 29 '19 at 19:54

find is not going to treat hard links specially except insofar as the -links test is concerned.  Symbolic links to files are going to be treated very similarly, too.

I would read the find man page to you, but I assume that you've already read it.  Man pages are written in a cryptic language that is hard for beginners to understand.  An example would probably help.  Do this:

$ mkdir dir1 dir2 dir3

$ touch dir1/file1 dir2/file2 dir3/file3

$ ln -s dir2 two

$ cd dir1

$ ln -s ../dir3 three

$ cd ..

$ ls -lR                                # I have deleted my user name from the below.
total 1
drwxr-xr-x 1           0 Sep  4 13:08 dir1
drwxr-xr-x 1           0 Sep  4 13:08 dir2
drwxr-xr-x 1           0 Sep  4 13:08 dir3
lrwxrwxrwx 1           4 Sep  4 13:08 two -> dir2

total 1
-rw-r--r-- 1           0 Sep  4 13:08 file1
lrwxrwxrwx 1           7 Sep  4 13:08 three -> ../dir3

total 0
-rw-r--r-- 1           0 Sep  4 13:08 file2

total 0
-rw-r--r-- 1           0 Sep  4 13:08 file3

$ find dir1 two

$ find -P dir1 two                      # This is the default; i.e., same as the above.

$ find -H dir1 two

$ find -L dir1 two

Note that:

  • In the default behavior (i.e., the -P behavior), find does not follow either symbolic link.  two (in the top-level directory) and dir1/three are simply reported as objects.
  • Under -H, the symbolic link twodir2 is followed (i.e., we get to see file2, which is in dir2) because two is specified on the find command line.  Note that dir1/three is still reported as an object.
  • Under -L, both symbolic links are followed.  We get to see file2, because the two → dir2 link is followed, and we get to see file3, because the dir1/three → ../dir3 link is followed.

If it's not perfectly clear to you now, try running the find commands in my example with -ls at the end (as an alternative to the default -print) and pay particular attention to the ways two and three are listed.  You will notice that symbolic links to files are also reported differently under the different options.

Here's another example:

$ ln -s /bin/sh mysh

$ find . -size +9

$ find -H . -size +9

$ find -L . -size +9

The symbolic link ./mysh is small.  It points to /bin/sh, which is a fairly large file.  Testing with -size, ./mysh is treated as being small under -P (default) and -H, but it is treated as being large under -L, because -L means "look at the file that the link points to".

Yet another example:

  • find . -type f (and find -H . -type f) will find plain files only.
  • find . "(" -type f -o -type l ")" will find plain files and (all) symbolic links.
  • find -L . -type f will find plain files and symbolic links that point to plain files.  (Also, if the directory tree contains any symbolic links to directories, those directories will also be searched.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.