Basic unix commands for java developers

Command to know the version of UNIX/LINUX installed

uname –a

 

Command to know the Home directory of User

echo $HOME

Navigation Command

cd .
Current Directory
cd ..
Parent Directory
cd
Home Directory
cd –
to go back to the 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 the current directory

File Command

Creation of File

vi   <File_Name_To Create>

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

OR

Touch <File_Name_To_Create>

Note*: It will only create the file with the 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 the 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

The numeric value of file permission would be of length 3.  The 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 categories 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 then the final value would be 7-4= 3.

Group Permission

As given above look for the permission a group has and then the 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 the 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 the final value would be 7-4= 3

In the end we will add all 3 numbers obtained through the 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)

What is the best way to know the list of Users in Linux?

There is no direct command to know the list of Users in Linux. But alternate ways are available to know the list of Users. Linux/Unix stores users Login related information in a file “/etc/passwd“. This file can be used to list down users.

$ cat /etc/passwd

Command that can be used to know logged in users

  1. who
  2. finger
  3. users

I have already logged in as Super User. Now the question is how can i logged out of Super User so that i can login as another user?

You just need to use exit command to go out of any user. and you can log in as another user by using again su command.
su <user_name>

How to login as Super or Root User in Linux?

su command  can be used to login as Super User.
Execute the Command as “su –” it will ask for super user password. If successful you will be logged in as Super User.

Now how would you know if you have logged in as Super User or not?

Check for the icon on console.

$ : Means you are not acting as root / super user.
# : Means you are acting as root / super user.

What is the difference between su and sudo?

Both are ways to gain root access. But with su you will get root access and will be switched to root access. You will be in root access until you explicitly exit from root privileged. While with sudo runs single command with root privileged. Once command runs properly you will be back in normal mode.

How to change command prompt message?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.