centos - Find and delete line in .htaccess file

07
2014-07
  • user209827

    I am trying to figure out how could possible a command do search and delete line from .htaccess file . the following line is the line i want to search and delete

    RedirectMatch \.(dynamiccontent|pl|plx|perl|cgi|php|php4|php4|php6|php3|shtml)$ http://server.linux.com/cgi-sys/movingpage.cgi
    

    Note that this line have special characters

    This is the command to find the code

    find /home*/*/public_html/ -mindepth 1 -iname "\.htaccess" -type f -exec grep -Hi "RedirectMatch*" '{}' \;
    

    But this is only find not find and delete the line in .htaccess file

    How can i modify the command to find and delete the line that i mention ?

  • Answers
  • stib

    This should do it:

    while IFS= read -r -d '' file; do
    grep -iv "RedirectMatch*" $file>tmp
    mv tmp $file
    done < <(find /home*/*/public_html/ -mindepth 1 -iname "\.htaccess" -type f -print0)
    rm tmp
    

    The output of the find command gets used by process substitution in the while loop, as $file. Then grep -vi $file returns every line in $file that doesn't match (ignoring case). It writes that to a temp file called tmp, and then copies it over the original .htaccess files. For safety you could add this extra line before the mv command:

    mv "$file" "$file".old
    

    This will rename the original htaccess files to .htaccess.old, in case something is borked.


  • Related Question

    How to search for files and directories using a single find command
  • Stormshadow

    I am using UnxUtils on Windows and it comes with the find tool. I am using it to search for files and directories. If I wish to search for a file name which contains the word 'Identity', I use the following (for a case insensitive search)

    find <location> -type f -iname "*Identity*"
    

    and the following for searching directory names

    find <location> -type d -iname "*Identity*"
    

    I wish to search for both files and directories using a single command (to replace windows search). Is this possible using find ? Is there any native Windows tool which I can use instead to do a search on the command line ? The Windows commands FIND and FINDSTR are like the Unix grep and do not really help.

    EDIT:

    find <location> -iname "*Identity*" -ls
    

    Searches for all files and folders in the given location (recursively). The -iname flag for a case insensitive search and the -ls flag to give an ls like output listing enabling us to know the file types.


  • Related Answers
  • Benjamin Bannier

    Just remove the type specification.

    find <location> -iname "*Identity*"