Yearly Archives: 2011


Include more filetypes to a WordPress Installation Media Library

In the folder <Installation Path>/wp-includes/ there is a file called functions.php, inside this file there is a function called get_allowed_mime_types().
This function is in charge of defining an Array of mime types keyed by the file extension with a regex corresponding to those types.

So in order to add a new file-type to the white-list you just have to add a new line with the following format in the list:

'jpg|jpeg|jpe' => 'image/jpeg',

Where, on the left you define the extensions you want to be accepted separated by the vertical bar character (“|”) and on the right you give a file definition.


Bash: Read a file, line by line

The following script will accept from the keyboard a filename, later it will read it and process it line by line.
In this example we will just number the lines, print them and count the total number of lines in the file.

#!/bin/sh
echo Enter the Filename to process
read filename
exec<$filename
lineNumber=0
while read line
do
	lineNumber=`expr $lineNumber + 1`;
	echo $lineNumber - $line;
done
echo "Total Lines Number: $lineNumber";


Remove leading characters/information from line using sed

Remove leading numbering from line

We had these log files that on most lines at the beginning there was a number followed by a dot and some times it had space characters.
The following sed command removes that prefix. and leaved intact the rest of the lines that do not have the prefix.

cat $log | sed '/^[0-9]*. */!d; s///;q';

e.g

Input: 123. Some text here.
Output:Some text here.

Remove leading whitespace (space and tabs) from line:

The following sed script will remove all leading whitespace from each line by using a regular expression to match space characters and tabs.

cat $log | sed 's/^[ t]*//';

e.g

Input:       Some text here.
Output:Some text here.

Extract filename from full path filename / Get file extension

The first command strips down the full path filename to the filename only ising the basename command.

filename=$(basename $filenamefullpath)

Afterwards you can see how to extract the file extension from the filename.  There is no need to do this after issuing the above command since this command will just remove everything after the first from right dot (‘.’) — so make sure that the filename you are parsing has a dot or you will end up with wrong results (like the full path or a part of the full path if it contains a dot somewhere).

extension=${filename##*.}

Finally, by issuing the following command you remove everything after the first dot on the right (including).

filename=${filename%.*}