Tuesday, November 16, 2010

Linux magical commands

Linux command like a magic stick in the hand of wizard, more you practice more you find scope of learning. I am playing with it from few years and still I find myself n00b in this context, although today I planed to share my n00b experience with you regards to few basic Linux commands . This POST is totally belongs to the beginners.

cat :
concatenate files and print on the standard output
$> cat file.txt gt; cat file.txt ##print the content of the file on terminal
$> cat -n file.txt gt; cat -n file.txt ##print the content of file with line number


grep :
grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
$>grep apple fruitlist.txt ##grep prints all lines containing apple
$>
grep -w apple fruitlist.txt ##grep prints all lines containing apple as a word
You can also use grep with regular expressions

sed
is a stream editor. A stream editor is used to perform basic text transformations on an input stream. While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

$>sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName$>
##s Substitute command
##g global
##/../../ Delimiterday
##(oldstuff)Regular Expression Pattern Search Pattern
##(newstuff)night Replacement string


awk :
An AWK program is a sequence of pattern {action} pairs and function definitions. Short programs are entered on the command line usually enclosed in ' ' to avoid shell interpretation. Longer programs can be read in from a file with the -f option. Data input is read from the list of files on the command line or from standard input when the list is empty.
$>awk [ -F ] {pgm} | { -f } [ ] [ - | ]
##ch: Field-separator character.
##pgm: Awk command-line program
##pgm file: File containing an Awk program.
##vars: Awk variable initializations.
##data file: Input data file.


tr :
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
$>
tr 'abcd' 'jkmn' ##maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'.
$>cat file.txt | tr -d '\n' ##delete new line character from file

curl :
Curl is a computer software project providing a library and command-line tool for transferring data using various Protocols.
$>curl -o example.html www.example.com ##cURL can write the output it retrieves to a file with the -o flag
$>curl www.example.com