FindReplace

From Hurlster Wiki
Revision as of 18:50, 1 May 2013 by Gqwill69 (talk | contribs) (→‎Find Execute)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script searches a directory recursively and finds/replaces text.

#!/usr/bin/perl 

use strict; 
use warnings; 
use File::Find; 

my $startdir = '/var/www/directory/'; 
my $find = 'foo'; 
my $replace = 'bar'; 
my $doctype = 'cgi';  

print qq~Finding "$find" and replacing it with "$replace"\n~; 

find( 
   sub{ 
      return unless (/\.$doctype$/i); 
      local @ARGV = $_; 
      local $^I = '.bac'; 
      while( <> ){ 
         if( s/$find/$replace/ig ) { 
            print; 
         } 
         else { 
            print; 
         } 
      } 
}, $startdir);

print "Finished";

Find by Date

find . -maxdepth 1 -type f -newermt 2007-10-01 ! -newermt 2007-10-02 -ls
  • To Delete
find . -maxdepth 1 -type f -newermt 2007-10-01 ! -newermt 2007-10-02 -delete

SED remove line

  • Delete "SetEnv" and everything after plus 5 lines below
sed '/^SetEnv.*/,+5d' file.txt

Find Execute

find . -type f -name \*.avi -exec mv {} ./ \;

Rename

Using "rename" utility to Uppercase first letter in each word.

rename -v 's/\b(\w)/\u$1/g' myfile\ name\ here.txt
myfile name here.txt renamed as Myfile\ Name Here.txt