Linux Commands Hands-on

Linux Commands Hands-on

Linux Main Commands – useful to complete the entire workshop

Start Desktop

  • Login as hduser
  • Click on the right top corner and Chose Hadoop User.
  • Enter password – <your password>
  • screen-shot-2016-12-16-at-2-14-27-am
    • Click on the Top Left Ubuntu Button and search for the terminal and click on it.

    screen-shot-2016-12-16-at-2-18-44-am

    • You should see something similar as below

    screen-shot-2016-12-16-at-2-20-24-am

 

Learn Unix on a different directory

  • Create directory ‘scratch’ to learn unix and change the directory into it.
  • mkdir /home/hduser/scratch
  • cd /home/hduser/scratch

 

  • mkdir – make directories
    Usage: mkdir [OPTION] DIRECTORY…

    mkdir pvpsit
  • mkdir btech3rdyear
  • mkdir btech4thyear

 

  • ls – list directory contents
    Usage: ls [OPTION]… [FILE]…
    eg. ls, ls ­l, ls lhn
  • ls -lart # it should show you the directory you have just created.

 

 

  • cd – changes directories
    Usage: cd [DIRECTORY]
    eg. cd lhn
cd btech4thyear # it will change your current directory
ls -lart   # it should not show anything as this directory does not have anything yet

 

  • pwd ­-  print name of current working directory
    Usage: pwd

 

pwd # should print the current working directory.

o/p should show - 
/home/hduser/scratch/btech4thyear

 

  • nano – text editor
    Usage: nano <file name>
    eg. nano test.txt
nano test.txt # opens a file, write something

o/p
it should open a file to be edited in the command line. Please use CTRL X and chose option save to come out from there.

check using -
ls -lart

 

  • cp – copy files and directories
    Usage: cp [OPTION]… SOURCE DEST
    eg. cp sample.txt sample_copy.txt
    cp sample_copy.txt target_dir

 

cp test.txt test2.txt  # make a copy of your file just now you have created

check using -
ls -lart

 

  • mv – move (rename) files
    Usage: mv [OPTION]… SOURCE DEST
    eg. mv source.txt target_dir
    mv old.txt new.txt

 

mv test.txt  test1.txt # it will rename your file to test1.txt
check using -
ls -lart

 

  • rm ­ remove files or directories
    Usage: rm [OPTION]… FILE…
    eg. rm file1.txt , rm ­rf some_dir

 

rm test2.txt # it will delete the test2.txt
check using - 
ls -lart #you should see only one file test1.txt

 

  • find – search for files in a directory hierarchy
    Usage: find [OPTION] [path] [pattern] eg. find file1.txt, find ­name file1.txt

 

find /home/hduser -name "*.txt" # will display all the files with extension .txt

 

  • history – prints recently used commands
    Usage: history

 

history  # should show all the commands just now you ran - helpful or not ?

 

  • cat – concatenate files and print on the standard output
    Usage: cat [OPTION] [FILE]…
    eg. cat file1.txt file2.txt
    cat ­n file1.txt

 

cat test1.txt # it will show the contents which just now you have created.

 

  • echo – display a line of text
    Usage: echo [OPTION] [string] …
    eg. echo I love India
    echo $HOME
echo "Hello Amar" # just any string

echo $PATH   # environment variable - tells where the files can be present.

echo $JAVA_HOME #environment variable


  • grep ­- print lines matching a pattern
    Usage: grep [OPTION] PATTERN [FILE]…
    eg. grep ­i apple sample.txt

 

grep <contents> test1.txt # should show only line which contains the word

 

  •  wc ­- print the number of newlines, words, and bytes in files
    Usage: wc [OPTION]… [FILE]…
    eg.  wc file1.txt
    wc ­L file1.txt

 

wc test1.txt # will show you how many characters,words and line your file have.

 

  • sort – sort lines of text files
    Usage: sort [OPTION]… [FILE]…
    eg. sort file1.txt
    sort ­r file1.txt

 

sort test1.txt # show the contents sorted.

 

  • tar – to archive a file
    Usage: tar [OPTION] DEST SOURCE
    eg. tar ­cvf /home/archive.tar /home/original
tar ­xvf /home/hduser/scratch # will make a tar of your directory.

 

  • kill – to kill a process(using signal mechanism)
    Usage: kill [OPTION] pid
    eg. kill ­9 2275

 

