Thursday 8 August 2013

Linux Basics


These commands need to be arranged in term of their usability

1) A function letter does not need to be prefixed with a dash ("-"), and may be combined with other single-letter options.
2) A long function name must be prefixed with a double dash ("--").
3) Some options take a parameter; with the single-letter form these must be given as separate arguments.
 With the long form, they may be given by appending "=value" to the option.

Tar ( Tape archive)

1) A function letter does not need to be prefixed with a dash ("-"), and may be combined with other single-letter options.
2) A long function name must be prefixed with a double dash ("--").
3) Some options take a parameter; with the single-letter form these must be given as separate arguments.
 With the long form, they may be given by appending "=value" to the option.

All 4 below mean the same thing

a) tar --create --file=archive.tar file1 file2
Note -- c for create , f for file
b) tar -c -f archive.tar file1 file2
c) tar -cf archive.tar file1 file2
d) tar cf archive.tar file1 file2

Create archive archive.tar containing files file1 and file2. Here, the c tells tar you will be creating an archive; the f tells tar that the next option (here it's archive.tar) will be the name of the archive it creates. file1 and file2, the final arguments, are the files to be archived

Other useful addition to tar
A - Append ( you can add a file to already present archive file)
t- List contents (Notice T as t of LisT)
d- difference
v-Verbose
X-Extract
u- update (update a already present file in archive if its old copy)
z-zipped
c-Compressed

tar -tvf archive.tar--List the files in the archive archive.tar verbosely
tar -xf archive.tar -- Extract files from archive
tar -xzvf archive.tar.gz -- Extract files from zipped archive

Gzip (GNU zip) and Gunzip (GNU unzip)

Gzip is a compression command and is used to compress(reduce the size of file).So running gzip on 3 files will not combine them into one file.
In order to compress a folder, we need to first use tar and then zip with gzip
Example -
gzip file1 file2 file3 ... this will produce 3 files ,file1.gz, file2.gz and file3.gz with .gz extension.

Compressing a folder
tar cf – test/ | gzip > test.tar.gz

Some File manupulating commands

awk 'BEGIN {start_action} {action} END {stop_action}' filename

Note
1) $1 will print the first column
2) $0 will print the entire line
3) FS field separator
4) OFS - Output field separator variable
5) NF - Number of fileds variable
6) NR - Number of lines


awk '{print $1}' input_file -- Prints first column
awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}' input_file --Sum of values in 5th block
awk '{ if($9 == "t4") print $0;}' input_file ----notice the semicolon
awk 'BEGIN {FS=":"} {print $2}' input_file
awk 'BEGIN {OFS=":"} {print $4,$5}' input_file
awk '{print NF}' input_file  - tells us number of fields
awk '{print NR}' input_file -- tells us number of records

Cut Command

Cut command in unix (or linux) is used to select sections of text from each line of files. It can be used as substring command by specifying the start and end position to cut. Also it can be used similar to awk command by specifying the delimiter.
Note
d is used for delimeter
f is used for field posistion
c - Cut

cut -c4 file.txt
cut -c4,6 file.txt -- this means only 4th and 6th character. It will apply to all rows
cut -c4-7 file.txt--- Start position and End position.
cut -c10- file.txt ---Start position and no end position
cut -d' ' -f2 file.txt -- Notice no c here, We are picking a field

Important point to remember. Command always use delimeter to find end of a column
Example

logfile.dat
sum.pl
add_int.sh

 cut -d'.' -f1
--- this will give us logfile and others ( sum, add_int)

If we need to find the value after dot. We need to reverse the string and then cut

Reverse

echo "nixcraft" | rev

This will output tfarcxin

Head command in Linux


































2 comments: