Sunday, May 10, 2015

BASIC LINUX COMMANDS FOR HADOOP

9:18 AM - By ajay desai 0


                                                   BASIC LINUX COMMANDS

     1) Switch User:- To switch from one user to another. The below given command when executed,            switches from user:mrinmoy to user: root
 
       mrinmoy@ubuntu:~$ su root
 
       Password:
 
       root@ubuntu:/home/mrinmoy#



    2) cd .. :- this command is used to go back to the previous directory.


       root@ubuntu:/home/mrinmoy# cd ..
   
       root@ubuntu:/home#



    3) pwd : - to get the name of the present working directory.

       root@ubuntu:/home# pwd
   
       /home



    4) ll : - to display a list of files and directories present in the current working directory.
   
   

       root@ubuntu:/home# ll
   
       total 88
   
       drwxr-xr-x 15 root    root     4096 2015-05-02 10:07 ./
   
       drwxr-xr-x 23 root    root     4096 2012-12-08 06:46 ../
   
       drwxr-xr-x  2 root    root     4096 2015-04-28 12:00 49BATCH/
   
       drwxrwxrwx  3 root    root     4096 2012-11-19 06:21 Aadi/
   
       drwxr-xr-x  2 root    root     4096 2013-05-08 21:36 Batch11/
   
       drwxr-xr-x  2 root    root     4096 2013-10-17 00:49 batch17/
   
       drwxr-xr-x  2 root    root     4096 2012-12-14 07:41 Batch2/
   
       -rw-r--r--  1 root    root      354 2013-01-03 06:33 derby.log
   
       -rw-------  1 root    root    12288 2015-04-18 04:39 .file.log.swp
   
       drwxr-xr-x  9 root    root     4096 2013-05-07 21:32 Gopal/
   
       drwxr-xr-x 53 mrinmoy mrinmoy  4096 2015-05-03 07:58 mrinmoy/
   
       drwxr-xr-x  3 root    root     4096 2013-01-20 06:39 Online/
   
       drwxr-xr-x  2 root    root     4096 2013-01-01 06:06 Raj/
   
       drwxr-xr-x  2 root    root     4096 2013-01-23 07:27 rajoutput/
   
       -rw-r--r--  1 root    root       15 2012-11-19 07:50 sample.txt
   
       drwxr-xr-x  2 root    root     4096 2013-01-01 06:16 Sekhar/
   
       drwxr-xr-x  2 root    root     4096 2012-12-15 08:58 Sunday/
   
       drwxr-xr-x  2 root    root     4096 2012-11-19 07:43 todayadi/
   
       -rw-r--r--  1 root    root      125 2012-12-08 06:47 today.txt
   
       -rw-r--r--  1 mrinmoy root        3 2015-05-01 10:53 vijay.txt



   5) mkdir: - To create a directory in the local file system.

   

      root@ubuntu:/home# mkdir BATCH49



   6) cd: - to change the directory.

   

      root@ubuntu:/home# cd BATCH49



   7) touch: - to create an empty file within a directory.
   
   

      root@ubuntu:/home/BATCH49# touch vijay.txt
   

      root@ubuntu:/home/BATCH49# cat vijay.txt

 

      As seen in the above given example, an empty file called vijay.txt is created and when cat command is executed
      on the file to display its content, no content is displayed.



   8) nano: - to edit the content of a file.

 
      root@ubuntu:/home/BATCH49# nano vijay.txt
   
      root@ubuntu:/home/BATCH49# cat vijay.txt
   
      Hi I am Vijay, I am learning Hadoop Technology.
   
      I aspire to work as hadoop data analyst in Yahoo.Inc..



   9) mv: - To move a file from one directory to another.
   
   
      root@ubuntu:/home/BATCH49# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-03 10:07 ./
   
      drwxr-xr-x 17 root root 4096 2015-05-03 08:47 ../
   
      -rw-r--r--  1 root root  103 2015-05-03 10:07 vijay.txt
   
      root@ubuntu:/home/BATCH49# cd ..
   
      root@ubuntu:/home# cd 49test
   
      root@ubuntu:/home/49test# ll
   
      total 8
   
      drwxr-xr-x  2 root root 4096 2015-05-03 10:06 ./
   
      drwxr-xr-x 17 root root 4096 2015-05-03 08:47 ../
   
      root@ubuntu:/home/49test# cd ..
   
      root@ubuntu:/home# cd BATCH49
   
      root@ubuntu:/home/BATCH49# mv vijay.txt /home/49test
   
      root@ubuntu:/home/BATCH49# ll
   
      total 8
   
      drwxr-xr-x  2 root root 4096 2015-05-03 10:08 ./
   
      drwxr-xr-x 17 root root 4096 2015-05-03 08:47 ../
   
      root@ubuntu:/home/BATCH49# cd ..
   
      root@ubuntu:/home# cd 49test
   
      root@ubuntu:/home/49test# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-03 10:08 ./
   
      drwxr-xr-x 17 root root 4096 2015-05-03 08:47 ../
   
      -rw-r--r--  1 root root  103 2015-05-03 10:07 vijay.txt



   10) cp: - to copy a file from one directory to another directory of the local file system.

   
       root@ubuntu:/home# cd 49test
   
       root@ubuntu:/home/49test# cp vijay.txt /home/BATCH49
   
       root@ubuntu:/home/49test# cd ..
   
       root@ubuntu:/home# cd BATCH49
   
       root@ubuntu:/home/BATCH49# ll
   
       total 12
   
       drwxr-xr-x  2 root root 4096 2015-05-03 10:12 ./
   
       drwxr-xr-x 17 root root 4096 2015-05-03 08:47 ../
   
       -rw-r--r--  1 root root  103 2015-05-03 10:12 vijay.txt




  11) rm -r: - to delete a non-empty directory.

   
      root@ubuntu:/home# rmdir 49test
   
      rmdir: failed to remove `49test': Directory not empty
   
      root@ubuntu:/home# rm -r 49test




  12) cal: - to display the current calender with today's date highlighted.

   
      root@ubuntu:/home# cal
       
          May 2015
 
     Su Mo Tu We Th Fr Sa
                 
                                    1  2
 
      3   4     5    6    7    8  9
 
     10  11  12  13  14  15 16
 
     17  18  19  20  21  22 23
 
     24  25  26  27  28  29 30
 
     31



  13) date: - to display the current date with time.

 
      root@ubuntu:/home# date
 
      Sun May  3 10:41:35 PDT 2015



  14) stat: - To show the statistics or details of a file.

   

      root@ubuntu:/home/BATCH49# stat vijay.txt
     
      File: `vijay.txt'
     
      Size: 103       Blocks: 8          IO Block: 4096   regular file
   
    Device: 801h/2049d Inode: 1573868     Links: 1
   
    Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
   
    Access: 2015-05-03 10:12:25.434320043 -0700
   
    Modify: 2015-05-03 10:12:25.434320043 -0700
   
    Change: 2015-05-03 10:12:25.434320043 -0700



  15) gzip: - To compress a file.
 
   

      root@ubuntu:/home/BATCH49# gzip vijay.txt
   
      root@ubuntu:/home/BATCH49# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-04 10:56 ./
   
      drwxr-xr-x 16 root root 4096 2015-05-03 10:39 ../
   
      -rw-r--r--  1 root root  115 2015-05-03 10:12 vijay.txt.gz
   
      root@ubuntu:/home/BATCH49# cat vijay.txt.gz
      �VFU vijay.txt%�A
      � E�y�x



  16) gunzip: - To decompress a file.
   
   
      root@ubuntu:/home/BATCH49# gunzip vijay.txt.gz
   
      root@ubuntu:/home/BATCH49# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-04 11:02 ./
   
      drwxr-xr-x 16 root root 4096 2015-05-03 10:39 ../
   
      -rw-r--r--  1 root root  103 2015-05-03 10:12 vijay.txt
   
      root@ubuntu:/home/BATCH49# cat vijay.txt
   
      Hi I am Vijay, I am learning Hadoop Technology.
   
      I aspire to work as hadoop data analyst in Yahoo.Inc..



  17) grep: - To search for a particular keyword and mark its instances in a file. this command is case-sensitive.


      root@ubuntu:/home/BATCH49# cat vijay.txt
   
      Hi I am Vijay, I am learning Hadoop Technology.
   
      I aspire to work as hadoop data analyst in Yahoo.Inc..
   
      root@ubuntu:/home/BATCH49# grep Hadoop vijay.txt
   
      Hi I am Vijay, I am learning Hadoop Technology.



  18) grep -i: - To search for a particular keyword and mark its instances in a file. This command is case-insensitive.


   
      root@ubuntu:/home/BATCH49# grep -i Hadoop vijay.txt
   
      Hi I am Vijay, I am learning Hadoop Technology.
   
      I aspire to work as hadoop data analyst in Yahoo.Inc..



  19) wc: - To count and display the total no: of lines, words and characters in a file.

 

      root@ubuntu:/home/BATCH49# wc vijay.txt
 
      2  19 103 vijay.txt



  20) wc -l: - To count and display the no: of lines in a file.

   

      root@ubuntu:/home/BATCH49# wc -l vijay.txt
   
      2 vijay.txt



  21) wc -w: - To count and display the no: of words in a file.
   
   

      root@ubuntu:/home/BATCH49# wc -w vijay.txt
   
      19 vijay.txt



  22) wc -c: - To count and display the no: of characters in a file.

   

      root@ubuntu:/home/BATCH49# wc -c vijay.txt
   
      103 vijay.txt



  23) ln: - used to create a link for a file or directory.

   

      root@ubuntu:/home# ln -s BATCH49 B
   
      root@ubuntu:/home# cd BATCH49
   
      root@ubuntu:/home/BATCH49# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-04 11:02 ./
   
      drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
      -rw-r--r--  1 root root  103 2015-05-03 10:12 vijay.txt
   
   
      root@ubuntu:/home/BATCH49# cd ..
   
      root@ubuntu:/home# cd B
   
      root@ubuntu:/home/B# ll
   
      total 12
   
      drwxr-xr-x  2 root root 4096 2015-05-04 11:02 ./
   
      drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
      -rw-r--r--  1 root root  103 2015-05-03 10:12 vijay.txt



 24) Redirectional command (>>): - used to redirect the output of a file to a newly created file.

   
     root@ubuntu:/home/BATCH49# wc vijay.txt>>abhay.txt
   
     root@ubuntu:/home/BATCH49# cat abhay.txt
   
     2  19 103 vijay.txt



 25) How to copy the contents of a file into another file with a different name

   

    root@ubuntu:/home/49BATCH# ll
   
    total 16
   
    drwxr-xr-x  2 root root 4096 2015-05-05 03:30 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root   13 2015-04-27 14:15 ajay.txt
   
    -rw-r--r--  1 root root   59 2015-04-28 12:00 akshata.txt
   
    -rw-r--r--  1 root root    0 2015-05-05 03:30 vijay.log
   
 
    root@ubuntu:/home/49BATCH# cp /home/BATCH49/vijay.txt vijay.log
   
    root@ubuntu:/home/49BATCH# cat vijay.log
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as hadoop data analyst in Yahoo.Inc..

   
 
    root@ubuntu:/home# cd BATCH49
   
    root@ubuntu:/home/BATCH49# cat vijay.txt
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as hadoop data analyst in Yahoo.Inc..



 26) How to rename a file

   

    root@ubuntu:/home/BATCH49# ll
   
    total 16
   
    drwxr-xr-x  2 root root 4096 2015-05-05 03:58 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root root  103 2015-05-03 10:12 vijay.txt
   
   
    root@ubuntu:/home/BATCH49# cat vijay.txt
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as hadoop data analyst in Yahoo.Inc..
   

    root@ubuntu:/home/BATCH49# mv vijay.txt xyz.log
   
    root@ubuntu:/home/BATCH49# ll
   
    total 16
   
    drwxr-xr-x  2 root root 4096 2015-05-05 03:59 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root root  103 2015-05-03 10:12 xyz.log
   
   

    root@ubuntu:/home/BATCH49# cat xyz.log
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as hadoop data analyst in Yahoo.Inc..



 27) sed: - to replace a particular keyword with the given keyword at various instances in a file.

   
    root@ubuntu:/home/BATCH49# cat xyz.log
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as Hadoop data analyst in Yahoo.Inc..

   

    root@ubuntu:/home/BATCH49# sed 's/Hadoop/Teradata/g' xyz.log
   
    Hi I am Vijay, I am learning Teradata Technology.
   
    I aspire to work as Teradata data analyst in Yahoo.Inc..

   

    root@ubuntu:/home/BATCH49# cat xyz.log
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as Hadoop data analyst in Yahoo.Inc..

   

    But when we use the cat command for the file: xyz.log again, we see the same old content as shown above.

   
    So to save the changes permanently we need to redirect the output of xyz.log to some other file as shown below: -

 
    root@ubuntu:/home/BATCH49# sed 's/Hadoop/Teradata/g' xyz.log>>abc.log
   
    root@ubuntu:/home/BATCH49# cat abc.log
   
    Hi I am Vijay, I am learning Teradata Technology.
   
    I aspire to work as Teradata data analyst in Yahoo.Inc..

   

    root@ubuntu:/home/BATCH49# cat xyz.log
   
    Hi I am Vijay, I am learning Hadoop Technology.
   
    I aspire to work as Hadoop data analyst in Yahoo.Inc..



 28) How to copy the contents of one directory to another directory

   

    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 root root 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root  107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 root root   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root root  103 2015-05-05 04:16 xyz.log
   
   

    root@ubuntu:/home/BATCH49# cd ..
   
    root@ubuntu:/home# cd 49BATCH
   
    root@ubuntu:/home/49BATCH# ll
   
    total 20
   
    drwxr-xr-x  2 root root 4096 2015-05-05 03:58 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root   13 2015-04-27 14:15 ajay.txt
   
    -rw-r--r--  1 root root   59 2015-04-28 12:00 akshata.txt
   
    -rw-r--r--  1 root root  103 2015-05-05 03:48 vijay.log

   

    root@ubuntu:/home/49BATCH# cd ..

   
    root@ubuntu:/home/BATCH49# cp -r *.log /home/49BATCH

   
    root@ubuntu:/home/BATCH49# cd ..
   
    root@ubuntu:/home# cd 49BATCH
   
    root@ubuntu:/home/49BATCH# ll
   
    total 28
   
    drwxr-xr-x  2 root root 4096 2015-05-05 04:31 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root  107 2015-05-05 04:31 abc.log
   
    -rw-r--r--  1 root root   13 2015-04-27 14:15 ajay.txt
   
    -rw-r--r--  1 root root   59 2015-04-28 12:00 akshata.txt
   
    -rw-r--r--  1 root root  103 2015-05-05 03:48 vijay.log
   
    -rw-r--r--  1 root root  103 2015-05-05 04:31 xyz.log



 29) How to change the owner of a file (The owner is a person who created the file or directory and has read,write and
     execute permissions on the directory but has only read and write permissions on the file by default)

   
    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 root root 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root root  107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 root root   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root root  103 2015-05-05 04:16 xyz.log

   

    root@ubuntu:/home# chown mrinmoy BATCH49
   
    root@ubuntu:/home# cd BATCH49

   
    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 mrinmoy root 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root    root 4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root    root  107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 root    root   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root    root  103 2015-05-05 04:16 xyz.log

   

    As seen above, the owner of the directory: Batch49 has changed, but the directory's files are still owned by the
    user: root. So to change the owner for all the
 content of the directory we need to use recursive function as follows:-

   
    root@ubuntu:/home# chown -R mrinmoy BATCH49
   
    root@ubuntu:/home# cd BATCH49
   
    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 mrinmoy mrinmoy 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root    root    4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 mrinmoy root     107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 mrinmoy root      22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 mrinmoy root     103 2015-05-05 04:16 xyz.log




 30) How to change the group of a file (group represents a group of members who are related to that directory or file and
     have only execute and read operations on the directory but only read permission on the file by default)
   
   
    root@ubuntu:/home# chgrp mrinmoy BATCH49
   
    root@ubuntu:/home#  cd BATCH49
   
    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 mrinmoy mrinmoy 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root    root    4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 root    root     107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 root    root      22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 root    root     103 2015-05-05 04:16 xyz.log

   

    As seen above, the group of the directory: Batch49 has changed, but the directory's files are still owned by the
    user: root. So to change the owner for all the
 content of the directory we need to use recursive function as follows:-

   

    root@ubuntu:/home# chgrp -R mrinmoy BATCH49
   
    root@ubuntu:/home# cd BATCH49
   
    root@ubuntu:/home/BATCH49# ll
   
    total 20
   
    drwxr-xr-x  2 mrinmoy mrinmoy 4096 2015-05-05 04:22 ./
   
    drwxr-xr-x 16 root    root    4096 2015-05-05 03:24 ../
   
    -rw-r--r--  1 mrinmoy mrinmoy  107 2015-05-05 04:22 abc.log
   
    -rw-r--r--  1 mrinmoy mrinmoy   22 2015-05-05 03:32 abhay.txt
   
    -rw-r--r--  1 mrinmoy mrinmoy  103 2015-05-05 04:16 xyz.log


