New address

Dear readers!
The new address for this blog is at it has several upgrades like a better theme and Syntax highlighting, thanks for visiting!



Leave a comment

Sorting Java Collections

Java collections are most of the data structures that come with the language, they share the same basics methods thanks to the collection interface, one of the most common things you may want to do with a collection, besides adding and removing elements, is sorting them.

Let’s see an example with ArrayList

public class Sorting {

	private final static ArrayList numbers = new ArrayList();

	public static void main(String[] args) {



Sorting an ArrayList of integers is as easy as using “Collections.sort” since Java already knows how to sort numbers, but what if we wanted to sort an array of objects? How does Java know which object is worth more than other? Well we need to tell Java how to compare our objects implementing the Comparator interface

Here is an example:

private ArrayList
articles = new ArrayList(); articles.add(new Article("gel27", 200)); private void sortArticles() { Collections.sort(articles, new Comparator
() { @Override public int compare(Article art1, Article art2) { char a1 = art1.getArticleCode().charAt(0); char a2 = art2.getArticleCode().charAt(0); if (a1 > a2) { return 1; } else if (a1 == a2) { return 0; } else { return -1; } } }); }

We implement Comparator using an anonymous class and override the compare method, this method arguments are the two objects to be compared and returns an int, 1 if the first object is greater than the second, 0 if they are equal and -1 if the second object is greater than the first one, you can pick any field from your object to perform the comparison, in this case I’m using the first character of the article code so the result will be an alphabetical ordered array, you could also use the compareTo method if you want to use the whole string instead of just the first character.

, , , , , ,

Leave a comment

Column formatting

Here is a quick tip on how to make the output of some tools prettier, for example we can use the mount command which by default looks like this

/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
none on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
none on /dev type devtmpfs (rw,mode=0755)

Using the column command with the -t option we can apply some formatting so it is more readable:

$ mount | column -t

/dev/sda1  on  /                         type  ext4        (rw,errors=remount-ro)
proc       on  /proc                     type  proc        (rw,noexec,nosuid,nodev)
none       on  /sys                      type  sysfs       (rw,noexec,nosuid,nodev)
none       on  /sys/fs/fuse/connections  type  fusectl     (rw)
none       on  /sys/kernel/debug         type  debugfs     (rw)
none       on  /sys/kernel/security      type  securityfs  (rw)
none       on  /dev                      type  devtmpfs    (rw,mode=0755)

, ,

Leave a comment

Ruby: fine grained sorting

If we had this array: ["abc", "aaa", "add", "bcc", "baa"] sorting normally we would get:

["aaa", "abc", "add", "baa", "bcc"]

Let’s say we wanted to sort by the second letter, we could do this using the sort_by method:

a.sort_by { |a| a[1] }
["baa", "aaa", "abc", "bcc", "add"]

Now lets see a more complex example, if we wanted to sort an email list:

First by the host name and then by the user name we can use the sort_by method like this:

sorted_emails = emails.sort_by { |e| [ e[/@.*/], e[/.*@/] ] }

We pass a block to sort_by with the ‘rules’ we want to sort by, we are using a regular expression to express how we want to sort, they are /@.*/ which matches everything after the at sign and /.*@/ which matches everything before.

Finally we can apply the same idea to uniq so that we can get unique data based on a pattern, I use this in Dirfuzz to filter the results and avoid duplicates when I have duplicate results that aren’t exactly the same.

@rel_links.sort.uniq { |link| link[/(?:\/\w+)+/] }

This regexp will allow me to get rid of duplicates with this data:


This would stay the same with a simple uniq, but passing a block with that regexp will get rid of the duplicates.

, , , , , ,

Leave a comment

Introducing Dirfuzz

Tired of certain other tool to crash and take ages to finish? meet Dirfuzz

Dirfuzz is a tool for directory discovery of web applications, by default it uses a dictionary based approach which is in data/fdirs.txt it can also use the crawler module to find links up to 1 level of depth.

Dirfuzz is designed to give you plenty of information fast and without having to scroll through hundreds of pages of output or deal with a clunky GUI.

The project is hosted at github and you can download it and get a bit more info from there:

, , , ,


The tree command

With the tree command in Linux you can get a tree representation of a directory structure, without any arguments it will start of the current dir and recursively go into each subdir to show a complete hierarchy.

# tree
├── 1
│   ├── 44
│   ├── aa
│   ├── bb
│   └── ff
└── 2
├── cc
└── dd

3 directories, 5 files

this is just some dirs and files I made for testing, but if you run this on a real dir you will get a lot of output, to solve this you can use the -L option to limit the depth

# tree -L 1
├── 1
└── 2

well that’s a bit better, you can also get other useful information like permissions using the -p option

# tree -p
├── [drw-r-----] 1
│   ├── [drwxr-xr-x] 44
│   ├── [-rw-r--r--] aa
│   ├── [-rw-r--r--] bb
│   └── [-rw-r--r--] ff
└── [drwxr-xr-x] 2
├── [-rw-r--r--] cc
└── [-rw-r--r--] dd

another useful one is -u to show the owners of the files,

# tree -u
├── [root ] 1
│   ├── [root ] 44
│   ├── [matu ] aa
│   ├── [matu ] bb
│   └── [matu ] ff
└── [root ] 2
├── [root ] cc
└── [root ] dd

Others that can also come in handy are -d to show only dirs and -s to show the size of files, but I will leave these to try on your own.

, ,

Leave a comment

%d bloggers like this: