Sunday, October 22, 2006

sed sample: to check duplicated packages

If there are duplicated packages installed in the FreeBSD system, you can use such script to check them out by the command sed:

pkg_info | sort | sed -e 's/-[0-9].*$//' | \
uniq -c | grep -v '^[[:space:]]*1'

Another easy sample, to replace pattern1 with pattern2 in a file:

  1. generate a backup file, foo.bak
    sed -i.bak s/pattern1/pattern2/g foo
  2. no backup files generated, notice there is no space after -i:
    sed -i'' s/pattern1/pattern2/g foo

Once, I use wget to download a whole website. However, the charset of these web pages I downloaded are no set correctly, which should orignally be utf-8. Thus, I resolved this problem with the method below.

  • Firstly, download the pages into a directory named myfavor:
    wget -m -E -Pmyfavor -k
  • Then, use this command to set the correct charset for all html files:
    find . -name "*html" -type f -print | xargs sed -i'' '/<\/title>/a\\<meta\ http-equiv="Content-Type"\ content="text\/html;\ charset=utf-8">'

Saturday, October 14, 2006

Speed your applications

Prelink is a powerful application, it allows us to pre-link the libraries needed for a binary before using it. So instead of looking for which libraries the binary will need when we launch it, prelink will modify the binary addind a little descriptions of the libs it nees to run. This get rid of the search for shared libraries everytime we launch the binary, so it makes it faster.
Important: Everytime that you upgrade the libraries that are needed for the binaries, (for example glibc) you have to re-run prelink on the system.

This is a little optimization that we will thank when launching big applications like KDE (moreover, if you prelink your system KDE will not need to launch kdeinit, so it will also run faster). The little binaries are already pretty fast, so will not appreciate the difference.

Requirements: it is a must to have compiled the binaries with binutils- and gcc-3.2 or higher, and also have installed glibc-2.3.1-r2 or higher. The size of the binaries will be bigger with prelink, and to run the process you need enough free space on the hard disk.

Way to go:

# emerge prelink

Find config file at /etc/prelink.conf

# prelink -afmR

This is the common use of prelink, that will prelink ALL the binaries, and also will look if the binaries were already prelinked, and prelink them again if needed.

It is possible that you get some errors when running prelink, because some binaries can not be prelinked (the ones compressed with upx, for example).

blogged from here,

Tuesday, October 10, 2006 no longer banned

This is a good news. is no longer banned by GFW, and we can directly access it from the mainland of China. Though can not be visited now, we still should celebrate it. Another surprise is that Google bought YouTube for $1.65 Billion at last, even though the latter has never known how to earn, and co-founders Steve Chen and Chad Hurley became a billionaire immediately. Google combined and seized this largest and fastest growing online video sharing community, while so expensively, comfort Yahoo!

Sunday, October 01, 2006

howto sleep and hibernate on thinkpad

In Windows, we can use Fn-F4 to suspend system to memory, and Fn-F12 to suspend system to disk. It is really very expedite and useful. When using Linux as a desktop, we can also have such way to sleep and hibernate our system and even faciler.

Sleep is also called suspend (to memory), while hibernate is called suspend2 ( to disk). Here is the specific steps:

  1. It is necessary to compile your kernel with ibm-acpi support, if you do not want to download it by yourself, and make sure your kernel source version is new enough, because the latest kernel has contained it. Modify your kernel configure file, add this sentence, which means that ibm-acpi will be compiled as a module.


    If you wanna actuate this module when system startup, usually it is. You should add "ibm-acpi" into, for example, /etc/modules.autoload.d/kernel-2.6.

  2. Install the ACPI daemon "acpid", and let it run when your system boots.
  3. Download the ibm-acpi package from here: We only need its scripts. Decompress the package, and copy the directory "config" into the root directory.
  4. Download the tpb package from here: Complie and install it.
  5. To make hotkeys work, you should add this into the startup script, /etc/conf.d/local.start:
    echo enable,0xffff > /proc/acpi/ibm/hotkey

    In order to use tpb successfully, you should have the privilege to read the device nram. Thus, add this script into local.start also:

    chmod a+r /dev/nvram
    echo 1024 > /proc/sys/dev/rtc/max-user-freq
  6. Install hibernate-script, which can be found here: If you just hope to sleep, you can write a script yourself. such as:
    echo "mem" > /sys/power/state

    It can also work successfully. If you want to use the whole functions of this hibernate-script, you have to patch your kernel source and recomplie it again. The patch can also be found at that website.

  7. Now, When you press ACPI key such as Fn-F4, then cat /var/log/acpid, you will find all ACPI events:
    [Sun Oct  1 17:54:44 2006] received event "ibm/hotkey HKEY 00000080 00001004"
    [Sun Oct  1 17:54:44 2006] executing action "/etc/acpi/ ibm/hotkey HKEY 00000080 00001004"
    [Sun Oct  1 17:54:44 2006] BEGIN HANDLER MESSAGES
    [Sun Oct  1 17:54:44 2006] END HANDLER MESSAGES
    [Sun Oct  1 17:54:44 2006] action exited with status 0
    [Sun Oct  1 17:54:44 2006] executing action "/usr/sbin/hibernate -F /etc/hibernate/ram.conf"

    And this means you can use your function key to suspend your system. If everything is ok, your system will sleep into memory just now.

  8. There is some other tips to use xmodmap, touch ~/.Xmodmap, write this into it:
    keycode 227 = F35
    clear lock
    add mod4 = Caps_Lock

    The first line let you can use Fn key, and the next two lines let you use Caps_Lock key as Windows Key. Run xmodmap ~/.Xmodmap, try CAPS_LOCK, and check if its action has changed. So cool :)

The further detail can be found here: