Thursday, July 28, 2011

*nix/cygwin find commands

I need to keep a record of some Linux/cygwin "find" commands that I find useful. This is sort of a cookbook or quick reference of find commands that are handy to have around.

Print files which were modified within the last 10 days but and NOT in a "CVS", "build" or "classes" folder:
find . -type d -name CVS -prune -o -name build -prune -o -name classes -prune -o -mtime -10 \! -type d -print 

Print a sorted list of the largest files above 20000k minimum size
find . -type f -size +20000k -printf '%kk %p\n' | sort -n -r 

Print files with XML extensions containing the phrase "search string"
find . -type f -name "*.xml" -exec grep "search string" '{}' /dev/null \; -print       

Copy files newer than file X

Where file-x.txt is the file, and /tmp/copy-destination/ is the place you want to copy the files to:

find . -newer file-x.txt $1 | xargs -I {} cp -p $1{} /tmp/copy-destination/

Use OutputStreamWriter not FileWriter

Search your Java code for FileWriter and replace it with OutputStreamWriter... especially if you write UTF-8 encoded documents. You can run into a nasty problem where you develop your Java code on Linux and then when it runs on Windows, character encoding problems crop up. FileWriter uses a OS System setting to determine what encoding to use, so it will change depending on what OS runs the Java code!

Here is an example of how to use OutputStreamWriter for UTF-8 encoded output.
         OutputStreamWriter out = null;
try {
try {
out = new OutputStreamWriter(new FileOutputStream(outputFile),"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage,e);
}
try {
out.append(yourStringDataToWriteToOutputFile);
out.flush();
} catch (IOException e) {
throw new RuntimeException(e.getMessage,e);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage,e);
} finally {
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage,e);
}
}

Hope this helps you get past file character encoding bugs with Java. Here is a great link on various Java Anti-Patterns which should be avoided when writing enterprise class robust code:

About Me

My photo
Lead Java Developer Husband and Father

Tags