Unaliased Fronts and KDE

Sep 24
2008

All of us (the people that hate vista and apple styled aliased fonts) have seen the ugly fonts in some KDE apps (Firefox for example). But this is not the case with XFCE where evreything works fine.
So here is a fix for KDE (just run the following command):

$ cat ~/.gtkrc-2.0
gtk-font-name = "Arial 10"

Remove All Empty Directories

Aug 04
2008
alias _rmdir_empty='rmdir * 2>/dev/null'

Converting Between Numeric Bases

Jul 31
2008

Sample .bashrc file:

# decimal to binary
alias d2b='ruby -e "puts ARGV.first.to_i.to_s(2)"'
# decimal to hexademical
alias d2h='ruby -e "puts ARGV.first.to_i.to_s(16)"'
# decimal to octal
alias d2o='ruby -e "puts ARGV.first.to_i.to_s(8)"'
 
# binary to decimal
alias b2d='ruby -e "puts eval(%q|0b| + ARGV.first)"'
# binary to hexademical
alias b2h='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(16)"'
# binary to octal
alias b2o='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(8)"'
 
# hexademical to decimal
alias h2d='ruby -e "puts eval(%q|0x| + ARGV.first)"'
# hexademical to binary
alias h2b='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(2)"'
# hexademical to octal
alias h2o='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(8)"'
 
# octal to decimal
alias o2d='ruby -e "puts eval(%q|0o| + ARGV.first)"'
# octal to binary
alias o2b='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(2)"'
# octal to hexademical
alias o2h='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(16)"'

TRAC – change SVN repository

Jul 07
2008

Go to your TRAC project dir and issue the following commands:

1. cd to_your_trac_project_dir

2. ruby -i -pe ’$_.sub! /(repository_dir\s=\s).+/, “repository_dir = NEW_repository_PATH”’ trac.ini

3. trac-admin . resync

downloader

Jun 09
2008
#!/usr/bin/perl
 
use strict;
use warnings;
use WWW::Mechanize;
use LWP::Simple;
use Data::Dumper;
use Time::HiRes qw( usleep tv_interval gettimeofday );
$|++;
my $url  = shift || die "no url given!\n";
my $ext  = shift || 'mp3'; #die "no file extention given!\n";
my $mech = WWW::Mechanize->new(agent=>"Mozilla/5.0");
my $ua   = LWP::UserAgent->new;
my %seen;
 
$mech->get($url);
$ua->timeout(5);
 
my $links = $mech->links;
 
no warnings;
my $c = 0;
for my $link ( @{$links} ) {
        my $url  = $link->url_abs;
        my $res  = $ua->head($url);
        my $http_res   = HTTP::Response->new($res);
        my $abs_name   = $http_res ->{'_rc'}->{'_previous'}->{'_headers'}->{'location'};
        my ($rel_name) = $abs_name =~ /.+\/(.+)$/;
        my $local_name = $rel_name;
 
        $seen{$local_name}++ and next;
 
        if( $rel_name =~ /\Q$ext\E$/i ){
                $rel_name =~ s/\%\d+/ /g;
                $rel_name =~ s/(.{35}).+/$1.../;
                print(pack('A45', "[$rel_name]"), "  is being downloaded ... ");
                my $s_time = q{};
                my $e_time = q{};
                my $flag = [];
 
                for( 1..5 ){
                        local $SIG{ALRM} = sub { die("timeout") };
                        eval {
                           alarm(10);
                           $flag = [head($abs_name)];
                           alarm(0);
                        };
                        next if $@ =~ m|timeout|;
                        last if $@ !~ m|timeout|;
                }
 
                if( $flag->[0] ){
                        $c++;
                        $s_time = [gettimeofday];
                        getstore($abs_name, $c."-".$local_name);
                        $e_time = tv_interval ($s_time, [gettimeofday]);
                }
 
                $e_time =  $e_time ? $e_time . " seconds" : 'less than 1 milisecond';
                print "timeout failure in $e_time seconds :(\n" and next if $@;
                print "done in $e_time\n";
        }
}
 
`ruby -e 'Dir["*"].each{|f| File.rename f, f.sub(/^\d+-/, "")}'`;
`ruby -e 'Dir["*"].each{|f| File.rename f, f.gsub(/%20/, "_")}'`;

Ruby system administration scripts

Mar 22
2008

OS users:

users = File.new("/etc/passwd").collect{|x| x.split(':')[0]}

Number of files in directory:

puts %x{ls}.split(/\n/).size
puts %x[ls|wc].split(/\s+/)[1]

Network interfaces:

inet = Hash.new{|h,k| h[k.split(/\s/)[0]] = k.scan(/addr:(\d+\.\d+\.\d+\.\d+)/)[0].to_s}
 
%x[ifconfig].to_s.split("\n\n").collect{|x| inet[x]}
 
# dump the structure: puts inet.inspect

to be continued….

find command (practical books’ dir copy)

Feb 03
2008

Copy all those formats (pdf|chm|txt|html) from $HOME/Desktop to the current working directory:

find  ~/Desktop/ -iname "*.pdf" \
  -o -iname "*.chm" \
  -o -iname "*.txt" \
  -o -iname "*.html" \
-exec cp -v {} . \;

more about find command

perldoc usage

Jan 09
2008

function manual:

perldoc -f grep

search the text in perlfaq:

perldoc -q grep

module content:

perldoc -m IO::Socket

module absolute filename:

perldoc -l IO::Socket

and of course:

perldoc perl

Special characters in file name

Jan 09
2008

Safely manipulate file with special characters in the name:

ruby -rfileutils -e 'FileUtils.rm_r "~"'

“Argument list too long”

Jan 08
2008

Given that linuxjournal has some good points, there is no need for another solution, except in the cases when you operate on different machine. Here is oneliner solution:

irb console #remove Opera cached files

too_many_files = "~/.opera/cache4/"
 
Dir.new(too_many_files_dir).each do |f|
  File.file?(f) && File.unlink(f)
end

Calendar

July 2010
M T W T F S S
« Apr    
 1234
567891011
12131415161718
19202122232425
262728293031  

Tags