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?
1 Answer
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 ./dir1: total 1 -rw-r--r-- 1 0 Sep 4 13:08 file1 lrwxrwxrwx 1 7 Sep 4 13:08 three -> ../dir3 ./dir2: total 0 -rw-r--r-- 1 0 Sep 4 13:08 file2 ./dir3: total 0 -rw-r--r-- 1 0 Sep 4 13:08 file3 $ find dir1 two dir1 dir1/file1 dir1/three two $ find -P dir1 two # This is the default; i.e., same as the above. dir1 dir1/file1 dir1/three two $ find -H dir1 two dir1 dir1/file1 dir1/three two two/file2 $ find -L dir1 two dir1 dir1/file1 dir1/three dir1/three/file3 two two/file2 Note that:
- In the default behavior (i.e., the
-Pbehavior),finddoes not follow either symbolic link.two(in the top-level directory) anddir1/threeare simply reported as objects. - Under
-H, the symbolic linktwo→dir2is followed (i.e., we get to seefile2, which is indir2) becausetwois specified on thefindcommand line. Note thatdir1/threeis still reported as an object. - Under
-L, both symbolic links are followed. We get to seefile2, because thetwo→dir2link is followed, and we get to seefile3, because thedir1/three→../dir3link 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 ./mysh 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(andfind -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 fwill 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.)
finddescribes-P,-Land-Hvery 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.ls,ls -A,ls -b,ls -c,ls -C,ls -F,ls -G,ls -i,ls -m,ls -q,ls -r,ls -tand about a dozen others — including (spoiler alert)ls -Handls -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)cat/more/less, an editor,cp,ln,mv,rm,mkdir,rmdir, maybechmod, etc.) and somebody tells you thatlsis the command to list files and directories, and that almost every letter in the alphabet is a valid option tols— but they don’t tell you any details, and you don’t have access to any relevant documentation. … (Cont’d)file1, and thenfile2, and thenfile3, you’re going to have a hard time discovering that-rand-tdo 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-Hand-L? How long is it going to take you to think of creating symbolic links and seeing howlstreats them when you specify different options? … (Cont’d)