find, grep, print or change first line of all perl file Shebang(#!)

search help:

find /home/smith/source -name \*.html -o -name \*.php -exec egrep -H 'str1|str2' {} \;
find /home/smith/source -name \*.html -o -name \*.php -exec egrep -H 'str1|str2' {} \;

seach and find hardcoded dblink strings in codebase.

[root@fts-vm-prod cgi-bin]# grep -r -i --exclude-dir=cron_scripts_old "ctsprd.cisco.com" . |wc -l
311


find /var/www/cgi-bin -name "*.pl" -type f -print | xargs file | grep "search text" | cut --delimiter=: --fields=1 | xargs -I file sed -n '1p' file

1.       First find gets all *.pl perl files
2.       Next pipe file command shows if it is text file or binary
3.       We filter using grep on text files
4.       Pick up the first field, the name of the file.
5.       Pass name of file to  sed to print first line.

On the step 5,
 if we change this sed
   from: “sed –n ‘1p’ file “
   to     : “sed –n `1 s/\/usr\/bin\/perl/\usr\/bin\/env perl”

We got all files changed.
=====  ABOVE does NOT work. Sorry

Here is the correct one in RHEL6.  NEED to test on subdirectories.  Doesn't go deeper.


 find . -name "*.pl" -type f -print | xargs file | grep "search text" | cut --delimiter=: --fields=1 | xargs -I file perl -p -i -e "s/<HOST_NAME>/my_hostName/g" file

Popular Posts