I created some illustrative and simple implementations of common Unix commands. For those who are familiar with Unix-like systems them make easier to understand Java. For those who are familiar with Java them make easier to understand Unix-like systems. :-)
1. PWD
The first one is pwd that show the current working directory.
public class Jpwd {
public static void main(String[] args) {
String pwd = System.getProperty("user.dir");
System.out.println(pwd);
}
}
Running this at /home/silveira directory gives us as output:
$ java Jpwd
/home/silveira
1. CAT
The command cat is usually utilized for displaying files.
$ java Jcat /etc/timezone
America/Fortaleza
3. LS
The command ls is to list files. The File API (java.io.File) is very flexible and portable, but in this example I want just list files and directories of the current directory.
import java.io.File;
public class Jls {
public static void main(String[] args) {
File dir = new File(System.getProperty("user.dir"));
String childs[] = dir.list();
for(String child: childs){
System.out.println(child);
}
}
}
Usage:
$ java Jpwd
/home/silveira/example
$ java Jls
directoryA
fileA
.somefile
4. CD
The cd command changes the current working directory.
Usage:
$ java Jpwd
/home/silveira
$ java Jcd /tmp
$ java Jpwd
/tmp