When searching for .txt files that are in 2 different home directories only one shows up depending on the present working directory. Why is this?
/home/bob/1.txt
/home/alice/dir1/2.txt
pwd /tmp
[root@host tmp]#find /home -name *.txt
/home/bob/1.txt
/home/alice/dir1/2.txt
pwd /home
[root@host bob]#find /home -name *.txt
/home/bob/1.txt
Why does searching from within the bob directory only return the one file?
Because when the working directory is
/home/bob, the*.txtin thefindcommand is expanded by the shell (to1.txt) and that is what is passed tofind. That is,find /home -name 1.txt. That will find the file in/home/bob, but not the differently named one in/home/alice. It would find/home/alice/1.txtif such a file existed.On the other hand, when the pattern does not match any file (relative to the working directory) it is passed on as a literal. At least by default -- you should be careful about this, because the pattern would instead be expanded to nothing if the
nullglobshell option were in effect and thefindcommand were executed from a location where the pattern didn't match any files.If you want to ensure that shell pathname expansion is not applied to the pattern then quote it:
or
or ....