Search code examples
bashsedxargssuse

xargs doesn't work on SUSE


This problem occurs only on , it works on ubuntu and even on windows through

My point is to replace a word in several files with another word.

This is what I'm trying:

$  grep -Inrs MY_PATTERN src/ | cut -d: -f1 | xargs sed -i 's/MY_PATTERN/NEW_WORD/g'

sed: can't read path/to/a/found/file_1: No such file or directory
sed: can't read path/to/a/found/file_2: No such file or directory
sed: can't read path/to/a/found/file_3: No such file or directory
...

Knowing that

$  grep -Inrs MY_PATTERN src/ | cut -d: -f1
path/to/a/found/file_1
path/to/a/found/file_2
path/to/a/found/file_3

UPDATE1

This doesn't work either

$  grep -lZ -Irs MY_PATTERN src/ | xargs -0 ls

ls: cannot access path/to/a/found/file_1: No such file or directory
ls: cannot access path/to/a/found/file_2: No such file or directory
ls: cannot access path/to/a/found/file_3: No such file or directory
...

$ ls -al path/to/a/found/file_1 | cat -vet
-rw-r--r-- 1 webme 886 Feb  1 13:36 path/to/a/found/file_1$

UPDATE2

$ whoami
 webme

$ uname -a
 Linux server_vm_id_34 3.0.101-68-default #1 SMP Tue Dec 1 16:21:37 UTC 2015 (ed01a9f) x86_64 x86_64 x86_64 GNU/Linux

UPDATE3

$ grep --version
grep (GNU grep) 2.7
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.


$ xargs --version
xargs (GNU findutils) 4.4.0
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b

# My project path /home/users/webme/projects/my_project
$ df -T
 dl360d-01:/homeweb/users/webme nfs   492625920 461336576  31289344  94% /home/users/webme

$ id
 uid=1689(webme) gid=325(web) groups=325(web)

$ mount -v
/dev/vda2 on / type btrfs (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
debugfs on /sys/kernel/debug type debugfs (rw)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,mode=1777)
devpts on /dev/pts type devpts (rw,mode=0620,gid=5)
/dev/vda1 on /boot type ext3 (rw,acl,user_xattr)
/dev/vdb on /appwebinet type xfs (rw)
rpc_pipefs on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
dl360d-01:/homeweb/users on /homeweb/users type nfs (rw,soft,bg,addr=xx.xx.xx.xx)
dl360d-01:/appwebinet/tools/list on /appwebinetdev/tools/list type nfs (ro,soft,sloppy,addr=xx.xxx.xx.xx)
dl360d-01:/homeweb/users/webme on /home/users/webme type nfs (rw,soft,bg,addr=xx.xxx.xx.xx)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
none on /var/lib/ntp/proc type proc (ro,nosuid,nodev)

I don't have exportfs, neither I have it in sudo zypper install exportfs


Solution

  • I'd suggest keeping it simple:

    $ grep -lZ -Irs foo * | xargs -0 sed -i 's/foo/bar/g'
    

    grep -l outputs matching file names only, which is really what you want in this pipeline. grep -Z terminates each matching file name with a NUL, which xargs -0 can pick up. This allows for file names with embedded white-space to pass between the grep and the xargs unfettered.


    # show the structure
    $ tree
    .
    └── path
        └── to
            └── a
                └── found
                    ├── file_1
                    ├── file_2
                    └── file_3
    
    # show the contents
    $ grep . path/to/a/found/file_*
    path/to/a/found/file_1:a foo bar
    path/to/a/found/file_2:a foo bar
    path/to/a/found/file_3:a foo bar
    
    # try it out
    $ grep -lZ -Irs foo * | xargs -0 ls -l
    -rw-rw-r-- 1 bishop bishop 10 Feb 13 09:13 path/to/a/found/file_1
    -rw-rw-r-- 1 bishop bishop 10 Feb 13 09:13 path/to/a/found/file_2
    -rw-rw-r-- 1 bishop bishop 10 Feb 13 09:13 path/to/a/found/file_3