Paul - The Programmer

simple & stupid

Good Linux Tool, find out the biggest file / directory in the system

No matter how big your hard disk is, it can still run out of space someday. So, do some regular cleaning up work is necessary. Free some space by deleting the files which are out of date or not needed anymore.

To get the space consumed by each directory with du and find out the biggest one with sort.

 

$du --max-depth=1 -h | sort -h -r

Meaning of du options:

--max-depth : print the total size of direcotries on the firest level.

-h  : print the size in human readable format (e.g.  1K, 2M, 3G, etc. ).

Meaning of sort options:

-h : sort human readable format numbers.

-r : reverse the result, the biggest number is on the top.

What I need to do is use this command recursively from the root directory ( / ) . It will finally lead me to the file or directory which consumes most of my storage.

Auto format XML file

1. Format XML file with VIM.

VIM's auto indent feature supports format the XML files. But generally this feature may be disabled by default. 

To enable the auot indent feature, just add the below options in the .vimrc in user's home directory.

filetype indent on

Next time when the xml file be opened by VIM, the xml file type's indent script will be auotmatically loaded as well. In my environment, the indent script for xml is /usr/share/vim/vim72/ftplugin/xml.vim. The xml.vim's path may vary in different system.

To format the XML contents, just use the vim command in the command mode:

gg=G

This command will reformat the entire contents.

2. Fomart XML file with xmllint.

The xmllint are generally used for validating the xml file syntax. But it also has the ability to format and align the XML documents. The XMLLINT_INDENT environmnet variable controls the indentation. The default value is two spaces ( "  " ).

Basically, open the xml file with VIM then execute the below vim command in command mode:

 

:%!xmllint --format %

This command means the xmllint be excuted with the entire xml document contents as input, then the result of xmllint will be used to update the content of current buffer.

The xmllint also do some refine for the xml document, such as, adding the xml version declaration in the document's head.

XPath error: Cannot select a node here: the context item is an atomic value

Assume the input xml file is like

<people>
    <person>
        <name>paul</name>
        <gender>male</gender>
    </person>
    <person>
        <name>lee</name>
        <gender>female</gender>
    </person>
</people>

This XPath error complained for the below style sheet. 

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="team">
                <xsl:for-each select="distinct-values(//gender)">
                    <xsl:variable name="gender" select="."/>
                    <xsl:element name="{$gender}">
                        <xsl:for-each select="//person[gender=$gender]"> <!-- Wrong! A node is selected for actomic context item. -->
                            <xsl:variable name="name" select="name"/>
                            <xsl:element name="{$name}"/> 
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This error means that a node was selected for a actomic context item. The atomic context item is chosen by the outer for-each loop <xsl:for-each select="distinct-values(//gender)"/>.

Node items can not be selected in this loop context.

The solution is define a variable outside of loop. the variable value is the string of the node path. Then select the node with that variable.

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="team">
            <xsl:variable name="person" select="//person"/> <!-- Define the variable with the node path. -->
                <xsl:for-each select="distinct-values(//gender)">
                    <xsl:variable name="gender" select="."/>
                    <xsl:element name="{$gender}">
                        <xsl:for-each select="$person[gender=$gender]"> <!-- Select the node by the variable defined outside of this for-each loop. -->
                            <xsl:variable name="name" select="name"/>
                            <xsl:element name="{$name}"/> 
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Weird enough, but seems like the only available solution. 

Good Linux tool, netcat

Netcat, as know as nc, is a simple command line tool to send and receive data with TCP or UDP protocal.

There are many tutorials for the netact on the internet, since it has been wildly used as a back-end tool to :

  • Simulate server
  • Scanning ports
  • File transfers
  • Proxy gateway
  • Many other things to transfer data with TCP/UDP from one place to another.

Hence here I'd like to show you something different, an example of how to create a echo server with netcat.

The server side just as simple as one line command:

$ nc -l -p 12345 -c 'while true; do read i && echo [echo] $i; done'

Then the telnet can be used as a client to connect to the nc server

$ telnet localhost 12345
hello
[echo] hello

Once you input any string, the server returns your input with [echo] prefix.

Good Linux tool, Module Assistant

Module Assistant is a powerful tool to build Debian kernel modules without re-compile the whole linux kernel. Module Assitant extramly facilitate the process of building kenerl modules by automatically download the module source code from Debian package repository and compile the source code to generate kernel module binary file. 

Moduel Assistant requires to run as root. With Module Assistant, the process of building kernel module is simplyfied to 5 steps.

- Install Module Assistant, 

$ sudo apt-get install module-assistant

- Prepare the artifact for building kernel modules,

$ sudo m-a prepare

The command will automatically download and install the kernel headers corresponding to current kernel and needed building tools like gcc, make and so on.

- Update the module assitant suppored module list,

$ sudo m-a update

-  Check supported module list,

$ sudo m-a list

- Build the needed module, such as, the module for Logitech Quickcam,

$ sudo m-a a-i qc-usb-source

Option a-i stands for automatic installation.