Monday, December 7, 2009

Find Biggest Files/Directories





Since ls does not give correct size on disk, better to
use du command.





$ du /path/to/dir | sort -nr




I hope above
command will give you the proper result you wanted.



Note: The above command may take large time, depending on

number of files in /path/to/dir . You can use depth(say 2) in
that dir.




$ du --max-depth=2 /path/to/dir | sort -nr






Tuesday, November 17, 2009

Newer Yahoo! Messenger Protocol on Pidgin on Debian Lenny 5.0

Yahoo! has changed authentication method of Yahoo! messenger protocol, which is not supported in Pidgin (Version <>= 2.5.7) supports newer Yahoo! Messenger Protocol. But Debian 5.0 Lenny repository has older pidgin (2.4.3). We can install newer pidgin, in the following steps:

## download latest pidgin from pidgin.im (at this time 2.6.3 is latest)
$ wget http://sourceforge.net/projects/pidgin/files/Pidgin/pidgin-2.6.3.tar.bz2

## extract files using tar
$ tar -zxvf pidgin-2.6.3.tar.bz2

$ cd pidgin-2.6.3

## Configure, Compile, and make
$ ./configure --disable-screensaver --disable-vv --disable-avahi --disable-tcl --disable-tk --prefix=/usr
$ make
$ sudo make install


Now you can use newer pidgin with newer Yahoo! Messenger. :)
Enjoy Yahoo!

Find Files Created Between 2 Times

In order to find files created between two times (start hour and end hour). The required hours are hours from current time. For example,

Current time = 2 PM = 14:00

If you want file created between 9 AM and 12 PM today, the start hour and end hour are:

Start Hour = (14 - 9) = 5
End Hour = (14 - 12) = 2

Hence required command is:


$ ./find_files_between_times.pl /path/to/dir 5 2



The Perl script which does this is given below:-


#!/usr/bin/perl -w
#===============================================================================
#
# FILE: find_files_between_times.pl
#
# USAGE: ./find_files_between_times.pl <dir> <start_hour> <end_hour>
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
# COMPANY: Yahoo Inc, India
# VERSION: 1.0
# CREATED: 11/13/2009 10:00:02 PM IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

if (@ARGV != 3)
{
print STDERR "Usage: $0 <dir> <start_hour> <end_hour>\n";
exit(-1);
}

my $dir = $ARGV[0];
## Calculate current_hour - given_hour
#my $start_time = `/bin/date "+\%H"`;
my $start_time = $ARGV[1];
#chomp($start_time);
my $end_time = $ARGV[2];
$end_time = time - ($end_time * 60 * 60);
#$end_time = $start_time - $end_time;
#$start_time = $start_time - $ARGV[0];

if ($start_time > $end_time)
{
($start_time, $end_time) = ($end_time, $start_time);
}

my $cmd = "find $dir -ctime -$start_time";
print "Running $cmd\n";
my @files = `$cmd`;
foreach my $file (@files) # (`find $dir -ctime $start_time`)
{
chomp($file);
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);

if ($ctime > $end_time)
{
next;
}
print "$file\n";
}


exit(0);


Monday, October 26, 2009

Better Console Calculator Using bc

If we use expr for mathematical calculations on console (terminal), it is frustrating, and hard to remember syntax and escape sequences used in expr. :( This small tip will help us.

Just add following line in your .bashrc file (~/.bashrc).

function calc
{
echo "${1}" | bc -l;
}

Then update the shell environment by:

$ source ~/.bashrc


"calc" from the shell will work as follows:

$ calc 2+3
5
$ calc 1+2*3
7
$ calc 1.1*2
2.2
$ calc "(1+2)*3"
9
$ calc "(1+2)^3"
27
$ calc "s(.5)"
.47942553860420300027

Friday, October 23, 2009

Checking Network Services Using Telnet

Suppose we want to check whether the web server(port 80) or any other network service is running or not, but we do not have any browsers (lynx, Firefox, IE, Netscape, ...), then there is a simple way of doing that. Just telnet to that machine on the http port (port no. 80 in general).


$ telnet <ip_addr> <port_no>


And then, give command "get /" (without quotes). If the webserver is running, it displays the HTML script of the homepage or basic info and closes the connection to the remote host.



$ telnet wordpress.com 80
Trying 76.74.254.126...
Connected to wordpress.com.
Escape character is '^]'.

get /
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Connection closed by foreign host.


Tuesday, October 20, 2009

Joining Multiple Videos into One Using Mplayer

In order to join multiple video files, into a single video file, you just need to issue following command:



$ mencoder -oac copy -ovc copy -o joined_single_video.avi video1.avi video2.avi video3.avi




Even you can use wild cards (*, ?) too.



$ mencoder -oac copy -ovc copy -o joined_single_video.avi video*.avi




PS. In place of 'avi', you can give whatever file format is available.

Monday, October 5, 2009

Converting Improper mp3 into Proper mp3 for Nokia E71

The music player of Nokia E71 is vulnerable to improper mp3. It stucks forever, even if there is a single improper mp3. Improper mp3 is a mp3 file which does not conform to mp3 standard, there are some problem in mp3 headers. For more information on improper mp3, please read manual of checkmp3 command.

$ man checkmp3

I have written a shell script which converts improper mp3's into proper mp3.



#!/bin/bash

## Script to fix mp3 files using checkmp3

## Checking existance of checkmp3
checkmp3=`which checkmp3`

if [[ "$checkmp3" == "" ]]
then
echo "$0: please install checkmp3."
exit
fi

if [ $# -ne 1 ]
then
echo "USAGE: $0 <mp3_file|dir_with_mp3s>"
exit
fi

dir="$1"
file=""

if [[ -d "$dir" ]]
then
echo "processing directory '$dir'"
temp_file="$dir/fixed.mp3"
for file in `find "$dir" -iname "*.mp3" | sed 's/ /\\\_/g'`
do
file=`echo "$file" | sed 's/\\\_/ /g'`
echo "processing file '$file'"
$checkmp3 -i -sf "$file" > "$temp_file"
if [ $? -ne 0 ]
then
echo "$0: error in processing file '$file'"
else
#eyeD3 "$temp_file"
#$checkmp3 "$temp_file"
mv "$temp_file" "$file"
fi
done
rm -f "$temp_file"
else
file="$dir"
echo "processing file '$dir'"
dir=`dirname "$file"`
temp_file="$dir/fixed.mp3"
echo "$temp_file"

$checkmp3 -i -sf "$file" > "$temp_file"
if [ $? -ne 0 ]
then
echo "$0: error in processing file '$file'"
else
#eyeD3 "$temp_file"
#$checkmp3 "$temp_file"
mv "$temp_file" "$file"
fi
fi
exit 0



Sample run:

$ ./fix_mp3.sh xyz.mp3
$ ./fix_mp3.sh /path/to/mp3/directory/