2014年3月19日星期三

Linux Commands for Beginners lesson 19 -- expr,let commands

In this part,I will show you how to do some arithmetic calculation in Bash.

1.COMMAND:
      expr
  DESCRIPTION:
       Print  the  value of EXPRESSION to standard output.
  SYNOPSIS:
       expr EXPRESSION
       
Example:
root@piniheaven:~# expr 1 + 1
2
root@piniheaven:~# expr 2 - 1
1
root@piniheaven:~# expr 4 / 2
2
root@piniheaven:~# expr 2 \* 2  #Tip: when you want to do some multiplication,you should use backslash
4



2.COMMAND:
     let
 DESCRIPTION:
     carry out arithmetic operations on variables. 

Example:
root@piniheaven:~# let " val=(1+1)*2"  # will compute 2 times 2 and assign the result to the variable "val".
root@piniheaven:~# echo $val    #print variable val
4

Linux Commands for Beginners lesson 18 -- Finding Files

In this part, I demonstrate how to find files.

1.COMMAND:
      find
DESCRIPTION:
   search for files in a directory hierarchy

SYNOPSIS:
       find <path> <fileName>
OPTION:
     -mtime n   File's data was last modified n*24 hours ago.
     -name      Base of  file  name  

Example:
If you remember the file's name,you can specify a path to find it out.
root@piniheaven:~# find /root/ -name find.txt
/root/tutorial/find.txt


Even you can't remember what the exactly file name is,you still can find it out.
root@piniheaven:~# find /root/ -name fin*
/root/tutorial/find.txt



Find out files that was last modified 24 hours ago.
root@piniheaven:~# find /root/tutorial/ -mtime  -1
/root/tutorial/
/root/tutorial/.find.txt.swp
/root/tutorial/poem.txt
/root/tutorial/find.txt

Linux Commands for Beginners lesson 17 -- Head and Tail Command

In this part, I go over both the head and tail commands.

1.COMMAND:
     head
DESCRIPTION:
     Print  the  first  10 lines of  FILE to standard output. 
SYNOPSIS:
       head [OPTION]... [FILE]...
OPTION:
     -n, --lines
              print  the first n lines instead of the first 10;


2.COMMAND:
    tail
DESCRIPTION:
    Print the last 10 lines of FILE to standard output.
SYNOPSIS:
    tail [OPTION]... [FILE]...
OPTION:
     -n, --lines      output the last K lines, instead of the last 10;


Examples:
root@piniheaven:~/tutorial# ls
poem.txt
root@piniheaven:~/tutorial# head poem.txt   # Print  the  first  10 lines of  poem.txt to standard output.
nt you to know
one thing.

You know how this is:
if I look
at the crystal moon, at the red branch
of the slow autumn at my window,
if I touch
near the fire
the impalpable ash
root@piniheaven:~/tutorial# head -4 poem.txt   # Print  the  first 4 lines of poem.txt to standard output.
nt you to know
one thing.

You know how this is:
root@piniheaven:~/tutorial# tail poem.txt    # Print  the last  10 lines of poem.txt to standard output.
you feel that you are destined for me
with implacable sweetness,
if each day a flower
climbs up to your lips to seek me,
ah my love, ah my own,
in me all that fire is repeated,
in me nothing is extinguished or forgotten,
my love feeds on your love, beloved,
and as long as you live it will be in your arms
without leaving mine
root@piniheaven:~/tutorial# tail -3 poem.txt    # Print  the last 3 lines of poem.txt to standard output.
my love feeds on your love, beloved,
and as long as you live it will be in your arms
without leaving mine

Linux Commands for Beginners lesson 16 -- The Watch Command

In this part, I talk about the watch command.

1.COMMAND :
      watch
  DESCRIPTION :
      execute a program periodically, showing output fullscreen
  SYNOPSIS :
       watch [option] <command>
   OPTION :
       -n :  By default, the program is run every 2 seconds; use -n  to specify a different interval.
   command: any linux command you want to type
Example :
[piniheaven@localhost ~]$ watch -n 10 free -m  #Every 10.0s free -mwill be executed automatically
Every 10.0s: free -m                                    Tue Sep  3 17:48:56 2013


             total       used       free     shared    buffers     cached
Mem:          5690       1388       4301          0         82        620
-/+ buffers/cache:        685       5004
Swap:            0          0          0

Linux Commands for Beginners lesson 15 -- du,df,free commands

 In this part, I talk about viewing system resources.


1.COMMAND :
       df 
DESCRIPTON :
       report file system disk space usage
SYNOPSIS :
       df [OPTION]... [FILE]...
OPTION :
      -h, --human-readable
Example :
[piniheaven@localhost ~]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1               15G  5.4G  9.0G  38% /
tmpfs                      2.8G  884K  2.8G   1% /dev/shm




2.COMMAND :
     du
DESCRIPTION :
     estimate file space
SYNOPSIS :
     du [OPTION]... [FILE]...
OPTION :
     -s, --summarize
              display only a total for each argument
     -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
Example :
[piniheaven@localhost ~]$ ls
Desktop            Documents  Music     Public        SolftWare  tutorial
distribute-0.6.49  Downloads  Pictures  Python-2.7.3  Templates  Videos
[piniheaven@localhost ~]$ du -sh Videos
254MVideos




3.COMMAND :
      free
DESCRIPTION :
      Display amount of free and used memory in the system
SYNOPSIS :
       free [-m]
