ult/handout.rst
author Puneeth Chaganti <punchagan@fossee.in>
Mon, 31 Jan 2011 12:24:43 +0530
changeset 149 4499aebbee83
parent 132 24cec0337e81
permissions -rw-r--r--
vcs: Fix pygments highlighting of code blocks with $ and ' Pygments highlighting breaks when a code block ends with a lone $ on a line or when it has an unmatched '.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
     1
Introducing Linux
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
     2
=================
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
     3
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
     4
GNU/Linux is an  operating system that uses the Linux Kernel. It is similar to the Unix operating system. It is an open source operating system which basically means you can view and change the code.  
25
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
     5
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
     6
The Linux Kernel written by Linus Torvalds in 1991. Although written only for x86 architecture , it was ported to many other architectures.The whole operating system contains the kernel and several other system and application software contributed by many different projects. A major contributor has been the GNU project. GNU project was started by Richard Stallman in 1983. Richard Stallman wrote the GNU General Public License which gave the first impetus to the free software movement leading up do development of the family of Linux operating systems that we see today . 
25
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
     7
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
     8
Design and Implications
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
     9
------------------------
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    10
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    11
Linux is a modular operating system, deriving much of its basic design from principles established in Unix earlier. The kernel  manages the systems resources like process control, networking, peripherals and file system access. Application Software written on top of it gives higher level functionality. 
25
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    12
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    13
Reasons for Using Linux
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    14
-----------------------
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    15
- Linux is free:
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    16
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
    17
As in "free beer". Linux can be downloaded in its entirety from the Internet completely for free. No registration fees, no costs per user, free updates, and freely available source code in case you want to change the behavior of your system.
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    18
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    19
- Linux can be deployed easily on clusters for parallel and distributed computing 
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    20
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    21
There are many distributions of Linux meant for clusters. One of the popular ones is Rocks Cluster Distribution.   
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    22
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    23
- Linux was made to keep on running:
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    24
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
    25
As with UNIX, a Linux system expects to run without rebooting all the time. That is why a lot of tasks are being executed at night or scheduled automatically for other times, resulting in higher availability during busier periods and a more balanced use of the hardware. This property allows for Linux to be applicable to environments where people do not have the time or the possibility to control their systems constantly.
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    26
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    27
- Linux is secure and versatile:
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    28
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    29
The security model used in Linux is based on the UNIX idea of security, which is known to be robust and of proven quality. 
25
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    30
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    31
- Linux contains a tools for scientific computing
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    32
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    33
Linux contains many tools like latex for reading and writing scientific text. It also contains many softwares like scilab , python and fortran used for scientific computing needs. 
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    34
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    35
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    36
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    37
Getting Started
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    38
================
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    39
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    40
Logging in, activating the user interface and logging out
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    41
----------------------------------------------------------
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    42
Linux supports multiple users on a machine. Each user must log in with his or her username and password.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    43
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    44
In order to work on a Linux system directly, one needs to provide a username and password. You always need to authenticate to the system. After booting , you will see a login screen/prompt asking for username and password , enter the username and password , if it is correct you will be logged in . One can logout by typing logout on the prompt or navigating to logout button if using Graphical User Interface . 
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    45
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    46
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    47
When you see the login screen again, asking to enter username and password, logout was successful.
27
fe0672fd1be4 Introduction, midway session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents: 25
diff changeset
    48
25
4df1ca9766b8 Introduction, added session 1.
Kadambari Devarajan <kadambarid@fossee.in>
parents:
diff changeset
    49
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    50
Basic Commands
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    51
===============
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    52
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    53
What files do I have on my computer?
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    54
-------------------------------------
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    55
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    56
All content in Linux  is kept on data structure called files.We can list those files to know what all is there.
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
    57
*ls* lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there.::
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    58
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    59
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    60
	$ ls
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    61
	jeeves.rst psmith.html blandings.html Music
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
    62
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    63
How do I move around the file system?
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    64
-------------------------------------
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    65
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
    66
This stands for "change directory". When one wants to change the directory .
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
    67
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
    68
       $cd Music 
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    69
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    70
One dot '.' represents the current directory while two dots '..' represent the parent directory.
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    71
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    72
“ cd -” will return you to the previous directory.
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    73
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    74
You can also use cd [absolute path] or cd [relative path] (see below):
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    75
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
    76
Absolute paths:
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    77
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    78
Absolute Path is the path of the directory from root i.e / . / is the top most level in file system.
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    79
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    80
For example to get to /var/www you would type::
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    81
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    82
	$cd /var/www
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    83
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    84
This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command).
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    85
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
    86
Relative paths:
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    87
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    88
   Releative Path is path in relation to your current location . 
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    89
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    90
    For example if you are in Music directory and want to get to Rock directory inside Music, you type::
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    91
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    92
	Music$ cd Rock
44
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    93
6a9f6526b4f4 Added few basic commands
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 27
diff changeset
    94
Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
    95
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    96
Linux is multiuser system so *who* all are using my system now?
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    97
--------------------------------------------------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
    98
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
    99
The standard Unix command *who* displays a list of users who are currently logged into a computer.::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   100
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   101
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   102
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   103
	$who
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   104
	user       tty7         2009-09-08 10:50 (:0)
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   105
	harry      pts/0        2009-09-08 11:25 (:0.0)
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   106
	dumbledore pts/1        2009-09-08 18:11 (potter.xyz.in)
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   107
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   108
The columns represent user, current terminal , date and time of login and the host from which he is logged in respectively. 
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   109
The command can be invoked with the arguments *am i* or *am I* (so it is invoked as *who am i* or * who am I*), showing information about the current terminal only (see the *-m* option below, of which this invocation is equivalent).
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   110
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   111
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   112
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   113
How do I organize my files?
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   114
---------------------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   115
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   116
This command is used to make a new directory. Normal usage is as straightforward as follows::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   117
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   118
	$mkdir name_of_directory
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   119
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   120
Where *name_of_directory* is the name of the directory one wants to create. When typed as above (ie. normal usage), the new directory would be created within the current directory. On Unix, multiple directories can be specified, and *mkdir* will try to create all of them.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   121
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   122
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   123
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   124
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   125
Where am I now on the filesystem?
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   126
--------------
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   127
pwd is a Linux / Unix command which prints the current working directory. If you wish to know the full path of the  directory in which you are in from the Linux console, then the pwd command will come to your rescue. pwd stands for Print Working Directory.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   128
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   129
Usage of pwd command::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   130
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   131
      $ cd Examples
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   132
      $ pwd
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   133
      /home/user/Examples
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   134
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   135
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   136
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   137
 I wish some commads were a bit smarter ? 
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   138
-----------------------------------------
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   139
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   140
The standard commands in Linux have a lot of options also called flags to change or provide some additional functionality to the command For example ::
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   141
      
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   142
       $ls -l 
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   143
       
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   144
       * *ls with flag -l* displays the result in long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   145
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   146
       $ls ­a 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   147
       * *ls with flag -a*  lists all files including hidden files
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   148
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   149
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   150
Similarly, mkdir with -p option automatically creates parent directory even if it does not exist.::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   151
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   152
	   $mkdir -p this/path/never/existed/earlier/
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   153
	    
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   154
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   155
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   156
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   157
Getting Help
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   158
============
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   159
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   160
How do I find what a command does?
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   161
----------------------------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   162
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   163
A short index of explanations for commands is available using the *whatis* command, like in the examples below::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   164
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   165
	$whatis ls
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   166
	ls (1) 		 - list directory contents
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   167
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   168
This displays short information about a command, and the first section in the collection of man pages that contains an appropriate page.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   169
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   170
More extensive Documentation
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   171
----------------------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   172
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   173
Man pages (short for "manual pages") are the extensive documentation that comes preinstalled with almost all substantial Unix and Unix-like operating systems. The Unix command used to display them is *man*. Each page is a self-contained document.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   174
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   175
To read a manual page for a Unix command, one can use::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   176
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   177
	$ man <command_name>
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   178
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   179
To see the manual on man itself do::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   180
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   181
	$man man
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   182
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
   183
