Friday, November 28, 2008

Misc. Awk Tips

Few quirks about awk that I found:

1. The $0 in expression part of awk invocation such as in

pinczakko@opunaga $ find . -type f | awk '$0 !~ /\<svn\>/ {print}'

doesn't represent any of the field in a record (a line) in awk terminology. $0 represents the whole record (the whole line). Therefore, if you want to match an expression to the entire line, $0 is the way to do that.

2. To match the whole word in awk, use the escaped angle brackets. Escaped angle bracket in awk is \<\> because backslash is the escape character in an awk expression.

pinczakko@opunaga $ find . -type f | awk '$0 !~ /\<svn\>/ {print}'

The invocation above asks awk to print any lines which don't have the word svn in it.

Thursday, November 27, 2008

Removing Duplicated File Automatically

It's annoying to have two or more of the same file laying around in your hard drive. Out of curiosity, I devised a small script to automate the process of finding and deleting duplicated file in two different directories. This is it.

#!/bin/bash
#
# This script checks whether there is a file duplication
# in two different directrories and deletes the one in
# ${SRC_DIR} if it found the same file
#

SRC_DIR="/home/darmawan/download"
DST_DIR="/home/sources"
TMP_FILENAMES="__filenames.txt"
CUR_FILE=""
DST_FILE=""
RESULT=""

find ${SRC_DIR} -type f > ${TMP_FILENAMES}

while read LINE
do
unset RESULT
unset CUR_FILE

CUR_FILE=$(basename "${LINE}")
#find ${DST_DIR} -type f -name "${CUR_FILE}" -exec diff -q "${LINE}" '{}' ';'
RESULT=$(find ${DST_DIR} -type f -name "${CUR_FILE}" -print)

if [ "${RESULT}" != "" ] ; then
echo "File of the same name found at ${LINE} and ${RESULT}" ;
echo "Diffing.."
diff -q ${LINE} ${RESULT}

# If the file differ (diff return value _is_not_0_ ),
# then print it out, otherwise delete the file in ${SRC_DIR}
if [ $? -eq 0 ]; then
rm -vf ${LINE}
fi
fi

done < ${TMP_FILENAMES}

unset LINE
unset FILENAME
unset CUR_FILE
unset DST_FILE
unset RESULT

rm -v ${TMP_FILENAMES}

This is a very rough script. Don't expect robustness out of it.

Thursday, November 20, 2008

Koneksi IM2 Broom di Linux

Hari ini gw nyobain Indosat Broom untuk pertama kali. Gw pikir awalnya bakal sedikit susah buat aktivasi, ternyata cuma sedikit lebih kompleks daripada bikin account email gratisan.

Konfigurasi sistem:
1. Linux Slamd64 12.1
2. Wvdial 1.60
3. AMD Turion64
4. RAM 1GB
5. Nokia E61.

Step garis besarnya:

a. Insert USIM dari starter-pack broom ke handphone/modem yang akan digunakan.

b. Dial ke situs registrasi broom menggunakan username dan password universal buat registrasi. Ini bisa dilakukan langsung. Jadi, pada dasarnya begitu dapat USIM (a.k.a SIM CARD) langsung bisa terhubung ke network registrasi Indosat M2.

c. Isi form registrasi dengan benar, kemudian di submit. Ntar bakal muncul jawaban aktivasi telah selesai.

d. Konfigurasi ulang username dan password sesuai dengan form registrasi.

e. Silahkan browsing, ftp, blogging, rss-feed, etc. sesuai kemauan anda ke semua destinasi.

Ok. Sekarang detailnya.

Step a: Ga perlu diilustrasikan coz sangat gampang.

Step b: Berhubung gw pake wvdial + Linux Slamd64, ini dia wvdial.conf yang gw pake

[Dialer im2_reg]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+IFC=2,2;+CVHU=1
Init4 = ATS7=60+DS=3,0;&K3
Init5 = AT+CGDCONT=1,"IP","indosatm2"
Init6 = ATS0=0
Phone = *99#
Username = indosatm2
Password = prabayar
Modem Type = USB Modem
Baud = 115200
New PPPD = yes
Dial Command = ATDT
Modem = /dev/rfcomm0
ISDN = 0

Pake wvdial.conf ini gw dial ke situs registrasi im2

root@opunaga # wvdial im2_reg


Step c: Buat registrasi, pake browser gw buka http://www.indosatm2.com/aktivasi. Ini situs registrasinya, trus isi form registrasi. Di form ini ada bagian untuk ngisi username + password yang akan kita pake browsing dan tipe servis Broom yang mau kita pake. Gw milih yg unlimited. Lumayan 100rb sebulan unlimited.

Step d: Tinggal modifikasi username ama password di wvdial.conf yang ada di step c, selesai deh.

Step e: Pake browser kalo pengen browsing, etc.

Sampai di sini. Tinggal dipake aja koneksinya.

Btw, di daerah Mega Kuningan lumayan kenceng koneksinya.

Thursday, November 6, 2008

Combined find, xargs and chown Trick

Sometimes, you want to change the ownership of files (including symlinks) in directories recursively. However, "malformed" directory and file names can become huge stumbling blocks. This is how to do that correctly:

root@copy_cabana# find . -user old_user -group old_group -print0 | xargs -0 chown -v -h new_user:new_group

A few notes:


  • The -print0 parameter tells find to append the "null" character after each matching filename instead of the usual "newline" character. This is handy to "fight" against malformed filename and directory name that contain white spaces, punctuation, etc.


  • The -0 parameter tells xargs that the delimiter in its input is not "newline" but a "null" character.


  • The -h parameter tells chmod to change the ownership of the symlink instead of the file/directory the symlink refers to.




That's it. Now you're prepared for weird filenames.