echo with color in shell script

  • The colour codes:
    Black        0;30     Dark Gray     1;30
    Blue         0;34     Light Blue    1;34
    Green        0;32     Light Green   1;32
    Cyan         0;36     Light Cyan    1;36
    Red          0;31     Light Red     1;31
    Purple       0;35     Light Purple  1;35
    Brown/Orange 0;33     Yellow        1;33
    Light Gray   0;37     White         1;37
  • Example:
    echo -e "${RED}Hello World${NO_COLOR}"

See also

grep to exclude lines with specific strings

grep with -v option will exclude the match lines in the output:
cat country.txt | grep -v "United States"

Tabs in bash script

echo with -e option will enable special characters:
echo -e "Name:\tJhon"

Python: check if file or directory exists

  • os.path.exists to check if file or directory exists.
    import os
  • os.path.isfile to check if file exists.
    import os

Python: Get file absolute path

Use os.path.abspath function:
import os

Clear history of QuickTime Player on Mac OS X

  • To clear recent items, File>Open Recent>Clear Menu
  • To prevent the history from being created, run the following commands in Terminal:
    defaults write NSRecentDocumentsLimit 0
    defaults delete RecentDocuments
    defaults write RecentDocuments -dict-add MaxAmount 0

Clear history of MplayerX for Mac

Open Terminal and type the following command:
defaults delete org.niltsh.MPlayerX.LSSharedFileList RecentDocuments
To prevent history from being created:
defaults write org.niltsh.MPlayerX NSRecentDocumentsLimit 0

Python: get the index of a char in a string

find and index method are available. The difference is that find returns -1 when what you're looking for isn't found, index throws an exception.
print(s.index('b')) # 1
print(s.find('b')) # 1
print(s.find('d')) # -1
print(s.index('d')) # exception

Python: string types in 2.x and 3.x

  • In Python 2.x, basestring is the base class for both str and unicode, while types.StringType is str. If you want to check if something is a string, use basestring. If you want to check if something is a ascii (bytestring), use str and forget about types.
  • Since Python 3.x, types not longer has StringType; str is always unicode. basestring no longer exists.

python: return inside with block

if returns value inside with statement, will the file be closed eventually?
The answer is YES

with open('myfile.txt') as f:
return [line for line in f if len(line)>80]

Ubuntu: system wide proxy settings

  • edit /etc/bash.bashrc
    export http_proxy=
    export https_proxy=
    export ftp_proxy=
    export no_proxy=localhost,,,*,172.23.65.*

python: iterables and iterators

A class with __iter__() method or __getitem__() method is iterable. The __iter__() method returns an iterator. A iterator object has a next() method (or __next__() method in python 3.0).

class Iterator(object):
def next(self):

class Iterable(object):
def __iter__(self):
return Iterator()

see also

python: len() function and __len__() method

len is a function to get the length of a collection. It works by calling an object's __len__ method.

class AClass(object):
def __init__(self):
self._list = [1,2,3]

def __len__(self):
return len(self._list)

print(a.__len__()) # 3
print(len(a)) # 3

python: call another static method

class AClass(object):
def __init__(self):

def a():

def b():

see also

Install thinkfan fan control utility on Lenovo thinkpad laptops


  1. Install thinkfan from repository:
    sudo apt-get install thinkfan
  2. Enable fan control in thinkpad_acpi module configuration:
    sudo echo "options thinkpad_acpi fan_control=1" > /etc/modprobe.d/thinkpad_acpi.conf
  3. Reload thinkpad_acpi module:
    sudo rmmod thinkpad_acpi; sudo modprobe thinkpad_acpi
  4. Set to start thinkfan automatically at boot time:
    sudo vi /etc/defaults/thinkfan


  1. Install lm-sensors:
    sudo apt-get install lm-sensors
  2. To check the temperatures of the sensors:
  3. To modify the thinkfan fan control settings:
    sudo vi /etc/thinkfan.conf