Basic unix commands for java developers

Command to know the version of UNIX/LINUX installed

uname –a

 

Command to know Home directory of User

echo $HOME

 

Navigation Command

cd .
Current Directory
cd ..
Parent Directory
cd
Home Directory
cd –
to go back to previous directory
pwd
Present working directory
cd $HOME
will go to home directory
cd
will go to root directory
cd $HOME
will go to home directory

 

 

Listing Command

ls                          : list down the files in current directory

 

File Command

 

Creation of File

vi   <File_Name_To Create>

Note: It will open VI editor to write the content in File.

OR

Touch <File_Name_To_Create>

Note*: It will only create the file with given name if it doesn’t exist.

Editing the File

vi   <File_Name_To Edit>

  • Enter into input mode – i   (Click small I)
  • Save the content and exit –     :wq    (! Can be used to force the exit)
  • Exit the file without saving it – :q       (! Can be used to force the exit)

View the Content of File

Read only View can be done using cat command.

cat <File_Name_To_View>

Note*: It will display the content and return to command prompt.

Remove the File

rm <File_Name_To_Remove>

Change File Permission

chmod  <Numeric Value of File Permission> <File_Name_To_Change_Mode>

Attribute

Readable : 4

Not Readable : 0

Writable : 2

Not Writtable : 0

Executable : 1

Not Executable : 0

 

Every file or directory has 3 type of permission.

  1. Owner Permission (Read,Write,Execute)
  2. Group Permission (Read,Write,Execute)
  3. Public Permission (Read,Write,Execute)

So the final permission of a file will look like this XXXXXXXXX (OwnerGroup ­­- Public).

 

How to calculate Numeric Value of File Permission

Numeric value is file permission would be of length 3.  First number will represent the permission of Owner, second is for Group and third is for public permission.

Now we will calculate permission for different category individually and then we will add it to get final permission.

Owner Permission

As given above look for the permission an owner has and then integer value of it to get the final number.

Readable + Writable + Executable = 4+2+1 = 7

Subtract the corresponding number if permission is not allowed for given action. Like no readable permission is there than final value would be 7-4= 3

 

Group Permission

As given above look for the permission a group has and then integer value of it to get the final number.

Readable + Writable + Executable = 4+2+1 = 7

Subtract the corresponding number if permission is not allowed for given action. Like no readable permission is there than final value would be 7-4= 3

 

Public Permission

As given above look for the permission public has and then integer value of it to get the final number.

Readable + Writable + Executable = 4+2+1 = 7

Subtract the corresponding number if permission is not allowed for given action. Like no readable permission is there than final value would be 7-4= 3

 

In the end we will add all 3 numbers obtained through above process. So if all category only has read property then Permission would be “333”.

If all permission is there then permission would be “777”.

Search File

File searching can be done using find command.

To search a file in current directory only (Not in subdirectory)

 

To search a file in current directory and subdirectory

find . -name MFSB*.pc

 

Search a term in Files

To search a term in files grep command can be used.

Look for word JBT in all file in current directory only.

 

grep -il “JBT” *

 

i–ignore case

l–file name (only)

n–line no’s

c–No of occurrence of a word

find . -name MFSB*.pc

find . -name–command

 

Other Commands

Clear Console                                                                               : clear

Previous command fired in same session              : Esc + k

Current logged in user                                                          : whoami

All running processes                                                           : ps -ef

All logged in user                                                                      : who

Remove all content of a file (Empty File)                :  > file_name

 

Display the list of Command Fired in Current Session

 

history                  // will display the last 10 commands fired

 

history 100        // will display the last 100 command fired

 

Alias Creation

In order to create ALIAS you need to make an entry in “.profile” file in user home directory.

alias — needs to be added in .profile in $HOME path

How to create Alias

Step 1: Look for the home directory of current user (Use Command “echo $HOME”).

Step 2: Switch to home directory (Use Command “cd $HOME”).

Step 3: Open “.profile” file (Use Command “vi .profile”).

Step 4:  Enter the line “  alias jbt=”echo Hello JBT”    ”

Step 5: Save and exit file. (Use Command  Esc+:+wq)

Step 6: Log off and Log in again

Step 7: Use Alias (jbt)

 

Log File Creation

How to create a file containing the output of a command / Shell (Success or Failure).

Add the output of a command to a file

ls -ltr > viveka.temp    // Here new file named viveka.tmp will be created in case no file exist with given name and it will have content as the output of given command. If file with given name is there then content of the file will be overridden.

ls -ltr >> viveka.temp   // Here new file named viveka.tmp will be created in case no file exist with given name and it will have content as the output of given command. If file with given name is there then content of the file will be appended.

Creating file with error message occurred while executing the command.

find . -name file   2>JBT2   1>JBT2

Here two file will be created JBT1 and JBT2. JBT1 will contain the error message (if any) and JBT2 will contain success message (If any).

How to search for text in executables

nm NCSRGDFS|grep -iw wrt_curr_dt

 

It will search for something in executable. (GREP will not work in case of executable)

-w  it will look for the exact word if you are looking for word @ any position then remove it

 

find . -name [Aa][Pp][Pp]* -exec grep -ie nextSysDate {} \+

| (Pipe doesn’t work with find  so exec needs to be used in case of grep)

By | 2017-07-21T21:57:09+00:00 March 2nd, 2013|Unix|0 Comments

About the Author:

Leave A Comment