Friday, May 6, 2011

Ubuntu Linux: How do I install .deb packages?

dpkg is a tool to install, build, remove and manage packages. dpkg itself is controlled entirely via command line parameters. For example -i use to install .deb file.

How do I install .deb file?

To install package called package.deb type the following command:
Go to directory where package.deb is kept. For example if it is in /tmp directory:

$ cd /tmp

Type the following command:

$ sudo dpkg -i package.deb

Wednesday, February 2, 2011

VI Editor Tips

The VI editor is a screen-based editor used by Linux/Unix users. The VI editor has powerful features to aid programmers.  VI has two modes insertion mode and command mode.  The editor begins in command mode. Insertion mode begins upon entering an insertion or pressing 'i' in keyboard. [ESC] returns the editor to command mode.

Following tips may be helpful for programmers / content writers.
Execute the command from VI Editor command mode. (press Esc, If Insert mode)


Insert space/character between 2 adjacent characters

Example 1
'LinuxFor' becomes 'Linux For' (Sentence Case)

:%s/\([a-z]\)\([A-Z]\)/\1 \2/g

Example 2
'Kerala government' becomes 'Kerala Governemnt'

:%s/\([a-z]\) \([A-Z]\)/\1 \U\2/g


Removes all lines starts with a word/patterrn.

Removes all lines begin with 'Ele'

:g/^Ele.*$/d

Removes all lines end with 'are'

:g/$*.are/d

Removes blank lines

:g/^$/d

Make First Letter Capital

:%s/^./\U&/

Monday, January 3, 2011

Find out a file which contains specific string

To find out a file which contains specific string, execute the following command from shell


# find /var/www/html/mysite-iname '*php' | xargs grep 'about' -s1

Here, searchesfor file which contains string 'about' .

Saturday, December 18, 2010

Compress and split large files under linux

One of the common problems we used to face is transferring large files from Linux through mediums that doesn't support large files, especially when it comes to more than 4 GB files, usually that is a virtual machine hard disk, or backup file.

To compress and split files under linux, this command can be used:


$ tar -cvj big-file.dvi | split -b 650M -d - "small-file.tar.bz."

This command will compress and split the "big-file.dvi" to many files with size 650MB for each, giving it the name "small-file.tar.bz.0, small-files.tar.bz.1, etc...."

To join the files together again, this command can be used:

$ cat small-file.tar.bz.* > big-file.tar.bz

and to extract the output file :

$ tar -xvj large-file.tar.bz

Sunday, November 14, 2010

rsync - The Linux Backup Command

rsync stands for remote sync.

rsync is used to perform the backup operation in UNIX / Linux.

rsync utility is used to synchronize the files and directories from one location to another in an effective way. Backup location could be on local server or on remote server.

Important features of rsync

  • Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
  • Security: rsync allows encryption of data using ssh protocol during transfer.
  • Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
  • Privileges: No special privileges are required to install and execute rsync

Syntax

$ rsync options source destination

Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Example . Synchronize Two Directories in a Local Server

To sync two directories in a local computer, use the following rsync -zvr command.

$ rsync -zvr /opt/lampp/htdocs/site1/ /backup1/site1bkp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes received 1098 bytes 54966.00 bytes/sec
total size is 44867 speedup is 1.63
$

In the above rsync example:

  • -z is to enable compression
  • -v verbose
  • -r indicates recursive

Tuesday, October 12, 2010

What is Crontab ?

What is Crontab?
Crontab (CRON TABle) is a program that manipulates the CRON daemon, making it easy for users to schedule task and run programs/commands at pre determined periods of time. Crontab can also be considered a file witch contains commands that will be run by the system as the user that owns the crontab file.

What is the purpose of Crontab?
Cron is designed to maintain a list of commands that the system needs to run at a given time interval.

For example if you have a script that generates statistics and needs to be run every couple of hours or everyday cron can do it for you. Or for example if you have a script that sends a newsletter every month you can use cron to run the script that sends the newsletter every month or week.

Crontab commands
When your logged in to your server you can use program cron using the following commands:

crontab -l
Lists the current cron jobs
crontab -e
Edit your current crontab file and ad/remove/edit crontab tasks.
crontab -r
Remove the crontab file.
crontab -v
Displays the last time you edited your crontab file.

The crontab file - components of crontab

When you enter the edit mode (crontab -e) and start adding tasks to your cron file you need to consider the following syntax:

The asterisk (*) symbolizes that every instance of that field (i.e. every minute, hour, day, month, weekday) will be used in the command.

Example on how to setup your first crontab
Lets say you have a script named run-me.sh located in /home/your_username you want to run every day at 13:30. You will need to login your server console and input the following commands:

crontab -e

This will open the crontab file and let you edit it. By default this file will be opened with the VI editor and you will need to press the "Insert" key on your keyboard to be able to write in that file.

30 13 * * * /home/your_username/run-me.sh >/dev/null 2>&1

The first character you see is "30" this means that crontab will run the script every time the clock hits the 30 minutes mark. Next "13" this means that crontab will run the script when the clock hits 13. The next three * tell crontab to run the script every day, of every month of every weekday. Combining these fields crontab will run the script every day at exactly 13:30. You may notice that we added the ">/dev/null 2>&1" string at the end of the command. The default cron job will always send and e-mail to the root account when ever a command is executed. Now you don't want to be notified every day that your crontab job has been executed. If you don't want to receive e-mails every day notifying you about your job's execution place this ">/dev/null 2>&1" at the end of every instance of every crontab command.

Now to list your crontab job just issue the following command: crontab -l
If you need to ad another crontab job or even more all you need to do is follow the same steps as above and just ad another line to the file.

REMEMBER: Every crontab job needs to be placed on its own line in the file and after the last line you need to insert a non-braking character (press Enter).

The crontab syntax goes a little beyond its boundaries and has more advance meaning for some users.
For example if you wish to use more then one instance of one column you will separate those instances by a comma "," or if you wish to use a time period lets say for example from Sunday till Tuesday you will use dash "-".

10,20,30 13 * * 0-2 /home/your_username/run-me.sh >/dev/null 2>&1

This crontab job will run your scrip "run-me.sh" on from Sunday until Tuesday (Sunday, Monday, Tuesday) at 13:10, 13:20 and 13:30. Remember there are no spaces between values separated by commas "," and neither in dashes "-". There is also an operator that some versions of cron support called the slash operator "/" that can be used to skip a given number of values in your jobs.

Crontab examples - cron examples
The syntax of crontab is not very easy to understand from the start and the best way of understanding is from examples:

Run the script every day at 12:00.
0 12 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every day at 12:00, 14:00 and 16:00.
0 12,14,16 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Sunday at 13:30.
30 13 * * 0 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every Saturday at 12:00, 14:00 and 16:00.
0 12,14,16 * * 6 /home/your_username/run-me.sh >/dev/null 2>&1

Run the script on the first, fifteenth and twentieth of every month.
0 0 1,15,20 * * /home/your_username/run-me.sh >/dev/null 2>&1

Run the script every day from 3 to 3 hours: 00:00, 03:00, 06:00 etc.
0 */3 * * * /home/your_username/run-me.sh >/dev/null 2>&1

Thursday, July 29, 2010

Install JSON PHP Extension on RedHat / Fedora

To enable JSON Support in php, do the following in Redhat / Fedora Server


  1. Execute the following Command from root account
    # pecl install json
  2. Ensure that json.so exists in folder /usr/lib/php/modules
  3. If exists, add the following line to /etc/php.ini
    extension=json.so
  4. Restart Apache (# service httpd restart)
  5. Execute phpinfo() to verify JSON enabled