Monday, December 31, 2007

Happy New Year

Hi World,

Happy 2008 :)

Saturday, December 29, 2007

Find images of certain size and copy them to some directory

Let us say you wanna find images of width 640 so you copy them to a separate directory for further processing. How can you do that?
Here is one way to do it
First, you have to find out the files with width 640. Second, copy them.

To find out file of certain size you can use the command identify

$ identify 0100.jpg
0100.jpg JPEG 378x251 PseudoClass 256c 9kb

Now, when u do
$ identify*.jpg | grep 640
new_0100-0.jpg[451] JPEG 640x425 PseudoClass 256c 16kb
new_0101-0.jpg[453] JPEG 640x425 PseudoClass 256c 22kb
new_0102-0.jpg[455] JPEG 640x425 PseudoClass 256c 24kb
new_0103-0.jpg[457] JPEG 640x425 PseudoClass 256c 26kb
new_0104-0.jpg[459] JPEG 640x425 PseudoClass 256c 12kb
new_0105-0.jpg[461] JPEG 640x425 PseudoClass 256c 23kb
new_0106-0.jpg[463] JPEG 640x425 PseudoClass 256c 20kb
new_0107-0.jpg[465] JPEG 640x425 PseudoClass 256c 24kb
new_0108-0.jpg[467] JPEG 640x425 PseudoClass 256c 17kb
new_0109-0.jpg[469] JPEG 640x425 PseudoClass 256c 28kb
new_0110-0.jpg[471] JPEG 640x425 PseudoClass 256c 31kb
new_0111-0.jpg[473] JPEG 640x425 PseudoClass 256c 36kb
new_0112-0.jpg[475] JPEG 640x425 PseudoClass 256c 27kb
new_0113-0.jpg[477] JPEG 640x425 PseudoClass 256c 25kb

so we do this and save the output in the file files
identify *.jpg | grep 640 | cut -f 1 -d [ > files

Finally
for i in `cat files`; do cp $i directory_640/; done

Thursday, December 27, 2007

Renaming or changing extension of multiple files at once

In a lot of time you want to rename multiple files at once, or maybe change their extension. My Sony camera names files with the extension .JPG which is really annoying!!! I'd love to have it as .jpg or maybe something else. Further, the camera naming scheme is DSCxxxx.JPG where xxxx is for the number of the image, and I'd like to have some custom name instead. So, how to do this?

First, let us start off with an introduction about file naming and shell scripts. Mass file renaming is a pretty common task among everybody and a lot of people do it in many situations, such as the Sony camera situation, or maybe you have so many files in one directory what you want to rename to something else. Examples of these cases are endless, so let us start with file renaming and some of the available tools to do that without wasting time discussing situations.

The BASH shell provides many useful tools for renaming multiple files. Please note that I'm not claiming that BASH shell is the best. I think that the Z shell might provide more tools, but it is so complex for me :)
One tool you can use in bash to rename multiple files is combining the UNIX command mv with a for loop like this:
for file in .; do mv $file hello; done
well... this is not useful.. in face this is disastrous this will rename all files in the current directory to hello, not only this, but also you'll have one file in that directory called hello. Poor you, nothing left to celebrate, no more old memories and nice moments taken by camera, because you've formatted your memory stick :) So let's not do this at all.

Alright, let's say you just want to rename all files in the current directory to holiday_xxxx.jpg, where xxxx is the serial number for a photo, starting from 0000 up to 9999 (of FFFF if your prefer hex)
Let us do that

#!/bin/bash
prefix="holiday"
i=0
for file in *.jpg; do
num=`printf "%.4d" "$i"`
let "i += 1"
mv $file "$prefix_$num.jpg"
done

And that is it we are done. We can also take the prefix as a command line argument and thus use this script in any place where we want to rename jpg photos:
#!/bin/bash
if [ -z "$1" ]; then
echo "please supply a prefix(name) argument"
exit
fi

prefix="$1"
i=0
for file in *.jpg; do
num=`printf "%.4d" "$i"`
let "i += 1"
mv $file "$prefix_$num.jpg"
done

Cool.. now let's explore another tool.. basename. You can use basename like in situations like this:
nitro$ basename /usr/bin/perl
nitro$ perl

Another useful use is when you supply a suffix to basename like this
nitro$ basename hello.jpg .jpg
nitro$ hello
nitro$ basename hello.jpg jpg
nitro$ hello.
nitro$ basename hello.jpg g
nitro$ hello.jp

So, to lowercase a lot of .JPG file you can do this:
nitro$ for file in *.JPG; do mv $file "`basename $file .JPG`.jpg"; done

in my opinion, the easiest is this one, which is using the built in bash variable substitution
for file in *.JPG; do mv $file ${file/.JPG}.jpg; done

Some good source to look @ http://www.debian-administration.org/articles/150

Wednesday, December 26, 2007

Simple Command Line Arguments in BASH

