Tuesday, January 13, 2009

Playing Directory in Mplayer

In order to play media files inside a directory with mplayer, I have written a shell script. Please save it in a directory in your $PATH variable.

For example, ~/bin/mplayer_dir.sh

#!/bin/bash


# Plays files inside a directory with mplayer

playlist="/home/mitesh/.mplayer/playlist.txt"

dir="./"
if [[ $# > 0 ]]
then
dir=$1
fi

#rm -f $playlist
find "$dir" -iname "*" | egrep -i "\.(wma|wmv|flv|mp3|avi|vob|dat|mp4|m4v|ogg|divx|xvid|rmvb|rm|asf)$" | sort > $playlist
#cat $playlist
nlines=`wc -l $playlist | awk '{print $1}'`

if [[ $nlines > 0 ]]
then
mplayer -playlist $playlist
else
echo "$0: Unrecognized files in $dir"
echo "Please update this script if playable format is present."

fi

--------------

Sample Run:

$ mplayer_dir.sh [directory]

Friday, November 28, 2008

Ping Every Host in /etc/hosts File.

If you want to know whether hosts specified in /etc/hosts file are reachable, just give the following command:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1

Sample run:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.028 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.028/0.028/0.028/0.000 ms


Friday, November 7, 2008

Missing Linux Entry in GRUB Menu

If any Linux entry is not present in GRUB menu,
you can visit following forum to view a solution.

Linux Forum: Unable to see Fedora 7 in GRUB Menu

Wednesday, November 5, 2008

Finding Best Linux Distribution

We can find ranking of Linux distributions (distros) at site distrowatch.com. This ranking is based on Page Hits Per Day.In order to find best Linux distro, I have written a Perl script:

#!/usr/bin/perl -w
#===============================================================================
#
# FILE: best_distro.pl
#
# USAGE: ./best_distro.pl
#
# DESCRIPTION: Finds best distro from distrowatch.com
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh)
# VERSION: 1.0
# CREATED: Tuesday 04 November 2008 04:05:19 IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

my $ndistro = 1;

if (@ARGV >= 1)
{
$ndistro = $ARGV[0];
if ($ndistro > 100)
{
$ndistro = 100;
}
}

my $lynx = `which lynx`;
chomp($lynx);
if ($lynx eq "")
{
print STDERR "$0: please install lynx\n";
print STDERR "sudo apt-get install lynx\n";
exit(-1);
}

open(LYNX, "lynx -source http://distrowatch.com/ |") or die("$0: cannot open lynx: $!\n");

my $i = 1;

while ($i <= $ndistro)
{
my $line;
my $distro;
my $rank = "";
while ($line = <LYNX>)
{
chomp($line);
$line =~ s/<th class=([^>]*)>(.*?)<\/th>/$2/i;
$line =~ s/(\s+)//g;
if ($line eq "$i")
{
$rank = $line;

unless ($line = <LYNX>) {last;}
chomp($line);
$line =~ s/<td class=([^>]*)><a href=([^>]*)>(.*?)<\/a><\/td>/$3/i;
$line =~ s/(\s+)//g;
$distro = $line;

print "$rank\t$distro\n";
last;
}
}
++$i;
}

close(LYNX);

Sample runs of above program:
$ ./best_distro.pl
1 Ubuntu
$ ./best_distro.pl 5
1 Ubuntu
2 openSUSE
3 Mint
4 Fedora
5 Debian

Tuesday, November 4, 2008

Counts from min to max

In order to get counts from start number to end number,
I have written a shell script.


#!/bin/sh

if [ $# -ne 2 ]
then
echo "Usage: $0 min max"
exit
fi


min=$1
max=$2

while [ $min -le $max ]
do
echo -n $min " "
min=`expr $min + 1`
done
#end script



Sample run:
$ ~/bin/count.sh 2 5
2 3 4 5

Tip: This is very handy script, that is very helpful
in getting counts in the loop. For example:
$ for i in `~/bin/count.sh 2 8`
> do
> echo "Hello $i"
> done

Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8

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


Extra:
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