Sunday, November 25, 2007
Rename multiple files
In Linux,
rename gif jpg *.gif
This command replaces the occurance of gif (first parameter) in the file names with jpg ( second parameter) on all files (starting from third parameter)
In Windows,
ren *.gif *.jpg
It really works. I never thought windows has such useful stuff :D. But it works only on the extension. If u want to change the file name, instead of extension, it definitely fails.
Linux still rocks :)
Wednesday, November 08, 2006
Export Variables from Shell scripts
"When we run a script using ./script it creates a separate shell and then run it. So, since the variable is exported in that shell, it is not available to the parent shell after completion.
When we run a script using . script it runs the script in the same shell which makes the variables available even after the completion of the script."
In Tamil, we have a saying "Katradhu kai mann alavu. Kallaadhadhu ulahalavu". Meaning, Whatever you know is the amount of sand you can have in your hand. Whatever you dont know is as huge as the world. :)
Saturday, October 14, 2006
More on VIM
Browsers, editors in linux and now???? vim.
Everybody is after tabs... :) For people like me, who don't like to see the desktop cluttered with windows, like in some operating systems, it will be gud to use.
And will those OS introduce tab based explorers for browsing the local file systems in future????
Wednesday, October 11, 2006
Power/Weakness (???) of Reflection in Java
I got this question and answer from somebody else and thought of sharing:
public class test{
private String id;
public String getId() {
return id;
}
}
In this class, how to set value to id?
The answer is "Reflection"
Use the following code:
test obj = new test();
Field myfields[] = test.class.getDeclaredFields();
for (int i = 0; i < myfields.length; ++i) {
if ("id".equals(myfields[i].getName())) {
Field f = myfields[i];
//This is important , otherwise u get a access denied exception
f.setAccessible(true);
f.set(obj,"I modified it");
}
}
Seems like this is a security issue in Java. I read that if we used SecurityManager, this can be avoided. ( Have to learn about SecurityManager :) )
Thursday, July 13, 2006
Finding Symbolic Links in Java
File system listing shows (b.txt -> a.txt).
File f = new File("b.txt");
System.out.println("Canonical : "+f.getCanonicalPath());
System.out.println("Absolute : "+f.getAbsolutePath());
Output :
Canonical : a.txt
Absolute : b.txt
Here, if both the path are not same, then the file pointed by Absolute path is a link or may be present inside a folder which is a link to another.
This didn't told me whether a file is sym link or not. coz, if the file is present inside a directory which is a link to another, then I this code will say that the file is a sym link.
ie. if dirB is a link to dirA and the file is present in dirA, then this code will tell me that the file is a sym link, which is not true.
But I will know that, there is a path without any sym links to access this file.
Tuesday, June 27, 2006
Joining lines in sed
sed -e :a -e '$!N;s/\n\([a-zA-Z]\)/\1/;ta' -e 'P;D'
Done...
Friday, May 05, 2006
CAT or REDIRECT
I want to read a property file and assign values to variables.
My code was like cat prop_file | while read line ; do value=$line; done
print $value
Guess the output I got !!!!
Output: (Nothing) :(
I and my collegue fought for a looooooooooooooooooooooooong time. Then he asked me to use as follows. Modify the code as:
while read line ; do value=$line; done
print $value
and asked it to run as ./myfile.sh prop_file
Hurraaaaaaaaaaaaaaaaaaaaayyyyyyyyyyy!!!!!!!!!!!!!!
I got the output.
Reason: After some trials he told me that the reason might be: The cat might spawn a new shell and execute the entire while loop inside the shell. So the value might not persist after the end of while.
And this entry is dedicated to Vaidhy (my collegue) ... :) Thanks Vaidhy...
Thursday, March 23, 2006
Recording in VIM
Today I had to do something like moving a text following a pattern to a next line.
Eg: sometext; othertextI didn't wanted to go through all the lines individually and move it
to next line. At that time, I heard something called as "recording"
from one of my friends. As per that I did the following and I felt in
very deeeeeeeeeeeeeeeeeep love with VIM.
- Entered recording mode by pressing "q" followed by a letter say "a"
- Then searched for ";" using "/" and then went to insert mode
- Inserted a new line and then moved down to next line.
- Pressed "q" again to quit recording mode.
- Then pressed "@a" and it replaced the first occurance of the pattern to next line.
- To repeat 10 times, I used "10@a"
The number (10) indicates the number of times to replay.
Really a nice feature of VIM...
Monday, January 02, 2006
Creating Shared Libraries using GCC
- Created a function in a file (myshared.c) without a main() function.
- Compiled it with -enable-shared option and renamed to libmyshared.so. Thatz all. I have created a shared library.
- For using it, I updated the LD_LIBRARY_PATH to the location where the .so is present. The other option is to run ldconfig.
- To use it, I used gcc -lmyshared myfile.c.
- -l option searches for libmyshared.so in the library path.
- myfile.c is the one that uses the function that I have created in the myshared.c
- Done.....
Thursday, November 24, 2005
Cache-Busting with Javascript
Got an opportunity to work with Javascript. On reading, got this information.
What it is?
As we all know, when we are browsing the pages fetched by the browser will be stored in the browser's cache. If we want to get the page from the server and not from the cache, using Javascript, it is very simple. Just add a random key-value pair to the URL. This should be unique for each request.
How to do?
Set your request URL as :
URL +"?rand=" + (Math.random() * 99999999);
How it works?
I assume that, the browser MAY maintain a key-value pair (Something like a HashTable), that contains the "URL of the requested page" as the key and the "contents of the page" as its value. So when a request is made, it first checks its cache and if it finds a page it returns it, else fetches from the server. In our case, since random() generates different values for each call, the browser will assume that this is a new request for a page and fetches it from the server. This in no way affect the server. Since the server may not know about the additional parameter, it will discard the variable.
Wednesday, November 09, 2005
Most of us might have encountered a code like the one mentioned above. What does this mean? Goooogling gave me its meaning.
Actually, a macro by name _(str) will be defined as follows:
#define _(str) gettext(str)
What gettext() does?
The gettext function translates the "str" from the natural language message into the user's language by looking up the translation in a message catalog.
From this, what I understand is, suppose I am using Latin language in my machine. And the string "str" is in English. So whenever printing, the gettext() translates English to Latin gives it to printf(). So I'll be seeing the messages in Latin.
Hope Microsoft also uses a similar kind of macro "_T" for displaying strings in multi-byte format.
Wednesday, October 05, 2005
Today I SHIFT + Deleted all my mails from Outlook 2000 by mistake. I somewhere read that there is a way to recover the deleted mails. I Gooooooogled for sometime and I got a page that told me how to retrieve SHIFT + Deleted mails. As per that, I did the following steps.
- I downloaded XVI32 and Hexadecimal editor.
- Opened the "Personal Folders.pst" file with that.
- Replaced some characters with space and saved the file. (Corrupted the existing pst file. Hopefully the headers)
- Ran "scanpst.exe" on that file. (Comes with Windows. Recovers a corrupted pst file.)
- Started outlook.