Commands

Debug you PHP code using DRUSH

Technologies: 

Drush is a very handy tool which makes development in Drupal very fast and easy. Almost everyting is JACA("just a command away"). This particualr tip will help you to evaluate your php code using Drush.

Examples:

Consider a function: ji_custom($node). To evaluate/debug this function I type following in Drush:

drush php-eval "ji_custom($node);"

Aliases for php-eval are: eval, ev

N.B: JACA is invented while writing this post :)

Batch Process Images in Linux

Sometimes its a pain when you want to modify, resize multiple files at once in Linux. Fortunately ImageMagick comes with a handy command called convert that makes the work simpler

Install ImageMagick in Ubuntu

sudo apt-get install imagemagick

Batch Process set of files in a folder and resize them to 1024X768

for file in *.jpg; do convert "$file" -resize 1024x768 "$file"; done

You may also try an app called as phatch for GUI related batch processing of images.

Snippet to bulk rename files by replacing strings in filename

Update File Path in Drupal

We have a site that we had been migrating since Drupal 4. Few days back when we tried to migrate the same from Drupal 6 to Drupal 7. There was an issue with respect to file location that was present since previous upgrades. To resolve that a symlink was created called "files" with linked to sites/default/files during D6 upgrade. This led to first loss of many files as Drupal does not handle symlink based file saving well. Secondly the files created were saved under public://files/filename.ext instead of public:://filename.ext which caused created files not being accessible.

Executing PHP in command line

Technologies: 

There are times when one needs to execute PHP in command like, for example trying the date command output or a one line execution. For trivial tasks, executing the same at the command line without the need for writing and executing at a file level is a great tool for time saving.

The argument to be used is : -r

Example:

php -r '
$a = range(0,2);
foreach($a as $b) {
   echo "entry: $b\n";
}

The output is:
entry: 0
entry: 1
entry: 2

Another example:
php -r '
> echo date("d-m-Y");
> echo "\n";
> '
14-01-2013

Understanding Server Bottlenecks

Technologies: 

If the server becomes slow or unresponsive, execute the top command.
The first thing to look is the load average.
The load average typically looks like this: load average: 0.49, 0.41, 0.35 . The first number represents the server load average currently, the 2nd number for load average 5 minutes ago and 3rd number for 15 minutes ago. if you have 1 CPU and the load average is 1, it means that the server has been busy constantly. Four 4 core machine if the average is 4, then all cores are busy.