There are a few problems here. If this script is in the directory where you are looking for and changing occurrences of "./x.mak", one of the files it will try to modify is itself (in at least two places).
Second, in standards conforming versions of sed, \n
in the replacement string of a substitute command will insert an <n> character, not a <newline> character. I know that some versions of sed do provide the common backslash escapes here, but using them here might or might not work depending on what OS (or sed utility) you're using. In a standards conforming sed, you could use a backslash-escaped literal newline character instead of the \n to get what you want:
sed 's|\./x\.mak|chmod 775 x.mak\
./x.mak|g'
Third, the -print0 primary in find is also an extension to the standards. If the system you're using supports it and you're going to use xargs as well as find -print0 you have to also use the xargs -0 option when you invoke xargs. If you wanted a more portable way to do this you could try:
find . -type -f -exec sed 's|\./x\.mak|chmod 775 x.mak\
./x.mak|g' {} \;
And finally, the script you provided seems to try to print the contents of all of the regular files it finds in and under the current directory (with changes to any occurrence of "./x.mak") to standard output, but does not save any of the changes into any of the source files. I would guess this isn't what you're trying to do. (If this really is what you want, you could also change the \;
at the end of the find -exec primary's arguments in the last suggestion above to +
to run more efficiently.)
If you do want to actually change the contents of the files containing "./x.mak" (and only them), you are absolutely certain that no filename components in the file hierarchy you'll be processing contain a <newline> character, and you only want to change files in the current directory or a known list of directories (not those in all subdirectories recursively); you might consider a loop like:
grep -Fl ./x.mak * other_directory/* ... |while IFS="" read -r file
do process "$file"
done
where I would expect processing would exclude your script and actually change the contents of other files that contained the target string. Note that grep -F matches fixed strings (not regular expressions) so the periods don't need to be escaped here. (Note also that some versions of grep provide a recursive option (-R) that could be used as:
grep -FlR ./x.mak .|while IFS="" read -r file
do process "$file"
done
to process all files in and under the current directory containing the target string.)
Hope this helps.