Tuesday, 20 December 2016

Basic Unix Commands


                                                          Basic Unix Commands

       Hi all,
                
               This post will be useful to the beginners..

List (ls):
                To list the files present under the directory
% ls
To view the hidden files
% ls –a
To view the file with its size
% ls –l


Check what happens with  % ls –al  ????

      To know more about ls command please click here

Directories:
To change the directory
% cd directory_name

To go to the parent directory

% cd ..

Path Name: (pwd)
To know the current path on which you are working
% pwd

Concatenate (cat):
                The command cat can be used to display the contents of a file on the screen.
% cat filename

Less:
The command less writes (view) the contents of a file onto the screen a page at a time.
% less filename

gzless
    Gzless command is used to view the .gz files
% gzless filename.gz

Head:
The head command writes the first ten lines of a file to the screen.
% head filename
              
       To view the first 5 lines of the file
% head -5 filename


Tail:
The tail command writes the last ten lines of a file to the screen.

% tail filename

To view the last 5 lines of the file
% tail -5 filename

grep:
                grep stands for "Global regular expression print" it is used search the keyword in a file

% grep keyword filename
               
 If you not sure about your keyword (case sensitive)
% grep -i keyword filename                             (-i is for case sensitive it will consider both a and A)
                

To search a sentence or more than two keywords
% grep -i ‘keyword  keyword’ filename                  (we can use either single quote or double)


Some of the other options of grep are:
-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines

gzgrep
        To reduce the file size files are compressed to gz file(hello.txt.gz). gzgrep command is used to check for search keywords in gz files

% gzgrep keyword filename.gz

Word count:
                
           To know the number of words present in the file
% wc –w file name
                
          To know the number of lines present in the file
% wc –l file name

Sort:
                To sort the content in the file
% sort file name               ascending order
% sort –r filename           descending order
% sort –u filename          To remove repeated content

Pipes:
                With the help of pipe symbol we can combined perform commands
% ls | tail
% ls | sort

Example:
grep apple fruit.txt | grep grape| grep banana

History

To view the commands entered by you (user)

% history

Recall commands

% !! (recall last command)
% !-3 (recall third most recent command)
% !5 (recall 5th command in list)
% !grep (recall last command starting with grep)

Help
         To know the complete description of any command ant its syntax use man command. For example, to find out more about the wc (word count) command, type

% man wc

Alternatively,

% whatis wc         gives a one-line description of the command

Apropos
When you are not sure of the exact name of a command,
% apropos keyword

No comments:

Post a Comment