Tuesday, December 20, 2011

Rename files at linux

I recall "mv" while my colleague ask me how to rename lots of files.
Here is the question:
There are many files. How do we modify its extended name? For example, a.log, b.log, c.log...etc.
We need to change extended from "log" to "txt".

The below answer is incorrect:
mv *.log *.txt

"rename" is the correct command for this time. Here is man page of rename
NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified
       as the first argument.  The perlexpr argument is a Perl expression
       which is expected to modify the $_ string in Perl for at least some of
       the filenames specified.  If a given filename is not modified by the
       expression, it will not be renamed.  If no filenames are given on the
       command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the
       extension, you might say

               rename 's/\.bak$//' *.bak

       To translate uppercase names to lower, you'd use

               rename 'y/A-Z/a-z/' *


For my case, I can use following command:
 rename 's/\.log$/\.txt/' *.log


Wednesday, May 11, 2011

printk within uptime

All you have to do:
Turn on the CONFIG_PRINTK_TIME kernel option

Wala~You get it!

Sunday, January 09, 2011

select function does not stop

I use named FIFO and select function to do IPC programming. When program execute to "if(select(myfd+d, myfdSet, NULL, NULL, &waittime) > 0 && IS_SET(myfdSet))", it always fulfill condition even there is no any process wrote into named FIFO.
After I examin again, I find out that I open file as "O_RDONLY". Refer to Linux's man page, there is a section to discript it:


Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks.

Hence, I modify open statement from myfd = open("/tmp/a", O_RDONLY) to myfd = open("/tmp/a", O_RDWR|O_NONBLOCK). Everything is perfect right.