The previous example will take you to the "Manual" page entry about manual pages!
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   184
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   185
Looking at man pages is a very good way to actually check flags and other help related to a command. 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   186
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   187
--help
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   188
-------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   189
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   190
Most GNU commands support the --help, which gives a short explanation about how to use the command and a list of available options. Below is the output of this option with the *mkdir* command::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   191
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   192
	$ mkdir --help
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   193
	
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   194
	Usage: mkdir [OPTION]... DIRECTORY...
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   195
        Create the DIRECTORY(ies), if they do not already exist.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   196
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   197
        Mandatory arguments to long options are mandatory for short options too.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   198
          -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   199
          -p, --parents     no error if existing, make parent directories as needed
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   200
          -v, --verbose     print a message for each created directory
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   201
          -Z, --context=CTX  set the SELinux security context of each created
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   202
                            directory to CTX
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   203
          --help     display this help and exit
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   204
          --version  output version information and exit
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   205
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   206
        Report mkdir bugs to bug-coreutils@gnu.org
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   207
        GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   208
        General help using GNU software: <http://www.gnu.org/gethelp/>
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   209
        Report mkdir translation bugs to <http://translationproject.org/team/>
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   210
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   211
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   212
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   213
Working with Files
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   214
===================
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   215
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   216
Copying Files
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   217
-------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   218
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   219
*cp* is the command to copy a file from one place to another including different file system(#change? ellaborate). The original file remains unchanged, and the new file may have the same or a different name.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   220
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   221
Usage
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   222
~~~~~
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   223
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   224
To copy a file to another file ::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   225
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   226
	$ cp  SourceFile TargetFile
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   227
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   228
To copy a file to a directory::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   229
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   230
	$ cp  SourceFile  TargetDirectory
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   231
 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   232
To copy a directory to a directory::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   233
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   234
	$ cp  -r SourceDirectory  TargetDirectory
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   235
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   236
In case target Directory has a file of the same name::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   237
   	
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   238
	TargetDirectory$ls
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   239
	jeeves.rst psmith.html
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   240
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   241
	SourceDirectory$ls
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   242
	jeeves.rst index.html
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   243
	
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   244
	$cp -i 	jeeves.rst TargetDirectory/
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   245
	cp: overwrite 'TargetDirectory/jeeves.rst'? 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   246
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   247
-i option is for interactive usage.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   248
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   249
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   250
Flags
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   251
~~~~~
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   252
*-i* (interactive) – prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   253
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   254
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   255
*-r* (recursive) – copy directories (recursively copying all the contents)
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   256
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   257
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   258
Moving Files
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   259
------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   260
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   261
*mv* (short for move) is a Unix command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name.An interesting usage of mv is actualy to rename it by moving it in same directory under a different name. 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   262
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   263
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   264
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   265
Usage
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   266
~~~~~~~~
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   267
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   268
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   269
To rename a file ::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   270
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   271
   $ mv myfile mynewfilename  
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   272
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   273
To move to a different directory ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   274
   $ mv myfile otherdir/     
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   275
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   276
To move a directory ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   277
   
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   278
   $mv mydir otherdir
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   279
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   280
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   281
Using -i to avoid overwrite(just like cp)::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   282
   
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   283
   $mv -i mydir otherdir
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   284
   mv: overwrite `otherdir/mydir'?
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   285
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   286
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   287
Removing files
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   288
--------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   289
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   290
*rm*  is used to delete files from a filesystem. 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   291
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   292
Here's example to remove a file named "foo" from a directory, here shown with the -i option::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   293
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   294
  	$ rm -i foo
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   295
    	remove foo? y
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   296
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   297
Options
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   298
~~~~~~~
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   299
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   300
Common options that rm accepts include:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   301
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   302
    * *-r*, which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive")
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   303
    * *-i*, which asks for every deletion to be confirmed ("interactive")
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   304
    * *-f*, which ignores non-existent files and overrides any confirmation prompts ("force")
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   305
    * *-v*, which shows what is being removed as it happens ("verbose")
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   306
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   307
*rm* is often aliased to "rm -i" so as to avoid accidental deletion of files. 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   308
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   309
*rm -rf* (variously, rm -rf /, rm -rf `*`, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   310
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   311
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   312
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   313
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   314
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   315
Permissions
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   316
~~~~~~~~~~~
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   317
Linux is a proper multi-user environment. In a multi-user environment, security of user and system data is very important. Access should be given only to users who need to access the data. Since Linux is essentially a server OS, good and efficient file security is built right . The permissions are based on whether one is allowed to read, write or execute a file.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   318
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   319
Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.)
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   320
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
   321
To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any).
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   322
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   323
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   324
Is there a default organization system of files and Directories that Linux follows
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   325
==================================================================================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   326
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   327
In the File Hierarchy Standard (FHS) all files and directories appear under the root directory "/", even if they are stored on different physical devices. Note however that some of these directories may or may not be present on a Unix system depending on whether certain subsystems, such as the X Window System, are installed.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   328
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   329
The majority of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other thanmajor Linux distros.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   330
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   331
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   332
|   Directory   |             Description                        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   333
+===============+================================================+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   334
| /             | Primary hierarchy root and root directory of   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   335
|               | the entire file system hierarchy.              |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   336
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   337
| /bin/         | Essential command binaries that need to be     |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   338
|               | available in single user mode; for all users,  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   339
|               | e.g., *cat*, *ls*, *cp*.                       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   340
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   341
| /boot/        | Boot loader files, e.g., *kernels*, *initrd*;  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   342
|               | often a separate partition.                    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   343
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   344
| /dev/         | Essential devices, e.g., /dev/null             |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   345
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   346
| /etc/         | Host-specific system-wide configuration files  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   347
|               | (the name comes from *et cetera*)              |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   348
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   349
| /home/        | User's home directories, containing saved      |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   350
|               | files, personal settings, etc.; often a        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   351
|               | separate partition.                            |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   352
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   353
| /lib/         | Libraries essential for the binaries in        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   354
|               | */bin/* and */sbin/*                           |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   355
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   356
| /media/       | Mount points for removable media such as       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   357
|               | CD-ROMs, external hard disks, USB sticks, etc. |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   358
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   359
| /mnt/         | Temporarily mounted file systems               |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   360
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   361
| /opt/         | Optional application software packages         |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   362
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   363
| /proc/        | Virtual filesystem documenting kernel and      |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   364
|               | process status as text files; e.g., uptime,    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   365
|               | network. In Linux, corresponds to a *Procfs*   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   366
|               | mount.                                         |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   367
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   368
| /root/        | Home directory for the root user               |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   369
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   370
| /sbin/        | Essential system binaries; e.g., *init*,       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   371
|               | *route*, *mount*.                              |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   372
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   373
| /srv/         | Site-specific data which is served by the      |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   374
|               | system.                                        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   375
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   376
| /tmp/         | Temporary files. Often not preserved between   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   377
|               | system reboots.                                |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   378
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   379
| /usr/         | Secondary hierarchy for read-only user data;   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   380
|               | contains the majority of (multi-)user          |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   381
|               | utilities and applications.                    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   382
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   383
| /var/         | Variable files - files whose content is        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   384
|               | expected to continually change during normal   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   385
|               | operation of the system - such as logs, spool  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   386
|               | files, and temporary e-mail files.             |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   387
|               | Sometimes a separate partition.                |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   388
+---------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   389
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   390
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   391
man hier
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   392
---------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   393
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   394
This is the manual page on the UNIX filesystem. The syntax for this is::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   395
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   396
	$ man hier
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   397
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   398
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   399
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   400
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   401
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   402
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   403
Permissions and Ownership
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   404
=========================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   405
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   406
drwxr-x--- 2 user group 4096 Dec 28 04:09 tmp
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   407
-rw-r--r-- 1 user group 969 Dec 21 02:32 foo
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   408
-rwxr-xr-x 1 user group 345 Sep 1 04:12 somefile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   409
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   410
d - The file is a directory.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   411
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   412
r - Read permission. The file may be read. In the case of a directory, this would mean the ability to list the contents of the directory.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   413
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   414
w - Write permission.For a directory, this defines whether you can make any changes to the contents
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   415
of the directory. If write permission is not set then you will not be able to delete, rename or create a file.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   416
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   417
x - Execute permission. Whether the file may be executed. In the case of a directory, this attribute decides whether you have permission to enter,run a search through that directory or execute some program from that directory.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   418
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   419
The Permission are read in the following manner::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   420
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   421
    | User | Group | Others |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   422
    |------+-------+--------|
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   423
    | drwx | rwx   | rwx    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   424
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   425
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   426
Problem: Given a file with only read permission, add execute permission to User, Group and Others.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   427
--------------------------------------------------------------------------------------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   428
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   429
Solution::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   430
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   431
	$chmod a+x executablefile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   432
   	$ls -l executablefile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   433
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   434
Thats it .
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   435
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   436
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   437
chmod
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   438
------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   439
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   440
The *chmod* command changes the  file system modes of files and directories. The modes include permissions and special modes.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   441
 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   442
Usage
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   443
~~~~~
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   444
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   445
The *chmod* command options are specified like this:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   446
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   447
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   448
	$ chmod [options] mode[,mode] file1 [file2 ...]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   449
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   450
To view what the permissions currently are, type:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   451
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   452
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   453
	$ ls -l file
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   454
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   455
Command line options
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   456
~~~~~~~~~~~~~~~~~~~~
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   457
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   458
The *chmod* command has a number of command line options that affect its behavior. The most common options are:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   459
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   460
    * -R: Changes the modes of directories and files recursively
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   461
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   462
    * -v: Verbose mode; lists all files as they are being processed
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   463
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   464
Symbolic modes
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   465
+++++++++++++++
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   466
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   467
To the *chmod* utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   468
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   469
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   470
	$ chmod [references][operator][modes] file1 ...
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   471
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   472
The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all” but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   473
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   474
+--------------+--------+---------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   475
| Reference    | Class  |                Description                  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   476
+==============+========+=============================================+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   477
|      u       | user   | the owner of the file                       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   478
+--------------+--------+---------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   479
|      g       | group  | users who are members of the file's group   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   480
+--------------+--------+---------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   481
|      o       | others | users who are not hte owner of the file or  |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   482
|              |        | members of the group                        |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   483
+--------------+--------+---------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   484
|      a       | all    | all three of the above; is the same as *ugo*|
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   485
+--------------+--------+---------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   486
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   487
The *chmod* program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   488
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   489
+--------------+------------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   490
| Operator     |                      Description                     |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   491
+==============+======================================================+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   492
| +            | adds the specified modes to the specified classes    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   493
+--------------+------------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   494
| -            | removes the specified modes from the specified       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   495
|              | classes                                              |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   496
+--------------+------------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   497
| =            | the modes specified are to be made the exact modes   |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   498
|              | for the specified classes                            |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   499
+--------------+------------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   500
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   501
The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   502
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   503
+-----+--------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   504
|Mode |    Name      |                 Description                    |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   505
+=====+==============+================================================+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   506
| r   | read         | read a file or list a directory's contents     |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   507
+-----+--------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   508
| w   | write        | write to a file or directory                   |   
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   509
+-----+--------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   510
| x   | execute      | execute a file or recurse a directory tree     |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   511
+-----+--------------+------------------------------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   512
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   513
Examples
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   514
+++++++++
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   515
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   516
Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   517
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   518
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   519
	$ chmod ug+rw mydir
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   520
	$ ls -ld mydir
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   521
	drw-rw----   2 starwars  yoda  96 Dec 8 12:53 mydir
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   522
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   523
For a file, remove *write* permissions for all classes:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   524
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   525
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   526
	$ chmod a-w myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   527
	$ ls -l myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   528
	-r-xr-xr-x   2 starwars  yoda 96 Dec 8 12:53 myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   529
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   530
Octal numbers
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   531
+++++++++++++
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   532
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   533
The *chmod* command also accepts three and four-digit octal numbers representing modes. Using a three-digit octal number to set the modes of a file named myfile :
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   534
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   535
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   536
	$ chmod 664 myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   537
	$ ls -l myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   538
	-rw-rw-r--  1   57 Jul  3 10:13  myfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   539
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   540
Foe each one, you define the right like that :
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   541
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   542
    * a read right correspond to 4
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   543
    * a write right correspond to 2
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   544
    * an execute right correspond to 1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   545
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   546
You want the user to have all the rights? : 4 + 2 + 1 = 7
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   547
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   548
you want the group to have read and write rights : 4 + 2 = 6
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   549
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   550
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   551
Changing Ownership of Files
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   552
===========================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   553
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   554
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   555
chown
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   556
-----
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   557
The chown command is used to change the owner and group of files, directories and links.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   558
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   559
By default, the owner of a filesystem object is the user that created it. The group is a set of users that share the same access permissions (i.e., read, write and execute) for that object.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   560
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   561
The basic syntax for using chown to change owners is::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   562
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   563
    $chown -v alice wonderland.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   564
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   565
-v - For verbose Mode
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   566
 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   567
alice - Username of new owner 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   568
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   569
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   570
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   571
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   572
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   573
Working with text
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   574
=================
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   575
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   576
How do I look into a file?
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   577
--------------------------
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   578
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   579
more
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   580
----
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   581
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   582
In computing, *more* is a command to view  contents of a text file one screen at a time 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   583
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   584
Usage
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   585
~~~~~
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   586
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   587
The command-syntax is::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   588
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   589
	$ more [options] [file_name]
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   590
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   591
Traversing the pages ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   592
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   593
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   594
     SPACE       Display next k lines of text.  Defaults to current screen
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   595
                 size.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   596
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   597
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   598
     RETURN      Display next k lines of text.  Defaults to 1.  Argument
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   599
                 becomes new default.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   600
	       
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   601
     /pattern    Search for kth occurrence of regular expression.  Defaults to
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   602
                 1 .
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   603
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   604
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   605
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   606
less
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   607
-----
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   608
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   609
*less*  is similar to *more* in the sense that it is used to view files , but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, *less* does not need to read the entire file before starting, resulting in faster load times with large files.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   610
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   611
Usage
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   612
~~~~~~
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   613
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   614
*less* can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While *less* is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both *more* and *vi*. It is also possible to search for character patterns in the file.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   615
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   616
By default, *less* displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   617
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   618
The command-syntax is ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   619
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   620
	$ less [options] file_name
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   621
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   622
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   623
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   624
Frequently Used Commands
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   625
~~~~~~~~~~~~~~~~~~~~~~~~
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   626
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   627
    * [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   628
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   629
    * [Space bar]: Next page.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   630
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   631
    * b: Previous page.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   632
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   633
    * ng: Jump to line number n. Default is the start of the file.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   634
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   635
    * nG: Jump to line number n. Default is the end of the file.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   636
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   637
    * /pattern: Search for pattern. Regular expressions can be used.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   638
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   639
    * '^ or g: Go to start of file.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   640
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   641
    * '$ or G: Go to end of file.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   642
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   643
    * =: File information.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   644
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   645
    * h: Help.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   646
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   647
    * q: Quit.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   648
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   649
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   650
cat
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   651
---
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   652
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   653
The *cat* command is a standard Unix program used to concatenate and display files. The name is from "catenate", a synonym of *concatenate*.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   654
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   655
The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   656
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   657
If the filename is specified as -, then *cat* will read from standard input at that point in the sequence. If no files are specified, *cat* will read from standard input entered.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   658
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   659
Usage ::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   660
        $ cat foo boo
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   661
	This is file foo
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   662
	
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   663
	This is file boo.
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   664
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   665
Text Processing 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   666
---------------
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   667
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   668
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   669
How do look at part of a file?
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   670
------------------------------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   671
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   672
head
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   673
-----
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   674
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   675
*head* is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   676
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   677
	$ head [options] <file_name>
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   678
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   679
By default, *head* will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of somefile::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   680
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   681
	$ head -n 20 somefile
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   682
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   683
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   684
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   685
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   686
tail
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   687
----
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   688
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   689
*tail* is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   690
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   691
The command-syntax is::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   692
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   693
	$ tail [options] <file_name>
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   694
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   695
By default, *tail* will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The following example shows the last 20 lines of somefile::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   696
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   697
	$ tail -n 20 somefile
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   698
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   699
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   700
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   701
This example shows all lines of somefile from the second line onwards::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   702
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   703
	$ tail -n +2 somefile
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   704
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   705
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   706
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   707
Monitoring a continously changing file(example: A log file) 
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   708
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   709
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   710
*tail* has a special command line option *-f* (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   711
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   712
	$ tail -f /var/log/dmesg
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   713
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   714
To interrupt tail while it is monitoring, break-in with *Ctrl+C*. This command can be run "in the background" with &, see job control.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   715
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   716
More serious Text Processing:
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   717
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   718
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   719
Problem1. Get the names of people in the following file
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   720
-----------------------------------------------------
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   721
::
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   722
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   723
	Foot in Mouth:Bully:Fat:Peter
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   724
	Rich:Simple:Peabrain:Lois
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   725
	Self-concious:Wannabe:Adolescent:Meg
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   726
	Dim-witted:Fat:evil-monkey:Chris
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   727
	Matricidal:Over-Ambitious:Infant:Stewy
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   728
	Anthropomorphic:Democrat:Sensible:Brian
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   729
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   730
Soulution::
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   731
 	$cut -d : -f 4- file
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   732
	
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   733
	Peter
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   734
	Lois
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   735
	Meg
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   736
	Chris
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   737
	Stewy
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   738
	Brian
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   739
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   740
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   741
		
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   742
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   743
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   744
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   745
cut
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   746
----
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   747
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   748
In computing, *cut* is a Unix command line utility which is used to extract sections from each line of input — usually from a file.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   749
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   750
Extraction of line segments can typically be done by  a *delimiter (-d — the tab character by default)*. A range must be provided in which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   751
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   752
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   753
Options
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   754
-------
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   755
::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   756
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   757
	-b   select on the basis of bytes
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   758
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   759
        -c   select on the basis of characters
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   760
			
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   761
			
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   762
	-f  select on  the basis of fields,also print any line that contains no delimiter character
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   763
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   764
Problem2.Given two files of student name and marks merge them
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   765
-------------------------------------------------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   766
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   767
File of students(students.txt) ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   768
     
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   769
     Hussain
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   770
     Dilbert
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   771
     Alex
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   772
     Raul
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   773
     Sven
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   774
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   775
File of marks(marks.txt)::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   776
     
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   777
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   778
     89
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   779
     98
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   780
     67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   781
     78
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   782
     67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   783
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   784
Solution::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   785
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   786
	$paste students.txt marks.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   787
	Hussain	89
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   788
	Dilbert	98
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   789
 	Alex	67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   790
 	Raul	78
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   791
 	Sven	67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   792
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   793
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   794
	$paste -d : students.txt marks.txt
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   795
	
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   796
	Hussain:89
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   797
	Dilbert:98
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   798
	Alex:67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   799
 	Raul:78
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   800
 	Sven:67
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   801
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   802
	
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   803
	$paste -s students.txt marks.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   804
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   805
	Hussain	  	 Dilbert	 Alex	 Raul	 Sven	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   806
	89		 98		 67	 78	 67
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   807
121
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   808
caf2b538fa0a Added a question answer format for initial introduction to make it recipie based .
amit
parents: 98
diff changeset
   809
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   810
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   811
paste
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
   812
------
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   813
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   814
*paste* is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   815
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   816
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   817
Some frequently used Text Processing tools
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   818
==========================================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   819
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   820
Given a file with names US presidents  . Sort the file  on the names . The contents of the file are mentioned below
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   821
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   822
      
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   823
       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   824
       John%Kennedy
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   825
       Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   826
       Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   827
       Franklin%Rosevelt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   828
       George%Bush 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   829
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   830
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   831
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   832
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   833
       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   834
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   835
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   836
Solution:: 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   837
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   838
	 $sort presidents.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   839
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   840
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   841
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   842
       Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   843
       Bill%Clinton       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   844
       Franklin%Rosevelt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   845
       George%Bush 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   846
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   847
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   848
       John%Kennedy
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   849
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   850
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   851
Sort
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   852
====
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   853
sort command with the file name as a parameter sorts the lines of the file alphabetically and prints the output on the terminal.	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   854
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   855
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   856
To sort the same file using the last names     
132
24cec0337e81 Some changes made to title levels
amit
parents: 124
diff changeset
   857
------------------------------------------
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   858
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   859
::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   860
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   861
	$sort -t % -k 2 presidents.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   862
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   863
	George%Bush 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   864
        Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   865
        Bill%Clinton       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   866
        John%Kennedy
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   867
        Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   868
        Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   869
        Franklin%Rosevelt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   870
        George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   871
        George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   872
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   873
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   874
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   875
	-t - specify the delimiter 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   876
	-k - specify the column 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   877
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   878
There are other options like -r to sort on reverse order etc ..
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   879
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   880
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   881
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   882
Problem:Given a file remove all the duplicate lines in it ?
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   883
===========================================================
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   884
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   885
File ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   886
     
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   887
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   888
       Abraham%Lincoln
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   889
       Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   890
       Bill%Clinton       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   891
       Franklin%Rosevelt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   892
       George%Bush 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   893
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   894
       George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   895
       John%Kennedy
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   896
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   897
Solution::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   898
      
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   899
      $uniq -c sortpresidents.txt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   900
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   901
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   902
      2 Abraham%Lincoln       
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   903
      2 Bill%Clinton
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   904
      1 Franklin%Rosevelt
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   905
      1 George%Bush 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   906
      2 George%Washington
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   907
      1 John%Kennedy
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   908
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   909
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   910
uniq
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   911
====
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   912
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   913
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   914
``uniq`` command removes duplicate lines next to each other.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   915
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   916
-c - also gives the no. of repetitions .
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   917
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   918
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   919
Redirection and Piping
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   920
=======================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   921
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   922
In computing, *redirection* is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   923
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   924
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   925
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   926
Problem: Get a sorted list of all xml file in directory ?
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   927
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   928
Soulition::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   929
	$ls | grep '.xml$' > xmllist
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   930
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   931
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   932
This problem introduced a lot of concepts we shall look at them one by one .
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   933
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   934
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   935
Redirecting standard input and standard output
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   936
-----------------------------------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   937
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   938
Redirection refers to redirecting standard Input or output to a command or file .
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   939
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   940
Ex::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   941
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   942
	$ command > file1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   943
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   944
This command executes *command1*, placing the output in file1. Note that this will truncate any existing data in *file1*. To append output to the end of the file, use the >> operator like this
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   945
     	$ command >> file1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   946
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   947
Ex2::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   948
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   949
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   950
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   951
	$ command < file1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   952
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   953
executes *command1*, using *file1* as the source of input (as opposed to the keyboard).::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   954
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   955
	$ command1 < infile > outfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   956
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   957
combines the two capabilities: *command1* reads from *infile* and writes to *outfile*
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   958
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   959
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   960
In this example we see redirection in the end ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   961
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   962
   $(some commands) > xmllist 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   963
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   964
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   965
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   966
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   967
Piping
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   968
-------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   969
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   970
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   971
A pipeline of two programs run on a text terminal::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   972
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   973
	$ command1 | command2
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   974
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   975
executes *command1*, using its output as the input for *command2* (commonly called piping, since the '|' character is known as a "pipe").
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   976
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   977
This is equivalent to using two redirects and a temporary file ::
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
   978
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   979
	$ command1 > tempfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   980
	$ command2 < tempfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   981
	$ rm tempfile
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   982
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   983
In the above example we used piping in the following manner ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   984
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   985
	$ echo
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   986
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   987
This runs the ftp client with input user, press return, then pass.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   988
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   989
Redirecting to and from the standard file handles
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   990
--------------------------------------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   991
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   992
In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   993
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   994
+------------+-------------+------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   995
|   Handle   |    Name     |      Description       |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   996
+============+=============+========================+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   997
| 0          |   stdin     |    Standard input      |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   998
+------------+-------------+------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
   999
| 1          |   stdout    |    Standard output     |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1000
+------------+-------------+------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1001
| 2          |   stderr    |    Standard error      |
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1002
+------------+-------------+------------------------+
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1003
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1004
For example:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1005
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1006
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1007
	$ command1 2> file1
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1008
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1009
executes *command1*, directing the standard error stream to *file1*. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1010
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1011
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1012
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1013
	$ find / -name .profile 2>&1 | less
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1014
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1015
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1016
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1017
Chained pipelines
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1018
------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1019
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1020
The redirection and piping tokens can be chained together to create complex commands. For example:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1021
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1022
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1023
	$ ls | grep '\.sh' | sort > shlist
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1024
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1025
lists the contents of the current directory, where this output is filtered to only contain lines which contain *.sh*, sort this resultant output lexicographically, and place the final output in *shlist*. This type of construction is used very commonly in shell scripts and batch files.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1026
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1027
Redirect to multiple outputs
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1028
-----------------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1029
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1030
The standard command *tee* can redirect output from a command to several destinations.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1031
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1032
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1033
	$ ls -lrt | tee xyz
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1034
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1035
This directs the file list output to both standard output as well as to the file *xyz*.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1036
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1037
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1038
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1039
Shell Meta Characters
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1040
======================
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1041
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1042
Unix recognizes certain special characters, called "meta characters," as command directives. The shell meta characters are recognized anywhere they appear in the command line, even if they are not surrounded by blank space. For that reason, it is safest to only use the characters A-Z, a-z, 0-9, and the period, dash, and underscore characters when naming files and directories on Unix. If your file or directory has a shell meta character in the name, you will find it difficult to use the name in a shell command.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1043
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1044
The shell meta characters include:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1045
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1046
\ / < > ! $ % ^ & * | { } [ ] " ' ` ~ ; 
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1047
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1048
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1049
As an example,
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1050
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1051
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1052
	$ ls file.*
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1053
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1054
run on a directory containing the files file, file.c, file.lst, and myfile would list the files file.c and file.lst. However,::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1055
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1056
	$ ls file.?
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1057
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1058
run on the same directory would only list file.c because the ? only matches one character, no more, no less. This can save you a great deal of typing time. For example, if there is a file called california_cornish_hens_with_wild_rice and no other files whose names begin with 'c', you could view the file without typing the whole name by typing this::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1059
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1060
	$ more c*
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1061
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1062
because the c* matches that long file name.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1063
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1064
filenames containing metacharacters can pose many problems and should never be intentionally created.
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1065
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1066
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1067
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1068
74
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
  1069
grep
7dbeae0fac70 Removed numbering for sections
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 73
diff changeset
  1070
-----
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1071
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1072
*grep* is a command line text search utility originally written for Unix. The name is taken from the first letters in *global / regular expression / print*, a series of instructions for the *ed* text editor. The *grep* command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1073
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1074
Usage
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1075
~~~~~~
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1076
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1077
This is an example of a common *grep* usage:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1078
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1079
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1080
	$ grep apple fruitlist.txt
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1081
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1082
In this case, *grep* prints all lines containing 'apple' from the file *fruitlist.txt*, regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The *grep* command is case sensitive by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1083
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1084
Like most Unix commands, *grep* accepts command line arguments to change this and many other behaviors. For example:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1085
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1086
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1087
	$ grep -i apple fruitlist.txt
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1088
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1089
This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells *grep* to be case insensitive, or to ignore case.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1090
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1091
To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match):
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1092
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1093
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1094
	$ grep -w apple fruitlist.txt
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1095
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1096
Regular expressions can be used to match more complicated queries.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1097
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1098
Variations
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1099
+++++++++++
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1100
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1101
There are countless implementations and derivatives of *grep* available for many operating systems. Early variants of *grep* included *egrep* and *fgrep*. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern *grep* implementations as command-line switches (and standardized as -E and -F in POSIX). In such combined implementations, *grep* may also behave differently depending on the name by which it is invoked, allowing *fgrep*, *egrep*, and *grep* to be links to the same program.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1102
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1103
*pcregrep* is an implementation of *grep* that uses Perl regular expression syntax.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1104
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1105
Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The *pgrep* utility, for instance, displays the processes whose names match a given regular expression.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1106
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1107
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1108
Elementary Regex
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1109
=================
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1110
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1111
In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. A regular expression (often shortened to regex or regexp) is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1112
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1113
Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, Perl, Ruby and Tcl have a powerful regular expression engine built directly into their syntax. Several utilities provided by Unix distributions—including the editor *ed* and the filter *grep* — were the first to popularize the concept of regular expressions.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1114
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1115
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1116
Regular Expressions are a feature of UNIX. They describe a pattern to match, a sequence of characters, not words, within a line of text. Here is a quick summary of the special characters used in the grep tool and their meaning: 
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1117
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1118
* ^ (Caret)        =    match expression at the start of a line, as in ^A.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1119
* $ (Question)     =    match expression at the end of a line, as in A$.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1120
* \ (Back Slash)   =    turn off the special meaning of the next character, as in \^.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1121
* [ ] (Brackets)   =    match any one of the enclosed characters, as in [aeiou].
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1122
                      Use Hyphen "-" for a range, as in [0-9].
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1123
* [^ ]             =    match any one character except those enclosed in [ ], as in [^0-9].
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1124
* . (Period)       =    match a single character of any value, except end of line.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1125
* * (Asterisk)     =    match zero or more of the preceding character or expression.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1126
* \{x,y\}          =    match x to y occurrences of the preceding.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1127
* \{x\}            =    match exactly x occurrences of the preceding.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1128
* \{x,\}           =    match x or more occurrences of the preceding.
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1129
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1130
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1131
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1132
Here are some examples using grep:
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1133
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1134
*    grep smug files         {search files for lines with 'smug'}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1135
*    grep '^smug' files      {'smug' at the start of a line}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1136
*    grep 'smug$' files      {'smug' at the end of a line}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1137
*    grep '^smug$' files     {lines containing only 'smug'}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1138
*    grep '\^s' files        {lines starting with '^s', "\" escapes the ^}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1139
*    grep '[Ss]mug' files    {search for 'Smug' or 'smug'}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1140
*    grep 'B[oO][bB]' files  {search for BOB, Bob, BOb or BoB }
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1141
*    grep '^$' files         {search for blank lines}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1142
*   grep '[0-9][0-9]' file  {search for pairs of numeric digits}
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1143
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1144
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1145
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1146
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1147
Lazy quantification
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1148
--------------------
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1149
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1150
The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex. For example, someone new to regexes wishing to find the first instance of an item between < and > symbols in this example:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1151
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1152
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1153
	Another whale explosion occurred on <January 26>, <2004>.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1154
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1155
...would likely come up with the pattern <.*>, or similar. However, this pattern will actually return "<January 26>, <2004>" instead of the "<January 26>" which might be expected, because the `*` quantifier is greedy — it will consume as many characters as possible from the input, and "January 26>, <2004" has more characters than "January 26".
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1156
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
  1157
Though this problem can be avoided in a number of ways (e.g., by specifying the text that is not to be matched: <[^>]*>), modern regular expression tools allow a quantifier to be specified as *lazy* (also known as non-greedy, reluctant, minimal, or ungreedy) by putting a question mark after the quantifier (e.g., <.*?>), or by using a modifier which reverses the greediness of quantifiers (though changing the meaning of the standard quantifiers can be confusing). By using a lazy quantifier, the expression tries the minimal match first. Though in the previous example lazy matching is used to select one of many matching results, in some cases it can also be used to improve performance when greedy matching would require more backtracking.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1158
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1159
One Liners
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1160
===========
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1161
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1162
A *one-liner* is textual input to the command-line of an operating system shell that performs some function in just one line of input.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1163
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1164
The one liner can be
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1165
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1166
   1. An expression written in the language of the shell.
73
d32a19958ad9 Completed first review and cuts of ULT
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 55
diff changeset
  1167
   2. The invocation of an interpreter together with program source for the interpreter to run.
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1168
   3. The invocation of a compiler together with source to compile and 	  
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1169
      instructions for executing the compiled program.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1170
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1171
Certain dynamic scripting languages such as AWK, sed, and perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as these Unix shells or the Windows PowerShell, allow for the construction of powerful one-liners.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1172
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1173
The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1174
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1175
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1176
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1177
Here is a one line shell script to show directories:
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1178
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1179
::
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1180
98
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1181
      $grep user * | cut -d":"  -f1|uniq
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1182
678c7c01b5d5 changes to Intorduction to Linux and Text Processing in ULT
amit@thunder
parents: 74
diff changeset
  1183
This returns list of all files which has the word user in it .
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1184
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1185
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1186
Environment Variables:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1187
======================
55
6aa9568b5874 Completed rough draft of first three sessions in module one.
Kadambari Devarajan <kadambari.devarajan@gmail.com>
parents: 44
diff changeset
  1188
122
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1189
These variables like HOME, OSTYPE,Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.By convention, environment variables have UPPER CASE and shell variables have lower case names.  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1190
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1191
Some of examples of Environment variables are: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1192
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1193
   $ echo $OSTYPE 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1194
   linux-gnu
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1195
   $ echo $HOME
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1196
   /home/user 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1197
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1198
To see all the variables and there values use any of following commands: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1199
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1200
   $ printenv | less
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1201
   $ env
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1202
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1203
The most commonly used environment variable is "PATH", it defines a list of directories to search through when looking for a command to execute. If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1204
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1205
   $ set PATH=$PATH:$HOME/bin
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1206
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1207
See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1208
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1209
   $ set repo = $HOME/Desktop/random/code
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1210
   $ cd $repo
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1211
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1212
*set* command is used to define a variable for the current shell. Try opening a new shell and use the above mentioned command, it wont work as expected. The other child process wont be able to see these variables unless we *export* them. Repeat the above mentioned activity with *export* command. Now with all new shells, *$repo* will work.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1213
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1214
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1215
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1216
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1217
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1218
Shell Scripting:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1219
================
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1220
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1221
Basics:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1222
-------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1223
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1224
Shell program or shell script,a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands. The first *"Hello World"* sample for shell scripting is as easy as it sounds: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1225
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1226
   $ echo '#!/bin/sh' > my-script.sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1227
   $ echo 'clear' >> my-script.sh   
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1228
   $ echo 'echo Hello World' >> my-script.sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1229
   $ chmod 755 my-script.sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1230
   $ ./my-script.sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1231
   Hello World
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1232
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1233
The #! syntax(also known as shebang) is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The chmod is required to make the script executable. This script will just execute two commands, *clear* and *echo* one after another. One can also do the same task using a one liner command *clear; echo 'Hello World';* but as number of lines grows using a script file is helpful. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1234
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1235
So lets create a script which gives us all the filenames for given initial alphabet or string in a directory. Let the name of script be *initial.sh*, open it with text editor, and write: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1236
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1237
   #!/bin/sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1238
   ls > temp
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1239
   grep ^$1 < temp
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1240
   rm temp
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1241
   $ chmod a+x initial.sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1242
   $ ./initial.sh s
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1243
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1244
The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via *$#* with name of script being first member, that is $0. Now lets write a script for finding a file, and then checking when was it last modified: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1245
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1246
   #!/bin/sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1247
   name=`find . -name $1 -print`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1248
   echo $name
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1249
   last_modified=`stat -c %y $name| cut -f 1 -d " "`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1250
   echo "Last modified: $last_modified"    
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1251
   $ ./search.sh fname
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1252
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1253
Try giving some file you want to search in place of fname. Please note in second line *`* its a back-quote(other key mapped with tilda), it is specifically used to get the output of one command into a variable. In this particular case name is a User defined variables  which stores the value. We access value stored in any variable using *$* symbol before name of variable.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1254
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1255
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1256
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1257
Shell Arithmetic:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1258
-----------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1259
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1260
Shell also provides support for basic arithmetic operations. The syntax is: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1261
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1262
   $ expr op1 math-operator op2
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1263
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1264
Some of example which can be tried handily: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1265
   
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1266
   $ expr -3 + 5
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1267
   2
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1268
   $ expr 10 % 3
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1269
   1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1270
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1271
These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1272
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1273
   $ expr 2*3
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1274
   expr: syntax error
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1275
   
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1276
One can use back-quotes(`) also to get value of expr. ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1277
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1278
   $ echo `expr 6 + 3`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1279
   9
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1280
   $ result=`expr 6 + 3`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1281
   $ echo $result
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1282
   9
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1283
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1284
Shell uses three kinds of quotes. Double quotes("), anything enclosed among them except from variable trailing after $, and characters after \ would be printed as it is. Single quotes('), anything enclosed within them is just same, no formulation/interpretation. Back quotes(`), anything inclosed is considered as command, or is executed. ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1285
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1286
   $ echo "Today is date"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1287
   Today is date
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1288
   $ echo "Today is `date`"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1289
   Today is Wed Sep 16 17:32:22 IST 2009
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1290
   $ echo 'Today is `date`'
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1291
   Today is `date`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1292
   $ echo "Today is \n `date`"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1293
   Today is \n Wed Sep 16 17:40:13 IST 2009
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1294
   $ echo -e "Today is \n `date`"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1295
   Today is 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1296
    Wed Sep 16 17:41:13 IST 2009 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1297
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1298
if else construct:
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1299
------------------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1300
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1301
One can have simple *if else if* constructs in shell scripts to check conditions. Lets take simple example of writing a script which returns back whether the argument passed is positive or not: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1302
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1303
   #!/bin/sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1304
   if test $1 -gt 0
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1305
   then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1306
     echo "number is positive"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1307
   else
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1308
     echo "number is negative"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1309
   fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1310
   $ ./sign.sh -11
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1311
   number is negative
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1312
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1313
This script will compare the first value passed as argument with 0 *if test var -gt val*, var being $1 and val being 0, gt meaning greater then. Now this program has some flaw, it will give same result for following input: (-11) and (-1, 5), as we are checking just $1 which is first argument and hence the result. For handling such situation we can include *if-else* clause which will warn user of correct usage of script. ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1314
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1315
   #this is the case when no argument is passed  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1316
   if [ $# -eq 0 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1317
   then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1318
     echo "$0 : You must give/supply one integers"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1319
     exit 1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1320
   else 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1321
     if [ $# -gt 1 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1322
     then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1323
       echo "$0 : You must give one integer"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1324
       exit 1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1325
     fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1326
   fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1327
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1328
One important thing to note in shell script is spacing, with many comparison and evaluation operation a wrongly placed space will spoil all the fun. So in previous example the expression *[ $# -eq 0 ]* will work properly, but if we remove those leading or trailing spaces like *[ $# -eq 0]*, it wont work as expected, or rather throw a warning. Both *test* and *[]* do the same task of testing a expression and returning true or false.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1329
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1330
Lets create something interesting using these if-else clause. Now we will create a script which will greet the user when he opens the shell. We will create the script, change the permission to make it executable and append the *.bashrc* file with *./greet.sh* line and we are done. The script is: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1331
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1332
   #!/bin/sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1333
   #Script to greet the user according to time of day
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1334
   temph=`date | cut -c12-13`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1335
   dat=`date +"%A %d in %B of %Y (%r)"`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1336
   if [ $temph -lt 12 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1337
   then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1338
     mess="Good Morning $LOGNAME, Have a nice day!"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1339
   fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1340
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1341
   if [ $temph -gt 12 -a $temph -le 16 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1342
   then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1343
     mess="Good Afternoon $LOGNAME"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1344
   fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1345
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1346
   if [ $temph -gt 16 -a $temph -le 18 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1347
   then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1348
     mess="Good Evening $LOGNAME"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1349
   fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1350
   echo -e "$mess\nThis is $dat"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1351
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1352
For me when I open the shell the output is something like: ::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1353
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1354
   Good Morning user, Have a nice day!
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1355
   This is Wednesday 16 in September of 2009 (11:54:47 AM IST) 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1356
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1357
Loops
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1358
-----
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1359
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1360
Bash has three different commands for looping -- ``for``, ``while`` and ``until``. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1361
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1362
``for`` loop
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1363
~~~~~~~~~~~~
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1364
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1365
Suppose we have a set of files, that have names beginning with numbers followed by their names - ``08 - Society.mp3``. We would like to rename these files to remove the numbering. How would we go about doing that? It is clear from the problem statement that we could use a ``for`` loop, to loop through the list of files and rename each of the files.  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1366
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1367
Let's first look at a simple ``for`` loop, to understand how it works. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1368
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1369
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1370
  for animal in rat cat dog man
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1371
  do 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1372
    echo $animal
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1373
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1374
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1375
We just wrote a list of animals, each animal's name separated by a space and printed each name on a separate line. The variable ``animal`` is a dummy variable and has no significance. You could use something as lame as ``i`` in place of ``animal``.  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1376
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1377
Now, we use a simple ``for`` loop to list the files that we are interested in. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1378
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1379
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1380
  ls *.mp3 > list
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1381
  for i in `cat list`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1382
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1383
    echo "$i"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1384
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1385
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1386
If your filenames contain spaces, ``for`` assumes each space separated word to be a single item in the list and prints it in a separate line. We could change the script slightly to overcome this problem. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1387
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1388
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1389
  for i in *.mp3
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1390
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1391
    echo "$i"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1392
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1393
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1394
Now, we have each file printed on a separate line. Depending on the files that we have we could use grep to get the relevant portion of the filenames and rename the files. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1395
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1396
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1397
  for i in *.mp3
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1398
  do 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1399
    j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3")
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1400
    echo "$i -> $j"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1401
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1402
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1403
Now we just replace the echo command with a ``mv``  command. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1404
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1405
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1406
  for i in *.mp3
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1407
  do 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1408
    j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3")
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1409
    mv "$i" "$j"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1410
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1411
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1412
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1413
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1414
``while``
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1415
~~~~~~~~~
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1416
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1417
The ``while`` command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1418
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1419
Let's start with the lamest example of a while loop.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1420
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1421
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1422
  while true
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1423
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1424
    echo "True"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1425
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1426
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1427
This, as you can see, is an infinite loop that prints the ``True``. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1428
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1429
Say we wish to write a simple program that takes user input and prints it back, until the input is ``quit``, which quits the program. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1430
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1431
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1432
  while [ "$variable" != "quit" ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1433
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1434
    read variable
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1435
    echo "Input - $variable"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1436
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1437
  exit 0
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1438
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1439
``until``
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1440
~~~~~~~~~
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1441
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1442
The ``until`` loop is similar to the ``while`` loop, except that it executes until the conditional command does not execute properly. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1443
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1444
The infinite loop changes to the following, when ``until`` is used.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1445
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1446
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1447
  until false
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1448
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1449
    echo "True"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1450
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1451
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1452
Now lets try and use these above mentioned options provided by shell to write a utility. Until now, when we try find or locate it looks through directories and files for result. But they wont search through tar archives and zipped files. Lets create a shell script for especially looking through these files
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1453
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1454
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1455
  #!/bin/sh
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1456
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1457
  #To check number of arguments being passed.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1458
  if [ $# -eq 0 ] ; then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1459
  echo "Correct usage: $0 tar-archive filename \nOr $0 filename"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1460
  exit 1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1461
  else
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1462
    if [ $# -eq 1 ] ; then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1463
      tar_archive=`find $PWD -name "*.tar*"`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1464
    else
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1465
      tar_archive=`find $PWD -name $1`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1466
    fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1467
  fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1468
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1469
  #Search of particular file inside archives.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1470
  for archive in $tar_archive
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1471
  do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1472
    echo $archive
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1473
    variable=`tar -tf $archive`
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1474
    for word in $variable
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1475
    do
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1476
      if [ $# -eq 1 ] ; then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1477
        echo "$word" | grep -q ".*$1"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1478
      else
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1479
	echo "$word" | grep -q ".*$2"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1480
      fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1481
    if [ $? -eq 0 ] ; then 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1482
      echo "File present in $archive!" 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1483
    fi  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1484
    done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1485
  done
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1486
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1487
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1488
Functions
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1489
---------
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1490
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1491
When a group of commands are repeatedly being used within a script, it is convenient to group them as a function. This saves a lot of time and you can avoid retyping the code again and again. Also, it will help you maintain your code easily. Let's see how we can define a simple function, ``hello-world``. Function can be defined by using function name followed by a pair of parentheses. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1492
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1493
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1494
  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1495
  hello-world () {
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1496
    echo "Hello, World.";
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1497
  }
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1498
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1499
  $ hello-world
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1500
  Hello, World.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1501
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1502
Passing parameters to functions is similar to passing them to scripts. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1503
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1504
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1505
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1506
  #! /bin/bash
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1507
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1508
  hello-name()
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1509
  {
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1510
     echo  "hello ". $1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1511
        
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1512
  }
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1513
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1514
  hello-name $1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1515
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1516
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1517
  #!usr/bin/bash
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1518
  hello-name
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1519
  { 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1520
  echo "Hello, $1."; 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1521
  }
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1522
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1523
  hello-name $1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1524
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1525
  save this in a file helloscipt.sh and give it execute permission
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1526
  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1527
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1528
  $ ./helloscipt 9
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1529
  Hello, 9.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1530
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1531
Any variables that you define within a function, will be added to the global namespace. If you wish to define variables that are restricted to the scope of the function, define a variable using the ``local`` built-in command of bash.
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1532
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1533
  
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1534
We shall now write a function for the word frequency generating script that we had looked at in the previous session. 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1535
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1536
::
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1537
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1538
  word_frequency() {
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1539
    if [ $# -ne 1 ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1540
    then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1541
      echo "Usage: $0 file_name"
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1542
      exit 1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1543
    else 
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1544
      if [ -f "$1" ]
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1545
      then
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1546
        grep  "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1547
      fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1548
    fi
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1549
  }
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1550
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1551
 word_frequency  $1
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1552
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1553
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1554
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1555
	
70b86355e851 Refactored a lot of material . Mostly for continuity also some recipie based additions
amit
parents: 121
diff changeset
  1556