Linux Commands You Must Know

In this Blog we are going to discuss all the necessary and basic commands which  every IT professional working on linux platform should know.

Computer-savvy individuals consider Linux as the best operating system owing its feature of getting customized more easily than its popular counterparts.

To learn better there are screenshot attached with every commands to practice and refer for understanding.

 

1.ls

     a)Syntax:– ls

This command is used to list all the files and directories.

Snapshot:

All the files present in the current directory are shown once the ls command was typed in the terminal.

     b) Syntax :-  ls -ltr filename

command is used to list one file with name mentioned in command.

Snapshot:

Here you can see the command gives full detail about the file acadgild.
     

     c)Syntax: – ls –ltr

 -l long listing

-t time

-r recursive

Command is used to lists in the order of their creation time . list of files and directories contains permissions, links, ownership, group, date, time and file-name .

 Snapshot:-

Here you can see the list of all files present in the directory arranged  in order of creation . Different columns indicate permissions, links, ownership, group, date, time and file-name. For every file all these content are stored in computer by default .

     

     d) Syntax:- ls –lc filename

-l long listing

-c character specific file

Snapshot:-

Here you can see the listing of file in alphabetical order.
     

e)Syntax:– ls -la

To display all hidden files. All Hidden files are start with DOT(.)

 

Snapshot:-

Here you can see all the hidden files present in the current directory. We can notice all file is prefixed with .(dot) which indicates all file are system files, hidden from users.

 2.touch

 

     a)Syntax:- touch <filename>

To create an empty file. Size of file is 0 bytes.

Snapshot :-

Here file will be generated with name acadgild and will be present in same location until user modifies/deletes it.

 

     b)Syntax: touch f1 f2 f3 f4

Snapshot:-

Here the command will create 4 files named f1,f2,f3 and f4 at same location in the system.

 

3.mkdir

 

     a)Syntax:- mkdir directoryname

 

To create a new directory

Snapshot:-

Here you can see a directory named acadgild is created by the user.

 

     b)Syntax:- mkdir -p d1/d2/d3/d4/…………

 

-p parent directory

To create a directories & subdirectories recursively

 

Snapshot:-

Here you can see several directories and subdirectories are created and given parent permission at a time.

 

4.rm

     a)Syntax:- rm filename

It is used for to remove a file/files

Snapshot:-

Here you can see the file f1 is being deleted.

 

     b)Syntax:- rm -rf  < filename/directoryname >

 

-r recursively

                   -f         forcefully

To remove files, directories and subdirectories Recursively or Forcefully

Snapshot:-

Here you can see the the directory which contain data inside will also get deleted by using this -rf along with rm.

     c)Syntax:– rmdir < directoryname >

To remove an empty directory

Snapshot:-

Here you can see directory named sasi is being deleted.

5.echo

     a)Syntax:– echo

It reflects back the content ,whatever written after echo command.

Snapshot:-

Here you can see the comment written just after the command echo is printed again i.e anything following the echo command is displayed on the terminal.

     b)Syntax:- echo “some content” > filename

To insert text inside a file and will delete all previous data of the file.

Snapshot:-

Here you can see the statement is redirected to the file acadgild as content and later the content can be printed by cat command.

 

     c)Syntax:- echo “some content” >> filename

 

Snapshot:-

Here you can see the statement is redirected to the file acadgild along with the previous data as content and later the content is printed by cat command.

 

6.cat

Syntax:- cat filename

To read a file

Snapshot:-

Here you can see the cat command is used to through all the contents of file to the terminal.

 

7.more

 

Syntax:- more filename

 

To read a file page-by-page,

use SPACE-BAR to move for NEXT PAGE

Snapshot:-

 

 

 

Here you can see the data of /etc/profile is shown page by page. It is used to read long file, with lots of data.

 

8.inode

 

Syntax:- ls –i filename

 

Snapshot:-

Each inode stores the attributes and disk block location(s) of the filesystem object’s data

Here you can see for a file(acadgild) one inode will be created and that inode will be associated to file(acadgild).

 

9.who

 

     a)Syntax:- who