kill -9 2275 # will stop the process with process id 2275

 

  • ps – report a snapshot of the current processes
    Usage: ps [OPTION]
    eg. ps,  ps ­el

 

ps -eaf  # will show all the processes running currently

 

  • who – show who is logged on
    Usage: who [OPTION]
    eg. who , who ­b , who ­q

 

who # will list down totoal number of users currently logged in , in you case it will show only one user 'hduser'

 

  • passwd – update  a user’s authentication tokens(s)
    Usage: passwd [OPTION]
    eg. passwd

 

passwd # will prompt to take a new password - please don't do this during the workshop

 

  •  su –  change user ID or become super­user
    Usage: su [OPTION] [LOGIN]
    eg. su remo, su

 

su akhil # it will ask password for akhil and then you can login as user akhil

 

  • chown – change file owner and group
    Usage: chown [OPTION]… OWNER[:[GROUP]] FILE…
    eg. chown remo myfile.txt

 

chown test1txt # requires you to be working as root - please don't try this during workshop

 

  • chmod – change file access permissions
    Usage: chmod [OPTION] [MODE] [FILE]
    eg. chmod 744 calculate.sh
ls -lart test1.txt # observe file permission

chmod -x test1.txt # observer the permission again

ls -lart test1.txt

chmod 777 test1.txt # observe the permission again

ls -lart test1.txt
  • zip – package and compress (archive) files
    Usage: zip [OPTION] DEST SOURSE
    eg. zip original.zip original

 

zip / gzip test1.txt # it will zip the file and rename it with .zip

check using -

ls -lart

 

  • unzip – list, test and extract compressed files in a ZIP archive
    Usage: unzip filename
    eg. unzip original.zip

 

unzip/gunzip test1.zip  # it will uncompress your file again

 

  • ssh – SSH client (remote login program)
    “ssh is a program for logging into a remote machine and for
    executing commands on a remote machine”
    Usage: ssh [options] [user]@hostname
    eg. ssh ­X guest@10.105.11.20

 

ssh akhil@192.168.1.19 # you will login to the box with IP address 192.168.1.19 with user akhil

 

  • scp – secure copy (remote file copy program)
    “scp copies files between hosts on a network”
    Usage: scp [options] [[user]@host1:file1] [[user]@host2:file2]
    eg. scp file1.txt guest@10.105.11.20:~/Desktop/
scp test1.txt akhil@192.168.1.19:/home/akhil/  # it copies the file test1.txt to system having IP 192.168.1.19 in user akhil's home directory.

 

  • du – estimate file space usage
    Usage:  du [OPTION]… [FILE]…
    eg. du
du -s -h /home/hduser  # will show the total space occupied in /home/hduser directory.
  • df – report filesystem disk space usage
    Usage: df [OPTION]… [FILE]…
    eg. df

 

df -h . # it will show you the disk usage - very useful command

 

  • reboot – reboot the system
    Usage: reboot [OPTION]
    eg. reboot
reboot # it reboots the system - however it require you to be root. Please don't do it during the workshop

 

  • gedit ­ A text Editor. Used to create and edit files.
    Usage: gedit [OPTION] [FILE]…
    eg. gedit

 

gedit test1.txt # opens your file in GUI mode for editing - very useful for beginners

 

  • bg – make a foreground process to run in background
    Usage: type ‘ctrl+z’  and then ‘bg ‘
type ‘ctrl+z’  and then ‘bg ‘ # please run ctrl+z command in any foreground process and then type bg

 

  • fg – to make background process as foreground process
    Usage: fg [jobid]
fg # it bring backs the background process in the foreground

 

 

  • jobs – displays the names and ids of background jobs
    Usage: jobs
jobs # display all the jobs

locate – find or locate a file
Usage: locate [OPTION]… FILE…
eg. locate file1.txt

locate test1.txt # find the file for you in your system.

 

  • sed ­  stream editor for filtering and transforming text
    Usage: sed [OPTION] [input­file]…
    eg. sed ‘s/love/hate/g’ loveletter.txt

 

sed 's/tutorial/workshop/g' test1.txt # replace all the occurence of tutorial with workshop. Very useful command

 

  • awk ­ pattern scanning and processing language
    eg.  awk ­F: ‘{ print $1 }’ sample_awk.txt
awk -F ' ' '{print $1}' test1.txt # print first column of your file separated with white space