31) chmod: - this command is used to change the read, write and execute permissions for a file or directory.

    root@ubuntu:/home# mkdir BATCH49
     
     root@ubuntu:/home# cd BATCH49
     
    root@ubuntu:/home/BATCH49# ll
     
    total 8
     
    drwxr-xr-x  2 root root 4096 2015-05-05 07:43 ./
     
    drwxr-xr-x 15 root root 4096 2015-05-05 07:43 ../

     
    root@ubuntu:/home/BATCH49# nano ajay.txt
     
         
    root@ubuntu:/home/BATCH49# ll
     
    total 12
     
    drwxr-xr-x  2 root root 4096 2015-05-05 07:45 ./
     
    drwxr-xr-x 15 root root 4096 2015-05-05 07:43 ../
     
    -rw-r--r--  1 root root  121 2015-05-05 07:45 ajay.txt

     

    root@ubuntu:/home# chmod 777 BATCH49
   
     
    root@ubuntu:/home# cd BATCH49
     
    root@ubuntu:/home/BATCH49# ll
     
    total 12
     
    drwxrwxrwx  2 root root 4096 2015-05-05 07:45 ./
     
    drwxr-xr-x 15 root root 4096 2015-05-05 07:43 ../
     
    -rw-r--r--  1 root root  121 2015-05-05 07:45 ajay.txt

     

    Now if we want to apply the same access permissions of the directory on all the content of the directory, we need to
    use recursive function as follows: -

     
    root@ubuntu:/home# chmod -r 777 BATCH49

     
    root@ubuntu:/home# cd BATCH49
     
    root@ubuntu:/home/BATCH49# ll
     
    total 12
     
    drwxrwxrwx  2 root root 4096 2015-05-05 07:45 ./
     
    drwxr-xr-x 15 root root 4096 2015-05-05 07:43 ../
     
    -rwxrwxrwx  1 root root  121 2015-05-05 07:45 ajay.txt*

Tags: ,
About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top