The number of arguments in the command line in BASH scripts is stored in $#, while the list of arguments is stored in $@. You can access arguments by there index. For example, $1 is the first argument in the list and $2 is the second ... etc. Notice that $0 is the name of the file (script file name)

Code example (documentation is left as an exercise for the reader)

--------------------------------------------------------------------
#!/bin/bash
# This code will mv not executable (-x flag is off)
# from src_dir(def=".") to target_dir.

src_dir=''
target_dir=''

if [ -z "$1" ]; then
echo "Usage: `basename $0` [src_dir] target_dir
exit
fi

if [ "$#" == 2 ]; then
if [ ! -d "$1" -o ! -d "$2" ]; then
echo "Error: You must supply directory names on the cmd line"
exit
fi
src_dir=$1
target_dir=$2
else
src_dir="."
target_dir=$1
fi

for file in $src_dir/*
do
if [ ! -x $file ]; then
mv $file $target_dir
fi
done
--------------------------------------------------------------------

Tuesday, December 25, 2007

Revoming Empty Directories

http://www.cyberciti.biz/faq/linux-how-do-i-remove-all-empty-directories/

cleanlinks

Listing directories only

Some times we need to see only directories... and, as usual, there are a lot of ways to do this..

you can write a quick bash script like this:

for file in *; do if [ -d $file ]; then echo $file; fi; done

This will display, in columns, all subdirectories under the directory you are in.

In GNU it is really simple, you just do this
ls -d */

Now to display each entry on a line you can do this:
ls -d */ | less

or to avoid the 'q' to exit less, then u do this:
ls -d */ | cut -f 1 -d /

To get the effect of ls -l for directories only you can do this:
ls -l | egrep ^d

And you can have more fun, by piping to sort or something else
like
ls -d */ | cut -f 1 -d / | sort -r
which sorts things in reverse order

World Have fun :)

Monday, December 24, 2007

Bash loops

Section 1.0 If statements
Although costs much in terms of cycles.. we can't live without them...
Here is how you quickly

var="NiTrO"
if [ $var = "NiTro" ]; then
echo $var
else
echo "HelloWorld\n"
fi


Section 1.1 Loops

Let's do some BASH loops.. notice this is BASH not sh :)

a quicky for loop

for img in $(ls | grep *.jpg); do echo $img; done

for i in `seq 0 10`; do echo $i; done

i=0; # no spaces
while [ $i -lt 100 ]; do # a space after [
echo $i
let i=i+1
done

# until -- this will do the same as the previous loop
i=0;
until [ $i -gt 10 ]; do
echo $i
let i=i+1
done

Section 1.2 Logical Operators
we love logical operators, which are those weired things used in tests and conditional statements...

For strings... it is very easy
=, !=, <, >, -z (is null), -n (is not null)
Arithmetic
-lt, -le, -gt, -ge, -eq -ne

Ok i'm bored!! enough for today

Sunday, December 23, 2007

Dilbert... :)

What is the effect of introvert CS geeks on the depletion of the ozone layer in North Pole?


There is a basic flaw in the essence of the question raised because CS geeks are always busy on their computers that are placed indoors saving the world from the hazards of tomorrow (like cyber- crimes, incompetent users messing with competent software and so on). This leads us to the fact that CS geeks can have no direct affect on the climatic elements because they always choose to remain indoors by their choice and refrain from lunatic, mischievous activities outdoors. The primary reason for the depletion of Oz layer is the over-usage of CFCs mainly in the form of aerosols heavily present in cans of shaving creams, perfumes, sodas, etc. But guess what? Real CS geeks do not shave, use perfume, or drink soda (they live on coffee). So, this reiterates my point that cs geeks are innocent and have absolutely no contribution towards the depletion of the Oz layer whatsoever.

Perl loops

Here are some quicky perl loops

for(my $i=0; $i<$limit; $i++) {
print $i;
}

while($i<$limit) {
print "$limit\n";
}

for $i (@array) {
print "$i\n";
}

until($i > 100) {
print $i;
}

NetworkManager things

when stuck you can
pkill NetworkManagerDispatcher
sudp pkill NetworkManager
sudo NetworkManager
sudo NetworkManagerDispatcher

and this is good too
iwlist scan

ok.. this was a silly post :) sorry world :P

Turning off IPv6 in ubuntu for faster life

edit /etc/modprobe.d/aliases

comment this
alias net-pf-10 ivp6
#alias net-pf-10 ivp6

then we put these lines somewhere in the file

alias net-pf-10 ipv6 off
alias net-pf-10 off
alias ivp6 off

Self Esteem

Hi My dear blog,
I hope that I didn't disturb you... I don't know i just miss you and want to talk to you..
Thanks my dear blog :) I feel better

--------
LOOOOOOOOOOOOOOOOOOOOL this is silly :O)


World stay tuned

Saturday, December 22, 2007

why ByeWorld

Well... HelloWorld was taken.. so ByeWorld is the same when HelloWorld is flipped with respect to the X axis :)