Wednesday 5 March 2014

Unix basic commands

Below are the few basic Unix commands which will be more useful for Java developers who are working in Unix environment.

List all users:
cat /etc/passwd |grep "/home" |cut -d: -f1
awk -F":" '{ print "username: " $1 "\t\tuid:" $3 }' /etc/passwd

The above commands will list all the users in the system.

Some Basic commands:
cd path - change directory.
mv path1/filename1 path2/filename2 - Move files from one directory to another directory.
ls [options]- list the file names.
ll [options]- list the file names with permissions and size.
cp path/filename1 path2/filename2 - Copy files from one directory to another directory.
cat filename - To print the content of the file.
diff filenmae1 filename2 - To view the differences between two files.
./filename.sh - To run sh files. This is mostly used for executing shell scripts and server stop start.
view filename - To view the content of the file.
vi filename - To view and update the file.
top - To check the CPU performance and utilization.
df - To check the disk space utilized.

vi & view editor commands:
----------------------------
When a opened a file with vi/view command, the file will open in command mode as default. To make changes in the files needs bring to "Insert Mode".
i/<Ins> - insert text before cursor, until <Esc> hit. Also called "Insert Mode".
I - insert text at beginning of current line, until <Esc> hit
<Esc> - Change to "Command Mode".

:w - saves the current file without quitting
:# - move to line #
:$ - move to last line of file
?string - Search backward for string
/string - Search forward for string
n - move to next occurrence of search string
N - move to next occurrence of search string in opposite direction
<Esc>:w! - Saves the file
<Esc>:q! - Quit editing, no save

Create a .jar file:
$ jar -cvf <file_namer.jar> <class_file_path>

To work with above command, first we need to change the directory to JDK installed bin directory and execute the command.

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -cvf ibank.jar com/calc/*.class

The second command is useful when there are multiple class to be include in a jar. the path /opt/WebSphere7/AppServer/java/bin is a JDK installed path from whepsher server.


Update a .jar file:
jar -uvf <file_namer.jar> <class_file_path>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/Calculator.class
$ /opt/WebSphere7/AppServer/java/bin/jar -uvf calculator.jar com/calc/*.class

Unzar a .jar file:
$ jar -xvf <file_namer.jar>

Ex:
$ /opt/WebSphere7/AppServer/java/bin/jar -xvf calculator.jar
Once the above command executed, we will get the class files in unix files system. So first we need to change the directory where we need the files to be located and provide bin full path while executing the command.
If we not change the directory and executed from JDK installed path, all extracted files will be placed in JDK installed path.

Find a Class file in .jar file:
grep classname *.jar

Ex:
grep com/calc/Calculator.class *.jar
grep Ibank.class *.jar

To Know the package exactly:
$ /opt/WebSphere7/AppServer/java/bin/jar -tvf calculator.jar|grep Calculator*

Check HTTP Server status:
$ ps -efw|grep httpd

File system size:
When we are facing with file system space issue and not able to find which directory occupies more memory, use the below command.

du -sm *| sort -nr | head -15
du -sh * | sort -nr | more

To transfer tar files from one IP to another IP:
Login to the IP address, where the file is available and execute the below command.
$ /usr/local/openssh/bin/scp <path>/<file_name>  <userid>@<ip_address>:/tmp/


No comments:

Post a Comment