Tuesday 17 November 2015

Basic Linux Commands with Examples

                     In this post, I have given syntax and examples of some basic Linux commands which we need frequently (clear, which, mkdir, cat, cd, cd .., rm, rmdir, ls, mv, cp, chmod, man, top, kill, killall, su, apt-get install, ssh, scp, ifconfig, ping, iptables)
                     
                     Go through the following:


1. clear
                It is used to clear the screen.
Syntax: clear

2. which
               It is used to get a path of installed application/software.

Syntax: which application_name

Example:
              
3. mkdir
                It is used to create a new folder/directory.

Syntax: mkdir folder_name

Example:


4. cat
             It is used to create a text file from command line.

Syntax: cat > filename.txt

Example:
           

                        It can also be used to display content of file on terminal.

Syntax: cat filename.txt  

Example:

5. cd
              cd stands for Change Directory. It is used to enter into the folder.

Syntax: cd folder_name

Example:


6. cd ..
          It is used to come out from current directory.

Syntax: cd ..

Example:

7. rm

         It is used to delete a file or folder.

To delete file:

Syntax: rm filename

Example:

To delete folder and its subfolders:
    
Syntax: rm -r foldername

Example:


  8. rmdir
          It is used to delete empty folder/directory. 

Syntax: rmdir foldername

Example:

9. ls

        It is used to show list of all files and subfolders contained in the folder.

Syntax: ls

Example: 

(Note: Check more attributes of ls command by using command "man ls") 

10. mv
            It is used to move file/folder from Source Location to Destination Location.

Syntax: mv Source_Location Destination_Location 

Example: 
               In following example, we are moving three folders (test, college, placement) and one file (myfile.txt) from home folder to 'sample' folder.


 11. cp
           It is used to copy file/folder from Source Location to Destination Location.

           To copy file from source location to Destination location:

Syntax: cp Source_Location Destination_Location

Example:
           In following example, we are copying myfile.txt to folder 'test'.


           In following example, we are copying mysql-connector-java.jar file from Downloads folder to /usr/share/java folder:
       


           To copy folder and its subfolders from Source location to Destination location:

Syntax: cp -r Source_Location Destination_Location

Example:
          In following example, we are copying 'college' folder to folder 'test'.


12. chmod
        It is used to change mode/permissions of file or directory.

Syntax: chmod XXX filename/foldername

          (Note, here X is a Octal number from 0 to 7. First X stands for User, Second X stands for Workgroup, Third X stands for Others. Each X consists of three bits(1/0). First bit stands for Read permission, Second bit stands for Write permission, Third bit stands for Execute permission.)

Example:
       
       To change mode/permissions for all subdirectories:

Syntax: chmod -r XXX foldername


13. man
           It is used to get manual/help or details of any Linux command.

Syntax: man commandname 

Example:
            For ls command,






14. Application name like vlc, firefox, gedit

            We can directly run any application from terminal by mentioning process name e.g. vlc, firefox, gedit.

Syntax: processname (e.g. vlc)

Example: 
            For open vlc,

15. top  


           It is used to show all the running processes.

Syntax: top 

Example:


            
16. kill 
           It is used to kill/close any application or process by using it's process ID.

Syntax: kill process_ID

Example: 
(to close firefox whose process ID is 2374)

17. killall
            We can also kill/close any application or process by using its name with the help of command killall.

Syntax: killall process_name 

Example:
(To close/kill firefox,)

18. su  
            It is used to get logged in to Root account.

Syntax: su 

Example:

   
19. apt-get install / yum install
            It is used to install new application from the terminal.
            apt-get install is used for Debian based OS like Ubuntu while yum install is used for Red hat Linux based OS like Fedora.

Syntax: sudo apt-get install application_name
(Note: 'sudo' is used to get permission from the current user.) 
OR
yum install application_name


Example:
            To install vlc media player on Ubuntu,


20. ssh
            It is used to get logged in to the remote machine/system from same network.

Syntax: ssh username@remote_machine's_ip_address

Example:
            In following example, suppose we have to log in to user 'exam4' on system with IP address '192.168.5.18':

To Log Out, use 'exit' as shown below:


21. scp
           It is used to copy file/folder from one machine/system to another machine/system.

Syntax: scp Source_address Destination_address

Example:
           Suppose we are transferring 'myfile.txt' from Source machine (home directory on exam10@192.168.5.17) to Destination machine (exam4@192.168.5.18):


22. ifconfig
            It is used to get IP address of our system.

Syntax: ifconfig

Example:   
           As shown above, IP address of system is 192.168.5.17  .

23. ping
           Ping stands for Packet INternet Groper. It is used to check connectivity with other system. It uses ICMP protocol for the same.

Syntax: ping IP_address_of_another_system

Example:

24. iptables
             This command is used to allow or block the traffic to the system.

Syntax: iptables -I INPUT -p tcp --dport 8080 -j ACCEPT

Next: Mysql Tutorial - Basic SQL Queries

Previous: Finding Prime Numbers Between 1 to n in Optimized Way

1 comment: