Archive

Archive for the ‘Linux’ Category

Search and replace in multiple files

February 25th, 2009 deevis No comments

If you ever need to search and replace strings throughout a bunch of files here’s a neat, somewhat alternative, way to do it.

for file in `ls **/*.txt`;do perl -pi -w -e ’s/SEARCH_FOR_THIS/REPLACE_WITH_THIS/g;’ $file; done

The nested ‘perl’ command can work kind of like ’sed’.  Here are what the arguments being passed in mean:

-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

Categories: Linux Tags: ,

Java 101: building your java classpath from a lib folder

February 23rd, 2009 deevis No comments

OK – I admit it – my IDE and tools make me weak in many ways.  I was just having the hardest time getting my maven-managed project to run from a basic “java my.class.ClassName” command line invocation.  Why?  Because the freaking classpath is a million miles away from being a concern to me anymore.  So, jog the memory and immediately remember the -cp ( or -classpath ) option.  Also remember that I cannot specify a directory if I want to include all of the jars in that directory ( specifying a directory in the classpath will only load exploded .class files! ).

What I’ve got before me is a target/lib directory full of all of my maven2-managed dependencies.  I need to add each jar file explicitly into the classpath – barf!

Well, here’s a handy little shell script which will build that string for you:

for file in `ls lib`; do echo -n ‘lib/’ >> classpath.txt;echo -n $file >> classpath.txt;echo -n ‘:’ >> classpath.txt;done

Awesome – so just open the classpath.txt file and use that big ‘ol path however you want to.  Windows users would use a semi-colon instead of a colon above.  Also, remember that you need to specify the classpath option prior to the name of the class to run – otherwise the classpath is ignored ( as it is assumed to be an argument to the program and not a jvm option ).

Categories: Java, Linux Tags: