SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Florida
    Posts
    264
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SSH Commands - Deleting files and escaping characters

    I have two quick questions.

    Question 1
    I had a script create a backup of every file on my site using the following format "filename.php.bac". I want to delete these files now and I tired to use "rm *.bac" but that only deleted the files in the current directory. How can I delete ALL those files in EVERY directory and sub-directory starting at the public_html directory?

    Question 2
    How can I escape semi-colon's (;) in a perl script? I'm trying to run a search+replace script to update some Analytics code and I have a ton of files to update but for some reason if there is a semi-colon in the find varable, it assumes that it has reached the end of the contents in that variable.

    Here is the code. Take a look at the $find variable and you will see extra semi-colon's. How do I tell the script to not treat those semi-colons as the end of the variable?

    Code:
    #!/usr/bin/perl 
    
    use strict; 
    use warnings; 
    use File::Find; 
    
    my $startdir = '/home/site/public_html'; 
    
    my $find = "<script src=\"http://www.google-analytics.com/urchin.js\" type=\"text/javascript\">
    </script>
    <script type=\"text/javascript\">
    _uacct = \"UA-1234567-8\";
    urchinTracker();
    </script>
    ";
    
    my $replace = "";
    my $doctype = 'php';  
    
    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";

  2. #2
    SitePoint Enthusiast Syam's Avatar
    Join Date
    Mar 2006
    Location
    Kochi, India
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Answer for Question 1:

    find /path/to/folder -type f -name "*.bac" -delete

  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Florida
    Posts
    264
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Syam, when I try that I get the following error:

    find: invalid predicate `-delete'

  4. #4
    SitePoint Enthusiast Syam's Avatar
    Join Date
    Mar 2006
    Location
    Kochi, India
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then try this:

    find /path/to/folder -type f -name "*.bac" -exec rm -rf {} \;

    In my Ubuntu, both work.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Florida
    Posts
    264
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Syam, thank you sir. That worked perfectly!

    Anyone have any insight about question 2?

  6. #6
    SitePoint Wizard silver trophy
    beley's Avatar
    Join Date
    May 2001
    Location
    LaGrange, Georgia
    Posts
    6,117
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As long as the semicolon is in a string in quotes, it will be fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •