Z-Car

Linux Question of the Day – How do I Grep Recursively?

redheaded lady using laptop

Another common question that I hear on a weekly basis. You would think this would be a pretty straightforward answer, and it is. I think the shear number of options available with Grep is what confuses folks.  So, here is the basic way to perform this task.

grep -r “texthere” .

Simple, right?  “texthere” is the string that you are searching for, and the -r says search recursively starting from the current directory (.).  You can also specify specific filenames or types that you would like to search, such as *.txt, *.php, etc.

On some older Unix versions, you may find that Grep does not support the -r syntax.  In that case, try the following :

find ./ -type f | xargs grep “texthere”

Also som version also will not support searching for *.txt as the filename, in that case, try the following :

find /dir/to/search/ -iname *.txt -exec grep ‘texthere’ ‘{}’ ;

Little known piece of trivia, GREP stands for Get Regular Expression and Print


PHP Code to Open Zip Files and Extract the Contents

computerGirl on sofa

I recently was working on a project for a dynamic website that presented a huge collection of files available for download. All of the files (over 25,000) were stored in the ubiquitous ZIP format.  This is great for reducing the amount of disk space required, however it can make it challenging to work with them.  What we wanted to do was allow the visitors to the site to be able to review the contents of the file, and view any of the files that are contained within the ZIP file.  PHP has some handy functions that allow you to manipulate ZIP files, however they are not well documented.  Although fairly straight-forward, I am including some example code here that will allow you to quickly copy and paste it for your requirements.

The core routines include zip_open() that opens the ZIP file for use.  zip_read then allows you to transverse the ZIP directory to identify the files contained within.   The zip_entry routines provide additional details about the file enclosed, and zip_entry_open() provides the door that allows you to open a specific file and then zip_entry_read allows you to extract the contained file.   On our site, the contained files were mostly text files that could be easily displayed.  Simply iterate through the file collecting the contents into a temporary variable.  If displaying on a webpage, use the handy nl2lbr() function to convert line feeds into HTML line breaks.

All in all, PHP’s built-in ZIP functions are a handy tool, a few simple lines of code will allow you to easily manipulate ZIP files and allow individual files to be viewed.  Leave a comment if you have any questions or suggestions.  You can see the final site at The Programmer’s Corner.

Code that will allow you to open a ZIP file, iterate through the directory, and retrieve the files contained within.

   $zip = zip_open('PCorner/' . $_GET['category'] . '/' . $_GET['file']);
    while ($zip_entry = zip_read($zip)) {
        $output.= '<tr class="' . $class . '">';
        $output.= '<td>';
        $file = strtoupper(basename(zip_entry_name($zip_entry)));
        $size = zip_entry_filesize($zip_entry);
        $csize = zip_entry_compressedsize($zip_entry);
        $type = zip_entry_compressionmethod($zip_entry);
    }

Code that will allow you to open a ZIP file, find a specific file, and extract it for viewing, or further manipulation.

   $zip = zip_open('PCorner/' . $_GET['category'] . '/' . $_GET['file']);
        while ($zip_entry = zip_read($zip)) {
            $file = basename(zip_entry_name($zip_entry));
            if (strtoupper($file) == strtoupper($_GET['operation'])) {
                if (!zip_entry_open($zip, $zip_entry)) {
                    die('');
                }
                while ($data = zip_entry_read($zip_entry)) {
                    $output.= nl2br($data);
                }

            }
        }
   echo $output;

= does not == === | Learn about === alias The Triple Equals

lady_with_computer

I think this just may be the most important piece of information for any software developer today.  Whatever you do, don’t forget it.

Although many software developers have been using PHP and Javascript for years, many do not know about ===, also called “The Triple Equals”. In the simplest terms, it means equality without type coercion. In other words, if using the triple equals, the values must be equal in type as well as value.

  • 0==false // true
  • 0===false // false, because they are of a different type
  • 1==”1″ // true, auto type coercion
  • 1===”1″ // false, because they are of a different type

Many developers will first encounter === when using JSLint to test their Javascript. JSLint was written by Douglas Crockford, one of the more outspoken proponents of Javascript and JSON.

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===).
Many experienced JavaScript and PHP developers will advocate ALWAYS using === and !=== instead of == and !=.  The reasons are obvious, you will never find yourself finding out that:
  • 0 == ” is true
  • false == ‘false’ is false
  • false == ‘0’ is true

and the non-transitive demonstration is as follows:

  • false == undefined is false
  • false == null is false
  • null == undefined is somehow true?