Tuesday, July 30, 2019

Basic unix commands

Basic unix commands :-  
--------------------------

* Man command:
  It will gives DESCRIPTION and options how to use any command.
  eg:- man ls
         man cat

* Pwd : 
   It displays Present working directory or current directory.

* Ls Command :
  The 'ls' command is used to list files and directories.
  ls -a list all files including hidden file starting with '.'.
  ls -d list directories - with ' */'.
  ls -l list with long format - show permissions.
  ls -F Append indicator (one of */=>@|) to entries.
  ls -lh This command will show you the file sizes in human readable format.
  ls -r list in reverse order.
  ls -i list file's inode(index) number.
  ls -ltr View Reverse Output Order by Date.
  ls -t sort by time & date.
  ls -n It is used to print group ID and owner ID instead of their names.
  ls -m A list of entries separated by commas should fill the width.
  ls -g This allows you to exclude the owner and group information columns.
  ls -q Force printing of non-graphic characters in file names as the character `?';.
  ls -Q Place double quotations around the entry names.


Cd directory_name : To change the current directory to another directory.

Cd .. : It brings you out from your current directory but takes you only 1 directory ahead.
Cd .. /.. : It brings you out from your current directory but takes you 2 directories ahead.
Cd ~ : It is used to go back to your default directory or location.
Cd - : It is used to go back to your current directory or location.

Who : It displays how many users are currently logged in.

Who -q : It displays how number of users are currently logged in.
Whoami : It displays user who is logged in.

Cal :- It displays current month calender.

Cal -y :- It displays current year calender.

Date : It displays day, month, time with minutes and seconds along with year.


Cat : It is used to create, display and append the files.

Cat > filename : It is used to create a new file.
(Cat > filename)

Cat filename : It is used to display a file.
(Cat filename)

Cat -n filename : It is used to display lines along with line numbers.
(Cat -n filename)

Cat filename1 > filename2 : It is used to create file2 and inserts file1 data into it 
(Cat filename1 > filename2)

Cat >> filename : It is used to append or combine files.
(Cat >> filename
or
Cat file1 file2 >> file3
Appending data to the existing file)

Touch : It's a command used to create empty file.

Touch file1 file2 file3 : It create 3 empty files.
Touch -a : It is used to display last accessed time.
Touch -m : It is used to display last modified time.

Stat filename : It is used to display last accessed, modified and changed details of a file.


Mkdir directory_name : It is used to create directories.

Rmdir directory_name : It is used to remove only empty directories.
Rmdir directory_name1, directory_name2, directory_name3 : It is used to remove only all the 3 empty directories.
Rmdir -i directory_name : It is used to remove directories with interactive mode.
Rmdir -r directory_name : It is used to remove non empty directories.

Mv : It is used to move or rename the files.

Mv oldname newname : It is used to rename the file.
(Mv filename filename1)

Mv file1 /user/ file1 : It is used to move file1 from current directory to user directory file1.

Cp : It is used to copy the file. By default it supports only files not directories.

Cp shell perl : It is used to copy the file shell into the file perl. Perl contains all the contents in the shell file.
Cp -r direct1 direct2 :  It is used to copy directory1 to directory2.

Wc -l filename : It is used to print line count.
Wc -w filename : It is used to print word count. 


* Cut Command :
  Extracts parts of each line of a file. It cuts out a set of bytes or characters or fields from each row of the file based on the delimiter 
  Process the file vertically as result of which it produces a single or multiple coulums in the output.
  Acts like a filter that processes the file and extracts columns from it

  -b Bytes; selects only these bytes
  -c Characters; select ony these characters
  -d Delimiter; use the mentioned delimiter
  -f Fields; select only these fields
 
   Syntax1:- cut -d ',' -f1 file1;
   Syntax2:- cut -d ',' -f2 file1;
   Syntax3:- cut -b1-5 file1;
  
   Output1:
   apple
  
   Output2:
   ball
   
   Output3:
   apple,b


* Paste Command: 
  It is used to merge contents of two files vertically.
   
  -d To change the delimiter specified character instead of default delimiter tab
  -s Used to join the multiple lines of the file(in parallel) using the delimiters specified.
 
   Syntax1:- paste file1 file2
   Syntax2:- paste -d "|" file1 file2
   Syntax3:- paste -s file1 file2
    
   Output1:
 apple,ball,cat,dog,elephant,fish,goat,hen,icecream,jeep apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream
  
   Output2:
   apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream,jeep|apple,ball,cat,dog,elephant,fish,goat,hen,ice-  cream
  
   Output3:
   apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream,jeep
   apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream


* Comm Command: 
  It compares two sorted files line by line and produces three column output, when no option were passed to it. Column one contains lines unique to file1, column two contains lines unique to file2 and column three contains lines unique to both the files.

  -1 Do not display column 1 (Lines unique to first file)
  -2 Do not display column 2 (Lines unique to second file)
  -3 Do not display column 3 (Lines unique to both file)
  
  --check-order Verify files are in sorted order
  --nocheck-order Ignore even if files are not in sorted order

    Syntax1:- comm -1 file1 file2
    Syntax2:- comm -2 file1 file2  
    Syntax3:- comm -3 file1 file2
   
  Output1:
  apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream
  
  Output2:
  apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream,jeep
  
  Output3:
  apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream
  apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream,jeep


* Diff Command:  
  It compare files line by line. 
  This command is used to get uncommon records between both the files.
  eg:- diff file1 file2
  
  Syntax1: 
  diff file1 file2
  Syntax2:
  diff Proverbs.txt Proverbs2.txt
    
  Output1:
  < apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream,jeep
  ---
  > apple,ball,cat,dog,elephant,fish,goat,hen,ice-cream
  
  Output2:
  >  He who hesitates is lost
  >  Like father like son
  >  Fortune favours the brave


* Sort Command: 
  Sort rearranges the lines of a file such that lines appear in nummerical or alphabetical order. 
  If no options are given sorting is done as:
  Lines that egin with number will come before lines that begin with a letter
  Lines that begin with the lowercase letter will come before lines that begin with same letter in uppercase.  
  
  -t To specify the delimiter
  -k To specify starting and ending columns for sorting
  -r To sort in revers order
  -n To sort numerical data
  -u To remove duplicate lines
  -c To check the file is in sorted order or not (It will not sort file contents)
  
   Syntax1:- sort Proverbs.txt  
   Syntax2:- sort -r Proverbs.txt 
   Syntax3:- sort -u Proverbs.txt
   Syntax4:- sort -c Proverbs.txt
    
   Output1:
   Action speaks louder than words
   An apple a day keeps the doctor away
   An apple a day keeps the doctor away
   Better late than never
   Practise makes man perfect
   Sun sets in the west
   Sun rises in the east
 
   Output2:
   Sun rises in the east
   Sun sets in the west
   Practise makes man perfect
   Better late than never
   An apple a day keeps the doctor away
   An apple a day keeps the doctor away
   Action speaks louder than words
 
   Output3:
   Action speaks louder than words
   An apple a day keeps the doctor away
   Better late than never
   Practise makes man perfect
   Sun sets in the west
   Sun rises in the east
   
   Output4:-
   sort: Proverbs.txt:2: disorder:  Sun sets in the west


* Unique Command:
  It is used to eliminate or remove the repeated occurance of consecutive lines from the file. If same line is repeated more than once, but not consecutive, it willbe considered as two unique lines and hence not removed. If all the repeated lines of the file has to be removed, then the file has to be sorted before passing it to uniq command.

  -u Selecting non repeated lines 
  -d Selecting the duplicate lines
  -c    Counting the frequency of occurance.

   Syntax1:- uniq Proverbs.txt (one occurance of every records will appear here) 
   Syntax2:- uniq -u Proverbs.txt 
   Syntax3:- uniq -d Proverbs.txt
   Syntax4:- uniq -c Proverbs.txt
    
   Output1:
   Sun rises in the east
   Sun sets in the west
   An apple a day keeps the doctor away
   Practise makes man perfect
   Action speaks louder than words
   Better late than never
   
   Output2:
   Sun rises in the east
   Sun sets in the west
   Practise makes man perfect
   Action speaks louder than words
   Better late than never
 
   Output3:
   An apple a day keeps the doctor away
   
   Output4:-
   1 Sun rises in the east
   1  Sun sets in the west
   2  An apple a day keeps the doctor away
   1  Practise makes man perfect
   1  Action speaks louder than words
   1  Better late than never


* tr Command: Translating characters
  The tr command manipulates individual character in a line. It translates character by character.
  It translates each character of expression1 to its corresponding mapped character in expression2.
  It takes input only from standard input but not filename as an argument.

  -d deleting the characters
  -s compressing multiple consecutive characters (Squeeze Repeating Characters)
  -c complementing values of expression

   The following command is used to translate double quotes into single quotes.
Syntax1:- cat Proverbs.txt | tr -d apple
    Syntax2:- cat Proverbs.txt | tr [a-z] [A-Z] (It converts all the character to capital letters)
Syntax3:- echo 'TODAYYYY IIIS SOOO COOOLD ~' | tr -s 'A-Z' 'a-z'  

   The multiple consecutive characters 'T' in log file logs.txt can be squeezed to one character using option -s of tr command as shown below.
   tr -s 'T' < logs.txt
   
   Output1:
   Sun riss in th st
   Sun sts in th wst
   An   dy ks th doctor wy
   An   dy ks th doctor wy
   Prctis mks mn rfct
   Action sks oudr thn words
   Bttr t thn nvr
 
   Output2: 
   SUN RISES IN THE EAST
   SUN SETS IN THE WEST
   AN APPLE A DAY KEEPS THE DOCTOR AWAY
   AN APPLE A DAY KEEPS THE DOCTOR AWAY
   PRACTISE MAKES MAN PERFECT
   ACTION SPEAKS LOUDER THAN WORDS
   BETTER LATE THAN NEVER
   
   Output3:
   today is so cold 


* Rev Command : reversing the given text/line
  It is used to reverse the text/line character wise. It reverses the order of the characters in each line and prints to the standard output.
  
  Syntax1:- cat file1 | rev
  Syntax2:- rev Proverbs.txt
  
  Output1:- peej,maerc-eci,neh,taog,hsif,tnahpele,god,tac,llab,elppa
  
  Output2:
  tsae eht ni sesir nuS
  tsew eht ni stes nuS
  yawa rotcod eht speek yad a elppa nA
  yawa rotcod eht speek yad a elppa nA
  tcefrep nam sekam esitcarP
  sdrow naht reduol skaeps noitcA
  reven naht etal retteB 


* Grep Command :   
  The content of the file is processed line by line, to search for the pattern and the lines matching the pattern are displayed on the terminal.
  It is used to search for lines of text that match one or many regular expressions.
  
  -i
  -V
  -n
  -x
  -c
  -v 
  -l
  -f
  -E
  -P
  -A
  -B 
  -C
  
  Syntax:- grep apple Proverbs.txt
  
  Output:-
  An apple a day keeps the doctor away
  An apple a day keeps the doctor away


* FIND Command : 
  To find a file in a directory.
  
  Syntax:- find -name s1.sh
  

* Sed Command :
  It is used to replace any character in a file.
  
  Syntax:- sed -i 's/ /,/g' filename.txt
           sed -i 's/,/ /g' Proverbs.txt

  Without using '-i' in sed command, file will not change it's delimiter or character. Just result will be displayed with 2nd character. 























 

No comments:

Post a Comment

SUBQUERIES PRACTISE QUESTIONS

1. Write a SQL query to find those employees who receive a higher salary than the employee with ID 7369. SELECT * FROM EMP WHERE SAL >  (...