OPTION :
      -m switch displays it in megabytmegabytes.
Example :
[piniheaven@localhost ~]$ free-m
             total       used       free     shared    buffers     cached
Mem:          5690       1736       3953          0         74        615
-/+ buffers/cache:       1046       4644
Swap:            0          0          0
Explanation:
Actual Used Physical Memory (Memory Used by Apps) :1046 MB
Actual Free Physical Memory (Memory Available For Apps) :4644 MB


If you want to know more about what is the difference between buffer and cache memory,you can click following link.

Linux Commands for Beginners lesson 14 -- the grep command

In this part,I wil show you the basis of  Regular Expressions and the grep command


1.Command:
     grep
  DESCRIPTION:
     print lines matching a pattern

  SYNOPSIS:
     grep [OPTIONS] PATTERN [FILE...]
  OPTION:
    -n, --line-number         print line number with output lines


Regular expressions made up of anchors,character sets,modifiers
Anchors:specify the position
^ :at the beginnig of a line
$ :at the end of a line
Tips:If ^ is not placed at the beginning ,or $ at the end,the two won't act as anchors anymore

character sets:what is seached


Example:
root@piniheaven:~/tutorial# ls
A Girl.txt
root@piniheaven:~/tutorial# cat -n A\ Girl.txt
     1    the tree has entered my hands
     2    the sap has ascended my arms
     3    the tree has grown in my breast-
     4    downward
     5    the branches grow out of me, like arms
     6  
     7    tree you are
     8    moss you are
     9    you are violets with wind above them
    10    a child - so high - you are
    11    and all this is folly to the world


print lines contain words "tree"   
root@piniheaven:~/tutorial# grep 'tree' A\ Girl.txt
the tree has entered my hands
the tree has grown in my breast-
tree you are

print lines contain word "tree" and print which line they are in
root@piniheaven:~/tutorial# grep -n 'tree' A\ Girl.txt
1:the tree has entered my hands
3:the tree has grown in my breast-
7:tree you are


print lines start with word "tree" and print which line they are in
root@piniheaven:~/tutorial# grep -n ^'tree' A\ Girl.txt
7:tree you are


print lines contain word "are" and print which line they are in
root@piniheaven:~/tutorial# grep -n 'are' A\ Girl.txt
7:tree you are
8:moss you are
9:you are violets with wind above them
10:a child - so high - you are



print lines end with word "are" and print which line they are in
root@piniheaven:~/tutorial# grep -n 'are'$ A\ Girl.txt
7:tree you are
8:moss you are
10:a child - so high - you are



Matching Character Sets
--"abc" finds lines with "abc" in them
--match any character with "."(dot)
--specify a range with []
  [123] - lines that contain 1,2 or 3
  [0-9] - lines that contain at least a number
  [A-Za-z0-9] - lines that contain at leas a letters or a numbers


Example:
Find words start with letter 'm' and follow with a any character
root@piniheaven:~/tutorial# grep -n 'm.' A\ Girl.txt
1:the tree has entered my hands
2:the sap has ascended my arms
3:the tree has grown in my breast-
5:the branches grow out of me, like arms
8:moss you are

Find words start with letter 'a' or 'b'
root@piniheaven:~/tutorial# grep -n ^'[ad]' A\ Girl.txt
4:downward
10:a child - so high - you are
11:and all this is folly to the world


print lines start with a letter range from 'a' to 'm'
root@piniheaven:~/tutorial# grep -n ^'[a-m]' A\ Girl.txt
4:downward
8:moss you are
10:a child - so high - you are
11:and all this is folly to the world

Tips:
If you want to search for "[" or "]",you should use backslash "\
"
Example:
[\]] - search lines with "]" in them



modifiers:specify how many times the previous charater set is repeated
* - matches zero or more copies of the chars
[]\{1,4\} - lines that have between 1 to 4 mathes
\<word\> - searches for a specific and separate word

Example:

root@piniheaven:~/tutorial# ls
A Girl.txt  essay.txt
root@piniheaven:~/tutorial# cat -n essay.txt
     1    a
     2    aa
     3    aaa
     4    aaaa
     5    aaaaa
     6    aaaaaa
     7  
     8    abc
     9    abbc
    10    abcc

print lines contain word that mach [b-c][b-c]
root@piniheaven:~/tutorial# grep -n '[b-c]\{2,3\}' essay.txt
8:abc
9:abbc
10:abcc


If you want to just search specify word,following command failed to work.
root@piniheaven:~/tutorial# grep -n 'aa' essay.txt
2:aa
3:aaa
4:aaaa
5:aaaaa
6:aaaaaa

therefore,you can type command like this
root@piniheaven:~/tutorial# grep -n '\<aa\>' essay.txt
2:aa

Linux Commands for Beginners lesson 13-- which and whatis commands

In this tutorial, I talk about the which and whatis commands.
1.COMMAND:
       which
  DESCRIPTION:
      locate a command


SYNOPSIS
       which  commandName


Example:
piniheaven@fish:~$ which ls
/bin/ls
piniheaven@fish:~$ which nano
/usr/bin/nano


2.COMMAND:
     whatis 
  DESCRIPTION:
     display manual page descriptions


SYNOPSIS
       whatis  commandName


Example:
piniheaven@fish:~$ whatis firefox
firefox (1)          - a free and open source web browser from Mozilla
piniheaven@fish:~$ whatis ls
ls (1)               - list directory contents