It shows the information about the users who are logged into the system currently.

Snapshot:-

 

Here you can see two user logged in named root and pranu with detail of what date, time and IP they used to log IN.

     b)Syntax: who –b

          -b  indicates the time and date of the last reboot.

To see when the server/system was started.

Snapshot:-

Here you can see the last time when system started/booted was 29/12 at 12.06 pm.

     c)Syntax:- who -r

-r indicates the current run level of the init process

A run level is a state of init and the whole system that defines what system services are operating.

Snapshot:-

Here you can see the run level is 3

     d)Syntax:- who -a

-a lists processes, variables, users, run-levels

Snapshot:-

Here you can see all the who commands summary at glance.
10.cd

 

Can be used in two ways, either way will result change in directory

 

     a)Syntax:- cd <directory name>

Is  used to change directory

Snapshot:-

Here you can see we entered inside acadgild directory.

 

     b)Syntax:- cd ..

 

This command is used to exit from a directory.

Snapshot:-

Here you can see the command brings us one directory prior to where we was.

 

11.pwd

 

Syntax:- pwd

 

To display the present Working Directry

Snapshot:-

Here you can see the path of the current working directory. It is very useful while working on terminal.

12.date

 

Syntax:- date

To display the current date and time of the system.

Snapshot:-

Here you can see the current day,month,date and time followed by timing zone and year.

 

13.cal

      a)Syntax:- cal

It shows the present month calendar

 

Snapshot:-

Here you can see the calendar for a particular month.

 

      b)Syntax:- cal <year>

To display the calendar of the year mentioned.

 

Snapshot:-

Here you can see the calendar for year 2015.

14.mv

Syntax:- mv <sourcefile> <destinationfile>

It is used to rename a file or move the file from one location to another location

 

Snapshot:-

Here you can see a file f2 is being moved with the new name f3, with all the contents of file f2 preserved in f3.

 

 

15.cp

       a)Syntax:- cp <source file> <targetfile>

To copy a file in same directory.

Snapshot:- 

Here you can see the file m1 content is copied to file m2.

 

     b) Syntax:-cp -r :

 

To copy all files and subdirectories recursively present inside a parent directory.

 

Screenshot:-

Here you can see directories named big data, frontend and hadoop is copied when the parent directory is copied

     

     c) Syntax:-cp -f :

 

-f     Unlink. If a file descriptor for  a  destination  file

          cannot be obtained, this option attempts to unlink the

          destination file and proceed.

 

     d)Syntax:-cp -i :

 

                   -i         Interactive. cp prompts for confirmation whenever  the

          copy  would  overwrite  an existing target.

     e) Syntax:-cp -p :

 

-p preserve

 

it is use to (keep the same detail record of original file)preserve the permission, time, date of source file to target file

     

f)Syntax:- cp –rpfi <sourcefile> <destinationfile>

Snapshot:-

Here you can see the the contents of p1 is forcefully pushed into p2 by deleting all the previous data of p2.

 

16.df

     a)Syntax:- df -k

-k prints the allocation in Kilobytes

 

To display/read the free DISK FILES.

It lists file system name, allocated size, used size, available size, used size in %, mounted on.

 

Snapshot:-

Here you can see list of all the mounted disk path with actual disk utilization status in detail.

 

     b)Syntax: df -h

 

-h prints the allocation in  human readable format. scaling is done by repetitively dividing by 1024.

 

To display/read the DISK FILES in format

 

Snapshot:-

Here you can see the output is same as previous command but with only one difference the size are displayed in GB and MB which is more comfortable for humans to understand, as storage constraint is negligible in today’s era.

 

17.man

Syntax:- man <command-name>

 

It displays the full information about the command. Any command detail can be found inside man.

 

Snapshot:-

 

Here you can see the description of a command cat.

 

18.which

 

Syntax:- which <command-name>

It display  where the command is residing

Snapshot:-

Here you can see cat location is shown as output i.e, cat is being used from /usr/bin/cat .

 

19.uptime

Syntax:- uptime

 

To find out since how long the server is up & running

