1

I don't understand why this two commands don't provide the same result (the differrence is only relative vs. absolute path), can somebody explain it?

annika /srv/www/pages/com.example.www/www/povruc # find /srv/www/pages/com.example.www/www | xargs grep "datepicker()" 2>/dev/null 
/srv/www/pages/com.example.www/www/povruc/Application/Libraries/3rdParty/zebra/includes/Date.php:    function disable_zebra_datepicker() {
/srv/www/pages/com.example.www/www/js/functions.js:       $(".datepicker").datepicker();

versus

annika /srv/www/pages/com.example.www/www/povruc # find ../ | xargs grep "datepicker()" 2>/dev/null 
../povruc/Application/Libraries/3rdParty/zebra/includes/Date.php:    function disable_zebra_datepicker() {

In the second case the second match is not found

UPDATE - proved that there are no symlinks in the path:

annika /srv/www/pages/com.example.www/www/povruc # namei -ml $(readlink -f $PWD)
f: /srv/www/pages/com.example.www/www/povruc
drwxr-xr-x root       root /
drwxr-xr-x root       root srv
drwxr-xr-x root       root www
drwxr-xr-x root       root pages
drwxrwx--- Wexampl001 root com.example.www
drwxrwx--- Wexampl001 root www
drwxrwx--- Wexampl001 root povruc
  • 2
    directory you are in right now is likely a symlink; so ../ from where you are seemingly at will not go to dir below; rather dir below of `readlink -f $PWD` – Hrvoje Špoljar Nov 19 '14 at 10:05
  • btw, find | xargs is dangerous stuff, like really really dangerous, how dangerous it depends on what xargs is invoking, rather use find -print0 | xargs -0 . Hint, without -print0 -0 check what happens with files that have space in their name. – Hrvoje Špoljar Nov 19 '14 at 10:08

1 Answers1

1

In essence this is the problem; directory from which you are doing the find is symlink; so relative move .. is not moving you to where you would expect; rather to .. of $(readlink -f $PWD)

moo:~$ mkdir foo bar
moo:~$ cd bar/
moo:~/bar$ ln -s ../foo/
moo:~/bar$ touch w00t
moo:~/bar$ ls -1
foo  
w00t
moo:~/bar$ cd foo
moo:~/bar/foo$ ls -1 ../
bar
foo

cool utils to determine real path of some file

$ readlink -f some_name

or check with namei how some path is linked with e.g.

$ namei -ml /etc/passwd f: /etc/passwd drwxr-xr-x root root / drwxr-xr-x root root etc -rw-r--r-- root root passwd

Example above does not contain symlinks along the path but it there were any it would show you in detail what the true path of some file is without symlink insanity.

Hrvoje Špoljar
  • 5,245
  • 26
  • 42
  • No, there are not any symlinks in the path `annika /srv/www/pages/com.example.www/www/povruc # readlink -f $PWD /srv/www/pages/com.example.www/www/povruc` – Jakub Petržílka Nov 22 '14 at 22:42
  • 1
    what about output of `namei -ml $(readlink -f $PWD)` can you please update question with that output – Hrvoje Špoljar Nov 22 '14 at 22:55
  • Ok, I've updated the question – Jakub Petržílka Nov 22 '14 at 23:24
  • strange :\ I'm wondering if running `find ../ | grep 'js/functions.js'` when you're in `povruc` directory finds that file at all – Hrvoje Špoljar Nov 22 '14 at 23:39
  • Yes, it finds this file. What more is wierd, that if I do just: `find ../ | sort > find1` and `find /srv/www/pages/com.example.www/www/ | sort > find2` and then `diff find1 find2` it shows no difference – Jakub Petržílka Nov 23 '14 at 11:42
  • And now maybe I came closer to the source of the problem: `find ../ | xargs grep "datepicker()" 2>/dev/null` versus `find ../ | sort | xargs grep "datepicker()" 2>/dev/null` The first command without sort after find shows also only the first match, the second shows both. Maybe the number of arguments passed with xargs is limited somehow and takes only first X arguments? – Jakub Petržílka Nov 23 '14 at 11:49
  • do `xargs -n1 grep ...` this should resolve the problem if problem is passing multiple arguments to grep at once. – Hrvoje Špoljar Nov 23 '14 at 11:59
  • Ok, I'll answer myself, yes, the problem is the limit of arguments that can be passed by xargs `find ../ | xargs -s 20000 grep "datepicker()" 2>/dev/null` works aswell.. But why the files are beeing found in differend order with absolute resp. relative path? – Jakub Petržílka Nov 23 '14 at 12:00