CodePie Coding for fun

2May/130

Extract pages from PDF in Linux

I am not usually have to extract pages from a pdf file, but when I do, I used Adobe(R) Acrobat 7.0 that resides on my old Windows XP machine (believe me, it's still work at this time). Today one friend asked me to extract a page from his pdf file, and I only have my MAC and Linux workstation with me. Fortunately, I figured out I can just use a single command to do this. Kudos to pdftk. For example:

  1. pdftk A=inputfile.pdf cat A1-10 output outputfile.pdf

This command will extract page 1 to 10 from inputfile.pdf to outputfile.pdf

Very handy. Happy extracting!

Tagged as: , No Comments
3Feb/130

How to install rtorrent and rutorrent

In order for rutorrent to work, rtorrent must be compiled with xmlrpc-c support. Here is the full instruction.

1. First, check to make sure the following libraries are installed on your system. At any step below, if you cannot run ./configure script correctly, you might have to install -dev or -devel packages of these libraries

  • gcc
  • ncurses
  • libsigc++-2.0
  • libssl
  • xmlrpc-c
  • subversion

2. Download and install xmlrpc-c if your distro does not support it

  1. svn co http://xmlrpc-c.svn.sourceforge.net/svnroot/xmlrpc-c/advanced xmlrpc-c
  2. cd xmlrpc-c
  3. ./configure
  4. make
  5. make install

3. Download and install libtorrent and rtorrent

  1. wget http://libtorrent.rakshasa.no/downloads/libtorrent-0.13.3.tar.gz
  2. wget http://libtorrent.rakshasa.no/downloads/rtorrent-0.9.3.tar.gz
  3. tar xzfv libtorrent-0.13.3.tar.gz
  4. tar xzfv rtorrent-0.9.3.tar.gz
  5. cd libtorrent-0.13.3
  6. ./configure
  7. make
  8. make install
  9. cd ../rtorrent-0.9.3/
  10. ./configure --with-xmlrpc-c=/usr/local/bin/xmlrpc-c-config
  11. make
  12. make install

4. Execute this line to copy rtorrent config to your home directory

  1. cp /usr/share/doc/rtorrent/examples/rtorrent.rc .rtorrent.rc

5. Add the following line to the end of your .rtorrent.rc

  1. scgi_port = 127.0.0.1:5001

6. Create download and session directories as pointed out in .rtorrent.rc. You might also change these directory as desired.

7. Download, extract rutorrent and plugins and placed them anywhere under your web root so that you can access it via HTTP

  1. wget http://rutorrent.googlecode.com/files/rutorrent-3.5.tar.gz
  2. wget http://rutorrent.googlecode.com/files/plugins-3.5.tar.gz
  3. tar -xf rutorrent-3.5.tar.gz
  4. cd rutorrent
  5. tar -xf ../plugins-3.5.tar.gz
  6. cd ..
  7. mv rutorrent /var/www/

8. Delete /var/www/ruttorrent/.htaccess to disable htaccess authentication (you can always enable it in the future)

9. Fire up your web browser and open http://yourdomain.com/rutorrent. You will see the GUI very similar to uTorrent.

Happy torrenting. Download responsibly.

1Jun/121

How to disable comment box from Facebook like widget

It have been a long time since the last time I used Facebook like widget on my blog. At that time, I feel that Facebook scripts take a lot of time to load, hence slowdown my site and affect its performance. Today, I changed my mind and add it back. With just 2 pieces of code (one load all.js at bottom and one load the like button), Facebook like button appears again on my site. Sweet and simple!

But wait. When I test by click on Like button, an ugly comment box appear:

As this is unexpected, I want to disable it. But Facebook Developer page does not give me any information about this. Fortunately, with just one simple CSS trick, I can hide that ugly comment box so the Facebook like become beautiful (like it should)

  1. .fb_edge_widget_with_comment span.fb_edge_comment_widget iframe.fb_ltr {
  2.     display: none !important;
  3. }

Happy hacking!

Tagged as: , 1 Comment
21May/120

How to find all files of a particular size in a particular directory

Last weekend, I worked on a Linux malware cleaning project. After 30 minutes of finding, I found 2 suspected scripts that are not included in Drupal by default. I soon realized that there are many of them, and I have a very limit access to his website (just normal account SSH access). Fortunately, after doing some research, I found that I can easily find all files of a particular size in his home directory. Simple like that: Suppose we want to find all files that has size = 1234 bytes and in /home/codepie directory, just type the following command

  1. find /home/codepie -type f -size 1234c -exec ls {} \;

You might wonder what does 'c' in command mean. As units you can use:

  • b – for 512-byte blocks (this is the default if no suffix is used)
  • c – for bytes
  • w – for two-byte words
  • k – for Kilobytes (units of 1024 bytes)
  • M – for Megabytes (units of 1048576 bytes)
  • G – for Gigabytes (units of 1073741824 bytes)

Additionally, you can also search for bigger (+) or smaller (-) files. Just add a plus (+) or minus (-) for bigger and smaller files, respectively.

  1. find /home/codepie -type f -size +1234c -exec ls {} \;

Have fun finding!

Tagged as: , No Comments
20Apr/120

How to generate SSH public key from private key

Normally you will generate and store a pair of SSH public and private key. But do you know you can generate public key from private key with ease? Here is the trick:

1. For OpenSSH key: Open terminal and type:

  1. ssh-keygen -y

2. For PuTTY key: Open PuttyGen program and choose Load key. Public key will show up.

Have fun.

Tagged as: , No Comments
12Apr/120

How to format bytes to kilobytes, megabytes, gigabytes in PHP

This is the best function I found on StackOverflow:

  1. function formatBytes($bytes, $precision = 2) {
  2.     $units = array('B', 'KB', 'MB', 'GB', 'TB');
  3.  
  4.     $bytes = max($bytes, 0);
  5.     $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  6.     $pow = min($pow, count($units) - 1);
  7.  
  8.     // Uncomment one of the following alternatives
  9.     // If 1 kilobytes = 1024 bytes
  10.     // $bytes /= pow(1024, $pow);
  11.     // If 1 kilobytes = 1000 bytes
  12.     // $bytes /= (1 << (10 * $pow));
  13.  
  14.     return round($bytes, $precision) . ' ' . $units[$pow];
  15. }
18Sep/110

How to know exactly which Linux distribution you are using

I myself usually have to connect to a lot of Linux server to troubleshot. As you know, different Linux distribution has different config file location. For example, Apache config file in CentOS server is /etc/httpd/httpd.conf but in Ubuntu/Debian is /etc/apache2/apache2.conf

It would be easier for us if we know exactly the distribution name from the beginning. Here are several solutions:

1. From the boot time message

  1. dmesg | head -1

You might see something like this:

  1. Linux version 2.6.31-14-generic (buildd@crested) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009

2. Using /proc/version

  1. cat /proc/version

You might see something like this:

  1. Linux version 2.6.31-14-generic (buildd@crested) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009

3. Using /etc/issue

  1. cat /etc/issue

You might see something like this:

  1. Debian GNU/Linux 6.0 \n \l

4. Using /etc/*-release

  1. cat /etc/*-release

You might see something like this:

  1. Gentoo Base System release 1.12.9

Disclaimer: The results are different because I'm using different machine when testing. have fun :)

17Sep/110

CSS: How to add width and height attributes to anchor tag

You might face this problem sometimes: You want to add width and height to an anchor instead of giving these attributes to its container. Surprisingly, width and height attribute are not works. For example, consider the following codes:

  1. a.ansok {
  2.     width:86px;
  3.     height:41px;
  4.     line-height:41px;
  5.     background-image: url(../images/layout/button.png);
  6. }
  7.  

And here is what you will get (instead of beautiful buttons):

So, what is the reason and how to fix it?

Well, anchor tags are not block level HTML elements but rather inline elements. So what is the difference between the two? Here is the definition of the two items:

Inline Element definition:

  • Inline elements typically may only contain text and other inline elements. When rendered visually, inline elements do not usually begin on a new line.

Block Element:

  • Block-level elements typically contain inline elements and other block-level elements. When rendered visually, block-level elements usually begin on a new line.

So, all you have to do is just add display:block to CSS class of anchor. And here is the result:

Some useful links:

HTML inline elements
HTML Block Elements

17Sep/111

How to install Ant correctly on Ubuntu

When using Ant the first time, you might notice that it return errors

  1. ant -version
  2. Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-6-openjdk/lib/tools.jar

Is it because of wrong jdk version? You probably tried to locate tools.jar and found this:

  1. locate tools.jar
  2. /usr/lib/jvm/java-6-sun-1.6.0.06/lib/tools.jar

Is it the same tools.jar required by ant? How can you point to this file so ant will not return any error? Here is the solution:

1. Make sure you have the "multiverse" repository

2. Install all Sun Java 6 packages

  1. sudo apt-get clean
  2. sudo apt-get update
  3. sudo apt-get install sun-java6*

This process might take a while, so you might need to grab a cup of coffee and wait.

3. Check that you are using the newly installed jdk

  1. javac -version

The output should be something like this:

  1. javac 1.6.0_06

4. Check that you are using the correct jre

  1. java -version

The output should be something like...

  1. java version "1.6.0_06"
  2. Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
  3. Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

If you find you are not using the correct Sun Java 6 jre you can fix it by:

  1. sudo update-alternatives --config java

The above command will let you choose which jre to make default if you have multiple jre installed.

Tagged as: , , , 1 Comment
16Sep/110

How to make Cufon works in IE9

IE, and probably Microsoft, has a lot of surprises for you. As you know, each IE version has its own behavior. And don't surprise if IE9 does not render Cufón at all

Meaning that every beautiful things you see in Firefox, Chrome, ... will not even show up in IE9. Nothing. You will only see a blank space.

Then, how to fix it?

First, you need to download the latest version of Cufón. It's 1.09i, which has support for IE9. Download here

Second, add the following lines to your code:

  1. <!--[if gte IE 9]>
  2.         <script type="text/javascript">
  3.                 Cufon.set('engine', 'canvas');
  4.         </script>
  5. <![endif]-->

And it works. Combination of the two method will give you a great look of your website in IE9.

Have fun.