Snapshot:-

Here you can see the server is in running state science 2:14pm for 2 hours and 8 minutes.

 

20.id

 

      a)Syntax:- id

 

To display the present logged in user-id and group-id with username and groupname.

Snapshot:-

Here you can see the present user id  and group id you are logged in.

 

     b)Syntax:- id -g username

 

it shows only the group id

Snapshot:-

Here you can see only the group ID for acadgild user.

 

21.finger

 

Syntax:- finger

 

To see how many users logged into the system and also display the user name and its idle time.

Snapshot:-

Here you can see the root user is sitting idle for 2 hours  and 7 minutes.

 

22.write

 

The write utility reads lines from the user’s standard input and  writes them to the terminal of another user. When first invoked, it writes the message:

    Message from sender-login-id (sending-terminal) [date]…

    to user. When it has successfully completed the  connection,

    the sender’s terminal will be alerted twice to indicate that

    what  the  sender  is  typing  is  being  written   to   the

    recipient’s terminal.

 

To send a message to the user who is using the same system

 

23.whoami

 

Syntax:- whoami

 

To see which username you logged in

 

Snapshot:-

          Step 1:- PATH=/usr/bin:/usr/ucb (enter)

 

          Step 2:- Export PATh (enter)

 

          Step 3:- Whoami

Snapshot:-

Here you can see the username of system.

 

24.ping

 

Syntax:- ping ipaddress

This command is used to find out either server is alive are not.

 

Snapshot:-

Here you can see the the IP 192.168.56.24 is alive.

 

For continous reply ping -s Ipaddress

 

25.telnet

 

      a)Syntax:- telnet ipaddress

 

 

This command is used to connect the server (remote server).

 

Snapshot:-

Here you can see server is connected and after login it shows few details on last login.

 

26.nslookup

 

Syntax:- nslookup DNS name (ip address from DNS)

 

It is a name server lookup to resolve the host IP address from DNS name ( forward zone ) and to get hostname from the ip address ( reverse zone ) .

Syntax:- nslookup ipaddress

 

DNS from ip address

 

Snapshot:-

Here you can see the lookup command will search for the address of google and communicate with it, either with domain name or IP.

 

27.ifconfig

 

       a)Syntax:- ifconfig -a

-a = to all the interfaces

To see the Ip Address of the server we use this command

 

 

        Step 1:-PATH=/usr/bin:/usr/ucb (enter)

 

        Step 2:-Export PATH (enter)

 

         Step 3:-Ifconfig -a

 

Snapshot:-

Here you can see the IP of the server is 192.168.56.24

 

      b)Syntax:- ipconfig

 

In windows we use the command in cmd prompt for the same.

28.PERMISSIONS:

 

The full permission of a file/directory is 7 -7 -7 = rwx -rwx -rwx = ownership -group -other group

 

r-Read =4

w-Write =2

x-Execution = 1

so total rwx =7

 

The maximum permissions for a file is 6 -6 -6 = -rw- -rw- -rw-(- prefixed in owner  represents the type; file)

 

When we create a file the default permissions will 6 – 4 – 4. it Is obtained by reducing umask value to maximum permissions . i.e 666 – 022 = 644 (d prefixed in owner represent the type; directory)

 

Snapshot:-

 

The maximum permissions for a directory is 7 – 7 -7. it is obtained by reducing umask value to maximum permissions. i.e 777-022 = 755.

 

Snapshot:-

 

29.chmod

 

      a)Syntax:- chmod 755 filename

To change permissions of a file or directory

Snapshot:-

Here you can see the permission for the file is changed to 755 for user,group and owner.
     

     b)Syntax:- chmod -R 755 directory_name

This command is used to change the permissions recursively for a directory

 

Snapshot:-

Here you can see the directory dishitha is changed with permission 755.

 

      c)Syntax:- chmod 755 *

 

This command is used to change the permissions for all files & directories of present working directory

 

 

 

Snapshot:-

Here you can see all files permission is changed to 755 in the current directory.

      d) Syntax:-chmod 444 file name

