Friday, October 17, 2008

Total RAM in your System

In order to find total RAM in your system, give following command:

$ cat /proc/meminfo | awk '/^MemTotal/ {print $2/1024 " MB"}'
2008.07 MB

If you all information related to memory:

$ cat /proc/meminfo

Total CPU cores in your system:

$ cat /proc/cpuinfo | awk '/^processor/ {ncores++} END {print "Total cores = " ncores}'
Total cores = 2

Thursday, October 16, 2008

Finding Processes, which are Accessing a File System

After completing every task related to CD/DVD/USB, you might find it annoying to unmount/eject CD/DVD/USB, while it is being used by some unknown/stuck process. Therefore I am providing a solution (fuser). Suppose your were accessing CDROM mounted at /mnt/cd/.

To view which files (of CD) are being accessed by whom :
$ fuser -mu /mnt/cd/*

If you are sure that, the processes are stuck/hang. Then you can kill all those processes :
$ fuser -kmu /mnt/cd/*

Warning!!!: If you are using writable medium (like USB stick), please give following command before killing processes:
$ sudo sync

Wednesday, October 15, 2008

Retrieving Date and Time from Unix Timestamp

In Unix, we can retrieve Date and Time from Timestamp by using following command:
$ date -r 1220000000
Fri Aug 29 14:23:20 IST 2008

But in Linux, there is no '-r' option. Therefore, I have written a Perl script to
retrieve date and time from timestamp. Here is the script:

$ vi ~/bin/

#!/usr/bin/perl -w

use strict;
use warnings;

my $ts = time;
if (@ARGV >= 1)
    $ts = $ARGV[0];
my ($sec, $min, $hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
print "$mday-$mon-$year $hour:$min:$sec\n";

# end dater.

Sample runs of above program:
15-10-2008 12:34:37
$ 1220000000
29-8-2008 14:23:20

PS: you can change parameters in print function in the program
according to output you desire.

Monday, October 13, 2008

Terminating Processes Containing Given Pattern

Generally, if you want to terminate a process which contains the given pattern, you use following 4 steps:

  1. $ ps aux | grep pattern 
  2. Copy the pid of the process from output of above command.
  3. Paste this pid in the following command
  4. $ kill -9 pid

The above procedure is cumbersome and requires much typing.

Therefore I have written a program to terminate process(es), which contains given pattern. (I am assuming that ~/bin is in your $PATH).

$ vi ~/bin/


# Kills the program given

if [ $# -lt 2 ]
  echo "Usage: $0 [1/0] signal program-name"
  echo "[0/1] Do not Kill / Do kill (optional)"
  echo "signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)"

if [ $# -gt 2 ]

pids=$(ps auxww | awk "/ $program / { print \$2 }")

for pid in $pids
  if [ $donothing -eq 1 ] ; then
  echo "kill $signal $pid"
  kill $signal $pid


By default, this program does not kills the process. You have to

pass 0 as second parameter to the command to kill.

Sample runs of above programs:

Usage: /home/mitesh/Programming/Shell/WCS/ [1/0] signal program-name
[0/1] Do not Kill / Do kill (optional)
signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)

$ -TERM ping
kill -TERM 16050
$ -KILL ping
kill -KILL 16050
$ 1 -KILL ping
kill -KILL 16050
$ 0 -KILL ping

The last command actually terminates(-TERM) or kills(-TERM).

Friday, October 3, 2008

Shell Session Recording

In order to record the commands and their output in a shell session,
you can use script command. For example:

[mitesh@linuxbox:370:~]$ script
Script started, file is typescript
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
[mitesh@linuxbox:373:~]$ exit

Script done, file is typescript

You can view the output file typescript file using your favorite editor.
$ cat typescript
Script started on Friday 03 October 2008 04:51:27 PM IST
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
[mitesh@linuxbox:373:~]$ exit

Script done on Friday 03 October 2008 04:51:54 PM IST

PS: you can save the output to any file, you want instead of typescript in
current directory. For example
$ script shell_session_20081003.log
$ exit
$ cat shell_session_20081003.log

Listing the Last Times a User Logged In

To output a list of top 10 recent system users:

$ last | head

To output a list of last 10 logins of a user (say: mitesh):

$ last mitesh | head

Note: This command is very useful in determining who has
logged in the system at particular moment. Helpful in forensics ;)

Wednesday, October 1, 2008

Viewing PDF Files on Terminal

First you need to convert a PDF document to HTML, then you run it through the elinks pager. There's a fine utility for doing just that, and it's called (appropriately) pdftohtml. You can find the home page for pdftohtml. If pdftohtml isn't already installed in your distribution of Linux, or isn't on your CD set, it's commonly available for Debian and RPM-based distributions, such as Fedora, SUSE, and more. The elinks program is also easily available if it isn't automatically installed in your distribution.
For example, you can install pdftohtml and elinks in Debian Linux with this command:
# apt-get install pdftohtml elinks

Users of the yum package can get the RPM version with this command:
# yum -y install pdftohtml elinks

Now you can view a PDF document with the following command. This particular command has one drawback. The output will not include frames (PDF files generally have a frame on the left that lets you jump to different pages).

$ pdftohtml -q -noframes -stdout document .pdf | elinks

If you want the left frame of page numbers, you can always use the following command instead:

$ pdftohtml -q document .pdf ; elinks document .html

You can write a script to save you all this typing each time you view a document. Use sudo or log in as root to create the /usr/local/bin/viewpdf script and enter the following code:


pdftohtml -q $1 ~/temp.html
elinks ~/temp.html

#end of script

This code assumes it's OK to store the temporary HTML file in your home directory. You can use another location if you prefer. Now save your work and make the file executable:

$ sudo chmod +x /usr/local/bin/viewpdf

Create your own personalized boot splash backgrounds for GRUB

The default GRUB bootloader screen is rather bland, but you can spice it up a little by creating your own custom graphical background screen for the bootloader.
GRUB imposes a number of limitations on the image size and number of colors. It also doesn't let you move the menu. The menu appears in a rectangle near the top of the screen, with some text instructions below the menu. This makes it relatively easy to create a graphical background screen for the GRUB bootloader, because you can focus primarily on making the bottom one-third of the screen interesting. That is not to say you cannot use other areas of the screen, but you should be careful. For example, don't make it difficult to read the GRUB instructions by placing complex graphics behind the text.

Here are the rather strict requirements for the image:
  • It must be sized at 640x480.
  • It must be a .xpm format image file (gzip compression is optional).
  • It must contain no more than 14 colors.Most people will cringe at the 14-color limit, but it is rather amazing what you can do with just 14 colors. Cartoon images are quite suitable for this narrow range of colors, and the narrow range of colors to represent the official Linux penguin (Tux) works fine.Find or create any image you want to try as a background for GRUB.

If you create an image yourself, it's best to create a 640x480 image and use as few colors as possible so that you don't lose the fidelity of the image when you later reduce it to 14 colors. Don't worry about using your graphics editor to limit yourself to 14 colors, however. It is possible to use the Gimp to reduce your image to use 14 colors, which can be a good way of fine-tuning the results you want.
Here is what you need:
A graphics editor, such as the Gimp, if you want to create or modify an image.
You must install ImageMagick if it is not already installed. Nearly all Linux distributions provide this on the install CD, and you can use your preferred package manager to install it.Suppose you have found or created the image myimage.png.
If you have ImageMagick installed, all you need to do to prepare the image is log in as root and issue these commands:

# convert myimage.png -colors 14 -resize 640x480 myimage.xpm

The convert command recognizes the extension png and knows what format it must convert the image from. It also recognizes the extension xpm and knows what format to convert the image to. The -colors 14 switch reduces the number of colors in the image to 14. If the image isn't already sized at 640x480, the switch -resize 640x480 will do that for you.

This is sample myimage.xpm

Changing RunLevel in Linux

When a Linux system is booted, the first process that the kernel starts is /sbin/init. It is always process id (PID) 1 and has a parent process id (PPID) of 0. The init process is always running.
The /etc/inittab file is the configuration file for /sbin/init. /etc/inittab identifies the processes that init starts, and it can be customized as desired. Few environment variables are set when a process is started by init.
The inittab lines have four colon-separated fields:


Let's look at the meaning of each.
(i). id The inittab id consists of one to four characters that identify the inittab line. The id must be unique.
(ii). runlevels The runlevels field contains one or more characters, usually numbers identifying the runlevels for which this process is started.
  • 0 System Halt
  • 1 Single user mode
  • 2 Local multiuser without remote network (e.g., NFS)
  • 3 Multiuser with network
  • 4 Not used
  • 5 Multiuser with network and xdm
  • 6 System reboot
You can change runlevel by changing this field, to the number
you want. And reboot the machine.