Showing posts with label Technical. Show all posts
Showing posts with label Technical. Show all posts

Thursday, April 16, 2009

One line find and replace string command in Linux/Unix.

Hi,

One of my friend was in urgent need for a find and replace command in set of files of some specific extension and asked me if i have one.

I gave him the following: Hope this is useful for you.

find . -name "*.extension" -exec sed -i s/strtofind/strtoreplace/g '{}' \;

where,
extension- extension of the file names where you want to find and replace.
strtofind- string to be found in those files.
strtoreplace- string to be replaced in those files.

Note: Please take caution when you want to find and replace strings having "/". You may need to use \\/ to make sure that "/" is treated as string by the sed command.

Thursday, March 26, 2009

Recursive Listing of Files - Java code.

Hi,

Here is a small program that i have written to list directories recursively and also the count of files in each directory and output the same to console or to a file as per user preferences.

Usage: java ListFiles directory_to_scan [outputfile]

You can use this code if you really like and i dont impose any license restriction or something :)

But i will appreciate if you just send me a mail that you are benefitted by this simple piece of code.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ListFiles {
private static List filesList = new ArrayList();
public static void main(String args[]) {
if(args.length ==0) {
System.out.println("Usage: ListFiles directory [outputfile]");
System.exit(-1);
}
String directoryName = args[0];
String outputFile = null;
if(args.length == 2) {
outputFile = args[1];
}
if(directoryName != null && !directoryName.isEmpty()) {
File directory = new File(directoryName);
if(directory.isDirectory()) {
File[] files = directory.listFiles();
int fileCount = directory.listFiles(new DirectoryNameFilter()).length;
filesList.add(directory.getAbsolutePath() + " = " + fileCount);
getFilesCount(directory);
}
if(outputFile != null) {
File output = new File(outputFile);
BufferedWriter bufWriter = null;
try {
bufWriter = new BufferedWriter(new FileWriter(output));
for(String fileName: filesList) {
bufWriter.write(fileName + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(bufWriter != null) {
try {
bufWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} else {
for(String fileName: filesList) {
System.out.println(fileName);
}
}
}
}
private static List getFiles(File directory) {
List fileList = new ArrayList();
File files[] = null;
if(directory.isDirectory()) {
files = directory.listFiles();
for(File file:files) {
if(file.isDirectory()) {
fileList=getFiles(file);
} else {
filesList.add(file.getAbsolutePath());
}
}
}
return fileList;
}
private static void getFilesCount(File directory) {
File files[] = null;
int fileCount = 0;
if(directory.isDirectory()) {
files = directory.listFiles();
for(File file:files) {
if(file.isDirectory()) {
fileCount = file.listFiles(new DirectoryNameFilter()).length;
filesList.add(file.getAbsolutePath() + " = " + fileCount);
getFilesCount(file);
}
}
}

}

class DirectoryNameFilter implements FilenameFilter {

@Override
public boolean accept(File dir, String name) {
if(!new File(dir,name).isDirectory()) {
return true;
} else {
return false;
}
}
}

Sunday, April 13, 2008

Simple UML Tool

Hi All,

Recently i was assigned a task to create a UML diagram. Downloading tools like rational rose, smartdraw, visio etc will usually take long time and mostly will be a trial version blocking most of the important features. So I was looking for a simple, elegant UML tool covering all the basic UML diagrams and i found this UML tool called UMLet. This is a plugin for eclipse IDE and can be downloaded from http://www.umlet.com/

Thought this would be a useful information to some of you and hence posted. Download this and explore it - it is really nice.