Read permissions only we can given

 

Snapshot:-

Here you can see the file dishitha is given only read permission.

 

30.chown

 

There are different ways to use this command

 

      a)Syntax:-chown username(or)groupname filename (command is used only in root)

 

This command is used for the change the ownership

 

Screenshot:-

 

 

     b)Syntax:– chown username:group directory name (or) file name

 

To change the ownership of the file or directory. This command should be executed by root ( / ) user.

Screenshot:-

 

     c)Syntax:– chown –R username:group directory name (or) filename

 

-R=for recursive change

 

Snapshot:-

Here you can see the file hadoop owner is changed from root to 555.

 

31.ps

 

     a)Syntax:- ps -ef

PROCESS STATUS. To list all the process status.

 

e = entire running process

f = generate full listing process

 

Snapshot:-

Here you can see the list of all process status in detail.

 

      b)Syntax:- ps -ef |grep java

To display the java process

Grep is used for filtering/searching

| pipe symbol is used for sending output of one command to next command as a input if we use two or more commands in the program.

 

Grep -v …..void excluding

 

Snapshot:-

Here you can see the java process status in details.

 

32.du

 

     a)Syntax:– du -k filename

 

-k       Write the files  sizes  in  units  of  1024  bytes,

             rather than the default 512-byte units.

DISK UTILITY. To see how much size is occupying in the disk by each file & directory.

 

Snapshot:-

Here you can see the file dishu is occupying 1 disk utility.

      b)Syntax:- du -sk filename

 

-s       Instead of the  default  output,  report  only  the

             total sum for each of the specified files.

To display the each file & directory size recursively of present working directory.
Snapshot:-

Here you can see the dishitha is taking 1 disk utility for parent.

 

      c)Syntax:- du -sh filename

 

-h       All sizes are scaled to a  human  readable  format,

             for  example,  14K, 234M, 2.7G, or 3.0T. Scaling is

             done by repetitively dividing by 1024.

       To display the disk utility of a file & directory in kilobytes.

To display the utility size in human readable format

Snapshot:-

Here you can see the current directory is taking 1 KB of disk utility.

33.adduser

Syntax:- useradd<new username>

This command adds new user to login with

To check if user got added successfully to the system. we can see inside the master directory where all users of the system is present. i.e. /etc/passwd.

we can follow the screenshot below.

We see here the user which we created using command useradd is present.

 

34.clear

Syntax:- clear

 

It clears the terminal by bringing the current line with the cursor to the top of the terminal.You may see the screenshot for better unserstanding.

After the command executes  we get our terminal screen like below.

 

35.diff

diff <filename1> <filename2>

diff3 <filename1> <filename2> <filename3>

(a)Syntax:- diff filename1 filename2

Helps us tocomparee contents of two files.

 

(b)Syntax:- diff3 <filename1> <filename2> <filename3>

This also compares the file but instead fo 2 it can compaire 3 files at a time.

 

36.eject

Syntax:- eject

This commnad when entered in terminal it opens the CD-Rom drive out.

 

 

37.head

Syntax:- head <filename>

This command shows only few starting lines of any file. Unless like cat printing the complete file records, it saves memory, process time and resource time of the system.

 

38.tail

Syntax:- tail filename

 

This command shows only few end lines of any file. Unless like cat printing the complete file records, it saves memory, process time and resource time of the system.

 

39.kill

Syntax:- kill <process id>

If we know the process id of any program we can stop it through the terminal using kill command. In below screenshot, we find processid for java processes and we stop one of the process using kill.

 

40.locate

Syntax:- locate filename

The command helps user to find any type of file or directory present in system.

 

41.passwd

Syntax:- passwd 

This will cange the pasdword for the user you are currently logged in.

 

42.tar

-cf

-tvf

-xvf

(a)Syntax:- tar -cf <filename>

This command shows the list of files compressed inside.

(b)Syntax:- tar -tvf <filename>

This will list all the files along with properties of files such as there permissions, user access ,file size, date and time.

(c)Syntax:- tar -xvf <filename>

This will extract the file sin the present directory.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s