Thursday, December 1, 2011

Using calculator in UNIX

UNIX provides both INFIX and POSTFIX calculators.

INFIX is when the operations are embedded within the operators
12*2

POSTFIX is when the operators are listed down and then followed by operations.
12 2 *

So, now to use infix calculator type the command "bc"
bash-3.00$ bc
Nothing happend. Now type in what you need to calculate and the system gives you output
1*2
2
1*2+3
5
2+3*2
8

Ok, now how to get out of this? Its simple. "quit"
.
.
2+3*2
8
quit
bash-3.00$

Various powerful options with bc command:

Find square root?

bash-3.00$ bc
sqrt(16)
4
similarly use below:


Postfix calculator:

user command "dc"

bash-3.00$ dc
2
3
*
p
6
2 3 *
p
6
2 3 * 4 +
p
10
Use "p" to print the result of your calculation.
sqrt(n) Square root of n
% Remainder
^ To the power of (3^5 is 3 to the power of 5)
s(n) Sine(n)
c(n) Cosine(n)
e(n) Exponential(n)
l(n) Log(n)

How to find the date and time in an UNIX system

What I would like to know is the current date and time, so just type in command "date"

bash-3.00$ date
Fri Dec  2 11:02:42 IST 2011


What if I need to look into the calendar? Here it is..


bash-3.00$ cal
   December 2011
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

This list down only current month. What if I need the entire year?

Its very simple. With the command "cal" mention the year you are interested in

bash-3.00$ cal 2011

                                2011
         Jan                    Feb                    Mar
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
         Apr                    May                    Jun
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

         Jul                    Aug                    Sep
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
         Oct                    Nov                    Dec
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31


Ok, I agree. But what I need to look into the month of Feb for the year 1999?

bash-3.00$ cal 2 1999
   February 1999
 S  M Tu  W Th  F  S
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28


Is that ok?

Be careful while specifying the year. You have to give fully year, iec 2011 not 11. See the difference below.

bash-3.00$ cal 12 2011
   December 2011
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

bash-3.00$ cal 12 11
   December 11
 S  M Tu  W Th  F  S
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Wednesday, November 30, 2011

How to find out who is doing what in an UNIX system

OK, so you are here to learn how to find out who is doing what in your UNIX system.

Then first of all let me ask, who are you?
Now, you have a question in your mind, who am i?

type it down in your system and lets see what the system responds.

bash-3.00$ who am i
scott      pts/3        Nov 30 19:37    (10.203.54.223)

now you have your account name/user name, the communication line, date and time of login and your ip address as well.

Now you know who you are, right? Is that all that you need to know about you?

You know that UNIX is a multi user system. Hence the system you have logged in probably might have more users other than you. Rather than having a unique name, don't you think the UNIX system will have a unique id for each of the users in the system!! So let us check our what id Unix has assigned to you.

Type down what come to your mind. Nothing but id!!

bash-3.00$ id
uid=200(scott) gid=201(scott)


It give you both id and your group id as well. After all you don't need to bother much about UID and GID

Now you know who you are, and now its time for you to check who else is there in the system.

who

The only limitation here is, you can get details only on users who are currently logged in to the system.

bash-3.00$ who
scott      pts/2        Nov 30 11:31    (10.203.54.102)
taylor      pts/3        Nov 30 19:37    (10.203.54.223)
ram      pts/4        Nov 30 12:52    (10.203.54.4)
build      pts/5        Nov 24 19:15    (10.222.46.132)


Four users are logged into the same UNIX system. Their communication channel, the time they logged into the system and their system ip address is also listed down.


Now you know who all are there in the system. But do you know what they are doing? Watching some ...movies? You want to check it out?

Use the command "w", a combination of "who are they?" and "what they do?"

bash-3.00$ w
  8:02pm  up 215 day(s),  3:13,  8 users,  load average: 0.18, 0.20, 0.20
User     tty           login@      idle   JCPU   PCPU  what
scott    pts/2        11:31am    17                              bash
taylor    pts/3       7:37pm                                       bash


This command gives us info about the user name, ttyp, logged in time, how long since the user has done anything, combined CPU time of all jobs run by the user, CPU time taken by current job and last field tells you what the user is doing?


So, next time while you do something in your UNIX system, beware that other users can check it out, what you are doing!!

*All the commands explained here are tested against SunOS

How to change password for my UNIX account

Use the command "passwd" to change password of your UNIX account


bash-3.00$ passwd
passwd: Changing password for fwire
Enter existing login password:
New Password:
Re-enter new Password:
passwd: They don't match.
Please try again
New Password:


As I dont want to change my existing one, I gave it wrong :-) !!

Friday, November 25, 2011

How UNIX differs from other Operating System

UNIX is an operating system as like other OS available in market. Its does the task as any other OS does, running programmes, managing resources and communicating with other computer systems.

The uniqueness of UNIX OS is, multiple users can access the same system simultenously, hence we call UNIX a multiuser system. Multiple users can access the machine, run their programmes, do their work all in the same computer, which is not possible in a windows OS. Moreover these users can run multiple programmes from a UNIX machine, hence it is called a multitasking system.

UNIX is also called as a Multichoice system as it has three different primary command-line interface. This command line interface is called shell. Three available flavours of this command-line interface (shell) are Bourne shell, Korn shell and C shell.

UNIX has more than 250 individual commands ranging from simpler to complex ones for high speed networking, developing software and file revision management. Whereas Microsfot MS-DOS and Apple Macintosh has very fewer commands. These interfaces are easy to use which give only less controll to the user.

The way unix system stores data, ordering of files, connection to the external devices are entirely different from Windows or Macintosh OS. Unix use plain text for storing data; it has a hierarchical file system. Everything in UNIX is a file. Every programme, data, external devices everything. It treat devices and certain types of inter-process communication as files.

Tuesday, November 22, 2011

UNIX: Search for files

The grep command helps you to search with the text pattern in the file or filenames. It helps you to find the files by their content. Whereas find command helps you in searching for the files.

Examples:

Print all directories and filenames under the current directory
bash-3.00$ find .
.
./wm-dev
./wm-dev/bin
./wm-dev/bin/exec.sh
./wm-dev/config

Print all directories and filenames under the current directory/directory
bash-3.00$ find wm-dev
wm-dev
wm-dev/bin
wm-dev/bin/exec.sh
wm-dev/config

You can notice that the list printed starts with the searching directory.

Print all directories and filenames under the current directory/directory/directory
bash-3.00$ find wm-dev/bin
wm-dev/bin
wm-dev/bin/exec.sh

From the root directory search for a filename of the given pattern 'exec.sh'
bash-3.00$ find . | grep 'exec.sh'
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909
Another one alternative for the above option.
bash-3.00$ find . -name "exec.sh"
./wm-dev/bin/exec.sh
./wm-cn/bin/ca7-orig/exec.sh.orig
./wm-cn/bin/exec.sh
./wm-cn/bin-old/exec.sh
./wm-cn-dev/bin/exec.sh
./wm-prod-dev-old/bin/exec.sh
./wm-prod-dev/bin/exec.sh
./wm-prod-dev/bin/exec.sh_090909

Find files that have been modified in last 5 days (with minus)
find . -mtime -5

Find files that was modified exactly 5 days before (without minus)
find . -mtime 5

Find files that was not modified for past 5 days (with +)
find . -mtime +5

Find files in multiple directories.
find usr bin -name "*.cp"

List the directories alone.
find . -type d

List the directories alone.
find . -type f

List the directories alone.
find . -type l

UNIX: Terminating a process

You can terminate a process using the KILL command. To KILL a process you need to have the process ID passed to the KILL command.

There are different types of signals that you pass to a process.
1    SIGHUP - Hang Up
2    SIGINT - Interrrupt
9    SIGKILL - Kill Immediate
15  SIGTERM - Terminate

SIGHUP signal is sent to every process you are running just before you log out of the system.
SIGINT signal is sent when you press ctrl+c
SIGKILL - Kills a process immediately. Temporary files are not deleted here.
SIGTERM - Immediate termination of progamme but allows cleaning up the temporary files.

The default signal with KILL command is SIGTERM.

You can specify the signals with KILL command either by their number or by their name without the SIG
eg:-
kill -9 process.sh
or
kill KILL process.sh

Basically there are 30 different signals in UNIX. The above explained are the most important ones.

Example:
Finding the jobs
bash-3.00$ jobs
[2]-  Stopped                 vi
[3]+  Stopped                 vi
Killing the job with % (here we terminates the job)
bash-3.00$ kill %2
bash-3.00$ jobs
[2]-  Done                    vi
[3]+  Stopped                 vi
Finding Jobs:
bash-3.00$ jobs
[3]+  Stopped                 vi
Killing jobs with KILL signal (-KILL is missing)
bash-3.00$ kill KILL %3
bash: kill: KILL: arguments must be process or job IDs
starting a process in the background
bash-3.00$ vi &
[1] 28236
bash-3.00$ jobs
[1]+  Stopped                 vi
Kill process with signal number (kill immediate)
bash-3.00$ kill -9 28236
bash-3.00$ jobs
[1]+  Killed                  vi
Checking for jobs
bash-3.00$ jobs
No jobs left

create new job in bg
bash-3.00$ vi &
[1] 28321

bash-3.00$ jobs
[1]+  Stopped                 vi

Kill with signal number option for termination
bash-3.00$ kill -15 28321

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill 28321
bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$ kill -KILL 28321
bash-3.00$ jobs
[1]+  Killed                  vi
Note: Inorder to run the kill command for a second time to make sure that the kill works, enter "!!"

UNIX: Find the running processes

Finding out Jobs that runs in your UNIX system is a simple task. The command, jobs, helps you to list the jobs which are either stopped or moved to background for process.


Below code automatically moves the vi process to the background
bash-3.00$ vi &
[1] 27467

On entering hte "jobs" command it listed the jobs that are stopped in the background.

bash-3.00$ jobs
[1]+  Stopped                 vi
bash-3.00$
Alternative to find the processes running in your UNIX system:
The command ps is another one alternative to find the jobs running in your UNIX system. This is a little complex. The command ps stands for processor status and it lists out the processor status of the entire computer.


bash-3.00$ ps
   PID TTY         TIME CMD
 27467 pts/15      0:00 vi
 27469 pts/15      0:00 ps
  3784 pts/15      0:01 bash
  3780 pts/15      0:00 sh

Monday, November 21, 2011

UNIX Process or Jobs: Suspend and Resume

In unix whatever command you execute from the command line initiate a process. Say for example you tried ls tol list files invokes the process ls.  In UNIX any program that's running is a process. You can have multiple process running at once.

Any program that you are running is known as current job.

States of Jobs/Process:
There are different states for the Jobs/Process in UNIX. The most important one is "running".

Stopping and restarting a Job:
A job can be stopped by pressing ctrl+z and to restart it enter fg.

bash-3.00$ ./logging.sh
Stopping the server please wait .........\n
Tue Nov 22 12:08:52 IST 2011
Invoking the PL/SQL Procedures and Bulk add command \n
Bulk Add Start Time:
Tue Nov 22 12:08:52 IST 2011
^Z
[1]+  Stopped                 ./logging.sh
bash-3.00$ fg
./logging.sh
Bulk Add End Time:
Tue Nov 22 12:09:09 IST 2011
Done with  PL/SQL Procedures and Bulk add. Starting the server. Please wait....\n
Tue Nov 22 12:09:09 IST 2011
1
bash-3.00$
Why we use ctrl+z to stop and fg to restart it later?
These commands helps us to stop a a job or process (ctrl+z) that we are doing currently and perform some other job and then resume (fg) our stopped job later.

For example if we are working on a vi editor editing our file and in between you want to execute some other programme press ctrl+z and once after completing your second job you can resume to editing your file by entering fg.

bash-3.00$ vi
I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!!   ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z

~
~
~
Incomplete shell escape command - use 'shell' to get a shell
[1]+  Stopped                 vi
bash-3.00$
bash-3.00$ ls -l | tail -5
-rw-r--r--   1 fwire    fwire       1331 Apr 20  2011 publishpreview.sh
-rw-r--r--   1 fwire    fwire         11 Feb 25  2011 publishpreview_dummy.sh
-rw-r--r--   1 fwire    fwire        275 Oct 15 17:28 shiyas_new.txt
-rw-r--r--   1 fwire    fwire       1622 Oct 15 16:35 test.sh
-rw-r--r--   1 fwire    fwire        313 Oct 15 17:15 uniqtest.txt
bash-3.00$fg
 I am testing stopping job and resuming it later
let me stop the job now !!
i am pressing ctrl+z!!   ^Z^Z^Z^Z^Z^Z
ooops!! Before pressing ctrl+z we should be in command mode!!
Moving to command mode and then pressing ctrl+z


I am back !!
~

Wednesday, October 26, 2011

Shell Variables

Working with Shell variables.

As like any other programming languages shell also has variable (most people dont know we can do programming with shell commands.). The shell variables takes single values as strings, even the numeric values is also considered as string.

Declaring and assigning shell variable:
You can declare and assign shell variable as simple as below.
bash-3.00$ color=yellow

You need to be careful while doing this. There should not be any space before and after equal to sign '=', if exists throws error.


Referencing a shell variable:
Below command will display the value stored in the variable color


bash-3.00$ echo $color
yellow


 

what is the output?
bash-3.00$ echo $coloryish

As no variable is declared as coloryish the output will be null. If you are actually interested to display "yellowyish", place the variable in curly braces as below.

bash-3.00$ echo ${color}yish
yellowyish


What if instead of variable color we supplied colour, which is not defined?
bash-3.00$ echo The dress is ${colour}ish
The dress is ish

How to handle this? Fortunately shell helps us to have default values to be set in case the variable is not defined as below
bash-3.00$ echo The dress is ${colour:-green}ish
The dress is greenish


The default value will come into effect only if the variable referenced  is not declared. The syntax :- tells the shell to print the following character

Assigning Variables with read command:
We can assign values to multiple variables at a time with read commnad.

bash-3.00$ read city state message
Thazhekode Kerala Hi Mom!

The above command declares three variables city, state and message and assign values respectively.
city=Thazhekode
state=Kerala
message=Hi Mom!

bash-3.00$ read city state message
Thazhekode New Jersey Hi Mom!

city=Thazhekode
state=New
message=Jersy Hi Mom!

Read command assigns an individual word to a specified variable, and the remaining words are assigned to the last variable. To handle this situation you can use the escape character

bash-3.00$ read city state message
Thazhekode New\ Jersey Hi Mom!




Tuesday, October 25, 2011

UNIX: Shell scripts

Definition: Shell scripts are a series of shell commands added in a file in exactly the order in which they’ll be
executed. Upon giving execute permission for this file, you can execute this entire set of commands by just entering the file name as like a command.

Example:
This example script will search for a specified file in all dirctories.


UNIX: Setting custom prompts

The below command is based on ksh


$ bash
bash-3.00$ PS1='Order me$'
Order me$

UNIX Command Aliases

With alias, you can define new commands that does whatever you like to do with unix in a customized way.

Format for using aliases in
csh:- alias word command-sequence
ksh:- alias word='command'

Example:
bash-3.00$ alias shiyas='ls -CF'

If you want to see the list of aliases created, you only need to enter the word alias

Example:
bash-3.00$ alias
alias ls='ls -CF'
alias shiyas='ls -CF'

Note:
There should not be any space befor or after '=' sign.

If you want to make availability of an alias permanent add it into .chrsc if you are using csh or to .profile if you are using korn shell

C shell History Commands

CommandFunction
!!Repeat the previous command
!$Repeat the last word of the previous command.
!*Repeat all but the first word of the previous command.
^a^bReplace a with b in the previous command.
!nRepeat command n from the history list.

Friday, October 21, 2011

Identifying the type of shell

If you screen shows a % symbol, it means you are using the C shell or modified C shell (tcsh
). If your prompt contains $you could be using the Bourne shell, the Korn shell, or a variant thereof.

UNIX difference btw :q and :q!

Open a file in vi editor and modify the content of the file.

Now try entering :q

of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
No write since last change (:quit! overrides)


You get the error as above. In this case you should enter :q!

Hence
:q means quit vi editor
:q! quit regardless of whether any changes have occured

UNIX: Searching within the file

Go to 10th line
10G

Then what does G does?
Go to last line.

Search for a pattern in the file
Enter '/', will take you to the end of the file and then provide the patter to search...

primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
/decide



To find next occurence press "n"

What if I need to search backward
ans:
use the command ?
primary players. Each is a little different from the other.) Another contributor to the sprawl
of modern UNIX is the energy of the UNIX programming community; plenty of UNIX users
decide to write a new version of a command in order to solve slightly different problems, thus
spawning many versions of a command.
C
?decide



Now when you enter command "n" it will search backward!!

Thursday, October 20, 2011

Deleting text with "vi" editor

While in command mode there are few commands provided by UNIX to delete the text from the file.

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
do it, too.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Try entering "dd" which deletes an entire line and shift the lines up
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
UNIX, by contrast, is much more like a spoken language, with
command options (which you learn about later in this lesson)
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

"2dd" - will delete two lines
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
more complex commands acting akin to sentences. How you do a specific task can, therefore,
(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

 "D" - Delete the line to the right of the cursor
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.
_

(pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

Here after entering "D", we have
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


Now let us tweek a little of the "d" commnad
when "w" (word) is used with "d", dw this delete word by word


What is the command in Unix equivalent to delete and Backspace that we use in windows?
x - Deletes the text as like how we use the "delete" button in windows
X [<-] - Deletes the text as like when we use the backspace in windows.


What if you need to undo the delete?
You can undo only the last action using "u"

Example Scenario:
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>

type dw

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "D"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system_
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "u"
<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


type "U"

<<file content>>
you can learn the one-and-only
in doing that task as is the next person. It.s easy.

pronounce that .system five release four. or, to sound like an ace, .ess-vee-are-four.), and
BSD UNIX (pronounced .bee-ess-dee.) from University of California at Berkeley are the
<<>>


You can see that entire line is replaced. This can be achieved only if you hadn't moved from the line which you are editing.

Change the case of the letter
Use the command tild (~) or "delete" button in your keyboard.

Vi Editor

When we need to add some more lines to existing file we can use "cat" commnad, if we need to replace some string we have "sed"

But when we need to create a new file or edit already existing files we have to go for screen oriented editors.
There are different types of editors vi, ed, ex and emacs of which vi is the most choosen one. vi and emacs use the entire screen to edit.

Different modes of vi:
vi editor has two different modes, hence it is also called "modaleditor". The two different modes are "insert" and "command" mode. The way UNIX interprets your input depends on which mode we are. For example when we are in commnad mode and enter "dd", it will delete one single line, whereas if we are in edit mode entering "dd" will add "dd" to the file. So whenever you are in doubt in which mode you are, just keep on pressin escape button which will take you to command mode.


Diffrence beween vi and emacs:
emacs is a modeless editor. ie whenver you enter any key it adds to the file. Command in emac is entered by holding the control key while entering the command. For example ctrl+c will delete a character.

Cursor Moverments:

In keyboard the letters H,J,K,L comes in a series.
To move the curson left in a file -H
Right - L
Down - J
Up -K

You can move to the begining of next line by pressing the return key.
Move the cursor to the begining of line - ^,0 (zero)
Move the cursor to the end of line - $
Move forward word by word - w
Move backward word by word- b
Move forward to mid of the text in screen - ctrl+d
Move backward mid of the text - ctrl+u
Page down - ctrl+f
Page up -ctrl+b

vi insert modes:
There are basically four different ways you can get into vi insert mode.
i - insert text into file
a - append text into file
o - Open up a line below current line
O - Open up a line above current line (capital o)

Difference btw i and a for edit mode:
at present the cursor is on a, and now I am going to enter edit mode by entering "i"
On entring "i" the curson doesnt move and it will be there in the same location as above and now I am entering "cursor not moved" and you can see that the text entered to the left of "a" with cursor is not moved at all from "a" and then  press escape, you will find cursor move one space back.

On entering "a" to get into the edit mode, vi behaves differently.
Assume the cursor position is on "a" same as above and press "a" to move to edit mode, you can see that the cursor moves one place forward to "l" and whatever we enter is appended after the letter "l" and press escape, you see cursor moves one space back.

Tuesday, October 18, 2011

Power Filters and File redirection

Two powerful commands that we learn in this section:
awk:helps you get specific columns of information, modify text, swap the order of column of information in a file
tee: enable you to save a copy of the data being transmitted through a pipeline

awk
Syntax: awk '{commands}'
flags:
-f file  specifies that the instruction should be read from the file rather than from command line
-Fc indicates that the program should consider the letter c as the separator between fields of

using print with awk
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print}'
build      pts/2        Oct 17 11:28    (10.222.46.189)
root       pts/10       Jul 12 18:45    (10.209.188.122)
scholm  pts/12       Oct 18 23:46    (10.203.54.223)
jmadh  pts/15       Oct 19 11:06    (10.203.54.225)


bash-3.00$ who | awk '{print$1}'
build
root
scholm
jmadh

bash-3.00$ who | awk '{print$2}'pts/2
pts/10
pts/12
pts/15

bash-3.00$ who | awk '{print$3}'Oct
Jul
Oct
Oct
Here what you have seen is each column is considered as field and each field is represented by $1, $2, $3 etc...for field one, field two, field three respectively.

Much better customization:
bash-3.00$  who | awk '{ print "User " $1 " is on terminal line " $2 }'User build     is on terminal line pts/2
User root     is on terminal line pts/10
User scholm is on terminal line pts/12
User jmadh is on terminal line pts/15
Note: You need to be careful to place the strings to be printed in double quotes, else throw error as below, due to conflict with single quotes of the awk command.

bash-3.00$  who | awk '{ print 'User' $1 ' is on terminal line ' $2 }'awk: syntax error near line 1
awk: illegal statement near line 1


NF Variable:
NF indicates number of fields in a line.
When we use this variable without a $ sign it give us number of fields in each line

bash-3.00$ who | awk '{print NF}'6
6
6
6

When used with $ it displays the value at last field of each line
bash-3.00$ who | awk '{print $NF}'(10.222.46.189)
(10.209.188.122)
(10.203.54.223)
(10.203.54.225)

similarly NR stands for number of records.

Sum up the file size with the awk command:
bash-3.00$ ls -ltotal 80
-rw-r--r--   1 fwire    fwire         12 Oct 18 22:40 MyWords.txt
-rw-r--r--   1 fwire    fwire       2958 Oct 19 00:02 def.txt
-rw-r--r--   1 fwire    fwire       2976 Oct 19 00:01 def_new.txt
-rwxr-xr-x   1 fwire    fwire        141 Oct 18 23:40 search
-rw-r--r--   1 fwire    fwire       2958 Oct 18 21:57 what is unix.txt

file size is the fifthe field
bash-3.00$ ls -l | awk '{print $5}'
12
2958
2976
141
2958

Now we need to sum up all these size

bash-3.00$ ls -l | awk '{total=total+$5;print total}'0
12
2970
5946
6087
9045

To get the summed up vlaue alone use command below
bash-3.00$ ls -l | awk '{total+=$5;print total}'| tail -19045

Much better way is using END command
bash-3.00$  ls -l | awk '{total+=$5} END {print total}'9045
It will be better if we can put this code into some file so that our command line will be less lengthier
% cat << EOF > script
{ totalsize += $4 } END { print “You have a total of “totalsize .”}
EOF
The above command will create a file called script in your current directory with belwo content
{ totalsize += $4 } END { print “You have a total of “totalsize .”}

bash-3.00$ ls -l | awk -f scriptYou have a size of 9150
In above command -f tells awk command to look into the file script

 
We can use awk commnad for conditional execution of statement as well, as shown in the example below
% awk -F: ‘{ if (length($1) == 2) print $0 }’ /etc/passwd | wc -l

Here the passwd file is given as input for the awk command, where it says the field seperator is ':' and if the length of first field, ic account is two character lenght, then print the entire lines from the passwd file and this output  is then supplied to wc where we added -l flag, which gives us the total number of lines.

Say we recived an output of 5, this indicates that there are totally 5 accounts which are two character length.
 
tee  command has only one option, -a, which appends the stream data/ output to a specified file.
bash-3.00$ who | tee who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


bash-3.00$ cat who.outfwire      pts/10       Jul 12 18:45    (10.209.188.122)
fwire      pts/12       Oct 18 23:46    (10.203.54.223)
fwire      pts/15       Oct 19 14:33    (10.203.54.224)


Here who | tee who.out placed the output of who to who.out file, which can also be achieved by the command who > who.out. Hence, what is the difference between these two.

In the first type of command the primary thing is we are displaying who are all there in the system and in the background the contents are saved into a specified file, here who.out (you can see the output in the screen for first type of command whereas you cant see for the second type). In the second type of command, the action is writing the output to  the specified file but not been displayed in the screen.


 





tee command:
information, rather than the default of white space

UNIX command "sed" examples

Here "sed" stands for "stream edit"
The main use of sed is to replace the string provided

bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g'
In the above example the word LINUX is replaced by UNIX in the file def_new.txt and is displayed.
s stands for substitute.

What if you need replace multiple strings?
bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g;s/system/COMPUTER'

sed command can also be used to delete
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed 'd'
wont display anything as it deletes everything

bash-3.00$ who | sed '1d'
delete the first line
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '1,2d'
delete the first two lines
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '/build/d'delete only the user build
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '/rmc/,/build/d'
delete both rmc and build
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '1,/rmc/d'root      pts/12       Oct 18 23:46    (10.203.54.223)
delete from line 1 till the line has rmc

UNIX "grep" command Examples

What is grep used for?
ans: grep can be considered as a "search" command for UNIX/LINUX systems. Used for searching text strings and some regular expressions.

Options:

Examples:

1) Search for a text string in a file

grep 'shiyas' /etc/passwd

above command search for all the occurence of the text 'shiyas' in the file passwd and print the lines having those text on the screen.

2) Search for the string in multiple files

grep 'shiyas' *

above command search for all the occurence of the text 'shiyas' in all the files in current dierectory and print the lines having those text on the screen along with the filenames. Here the wildcard character * looks for all the files in the current directory.

grep 'shiyas' *.txt
this command will search for the pattern in all txt files.

grep 'SHIYAS' /etc/passwd
wont display anything as it doesnt have any string SHIYAS, and UNIX is very sensitive for this case

inorder to avoid this case sensitive issue you can use below command which will help you to get the output you need irrespective of case

grep -i 'SHIYAS' /etc/passwd

4) what if I need to display all the lines which doesnt have the string called 'shiyas'
grep -v 'shiyas' /etc/passwd

Note: It is not necessary to place the string in single quotes, but mandatory when a space is included.

5) find all the sub-directories in the current directory
ls -al | grep '^d'

6) search for multiple patterns at one time (egrep)
egrep 'and|loop|cursor' example.txt

Note: egrep stand for "extended grep"
7) suppose you want to search for the strings "Foo" or "Goo" in all files in the current directory. That grep command would be:

grep '[FG]oo' *

8) Search for the string 'bond' but not 'jamesbond'

grep '^bond' /etc/password

9) Display the files that has the search string?
grep -l 'bond' /etc/password

10) Display the line number as well along withe lines that has the search string
grep -n 'bond' /etc/password

11) Display the lines before/after your search pattern

grep -B 4 'bond' /etc/password
this command displays 4 lines before the search pattern

grep -A 4 'bond' /etc/password
this command diplays 4 lines after the search pattern

12) List down all files which has pattern 'bond' in all the subdirectories in the current directory tree

find . -type f -exec grep -il 'foo' {} \;
This command will list down all the files which has the given search pattern, no matter at what level of directory it is

13) What is the grep command that returns specific number of rows for the search pattern provided.
For ex :- if your file has 10 row with the given string and you want to display only 5 out of 10 rows use below command

grep text 'string' | head -5

What if i want to search for multiple patterns?
Use egrep. egrep stands for "extended grep".

How it works?
What if you have to search for a pattern either this"" or this "", you have rely on "egrep" command which has more powerful notational scheme than "grep" command.

NotationMeaning
cMatches the character c
\cForces c to be read as the letter c, not as another meaning the character might have
^Beginning of the line
$End of the line
.Any single character
[xy]Any single character in the set specified
[^xy]Any single character not in the set specified
c*Zero or more occurrences of character c
c+One or more occurrences of character c
c?Zero or one occurrences of character c
a|bEither a or b
(a)Regular expression


Application of egrep is explained as below.

cat passwd | head-10

root:x:0:0:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:

When I am looking for one or more occurence of string 'c' in /etc/passwd file, using grep the command is as below
grep 'cc*' /etc/passwd
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico

what if i am entering the command as
grep 'c*' /etc/passwd
this will list down all those lines which has zero or more occurence of the string 'c' which is equal to listing down the entire content of the file.

root:x:0:0:Super-User:/:/sbin/sh
daemon:x:1:1::/:
bin:x:2:2::/usr/bin:
sys:x:3:3::/:
adm:x:4:4:Admin:/var/adm:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:

whereas while using egrep it is as simple as,
egrep 'u+' /etc/passwd
root:x:0:0:Super-User:/:/sbin/sh
bin:x:2:2::/usr/bin:
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:
listen:x:37:4:Network Admin:/usr/net/nls:


then what if I am giving the command as
egrep 'cc+' /etc/passwd
this will list down all those lines which has two or more occurence of the charater 'c'
uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico


Now let us look into the question asked above, how to search for multiple patterns.
It is as simple as this

bash-3.00$ cat passwd |head -10 | egrep 'uu|mm'uucp:x:5:5:uucp Admin:/usr/lib/uucp:
nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico
smmsp:x:25:25:SendMail Message Submission Program:/:

I want to list down those lines which starts with a
bash-3.00$ cat passwd |head -10 | egrep '^a' passwdadm:x:4:4:Admin:/var/adm:
apache:x:104:103:Apache User:/export/apache:/bin/bash


What if I want to list down those lines which starts with a,b,c,d
bash-3.00$ cat passwd |head -10 | egrep '^[a-d]' passwddaemon:x:1:1::/:
bin:x:2:2::/usr/bin:
adm:x:4:4:Admin:/var/adm:
build:x:102:103:Build User:/export/build:/bin/bash
apache:x:104:103:Apache User:/export/apache:/bin/bash
csvn:x:204:204:CollabNet Subversion User:/opt/CollabNet_Subversion:/bin/sh


What is fgrep and how it is used?
fgrep stands for 'file based grep'. For example I have a file of search strings say MyWords.txt and what I need is to list down the lines in 'appreport.txt' file having these search strings.

For this let me go the directory
bash-3.00$ cd /tmp/shiyas/skills/unix/
bash-3.00$ lswhat is unix.txt
bash-3.00$ cat what\ is\ unix.txt | head -5X is a computer operating system, a control program that works with users to run
programs, manage resources, and communicate with other computer systems. Several people
can use a UNIX computer at the same time; hence UNIX is called a multiuser system. Any
of these users can also run multiple programs at the same time; hence UNIX is called
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot


let me create one file Mywords.txt having words manage & such

bash-3.00$ vi MyWords.txtmanage
such
~
~
~
:wq!

"MyWords.txt" [New file] 2 lines, 12 characters
bash-3.00$ cat MyWords.txtmanage
such


bash-3.00$ fgrep -f MyWords.txt what\ is\ unix.txtprograms, manage resources, and communicate with other computer systems. Several people
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot
used in high-speed networking, file revision management, and software development.
Why is having all this choice such a big deal? Think about why Microsoft MS-DOS and the


Is there any alernative for typing down such a long command?
Ofcourse you have, and it is using 'alias'
the command for alias is
bash-3.00$ alias search='fgrep -i -f MyWords.txt'
you have to be very careful with the syntax else will through error.
the space after '=' will give error as below
bash-3.00$ alias search= 'fgrep -i -f MyWords.txt'
bash: alias: fgrep -i -f MyWords.txt: not found


Now lets see how we can use this alias
bash-3.00$ search what\ is\ unix.txtprograms, manage resources, and communicate with other computer systems. Several people
multitasking. Because UNIX is such a pastiche.a patchwork of development.it.s a lot
used in high-speed networking, file revision management, and software development.
Why is having all this choice such a big deal? Think about why Microsoft MS-DOS and the
bash-3.00$


Now I am removing the alias function
bash-3.00$ unalias search
bash-3.00$ search what\ is\ unix.txt
bash: search: command not foundbash-3.00$

I need to display only the words that match instead of the entire line, what should I do?
To achieve this we have to use the 'awk' command , for which below is sample
bash-3.00$ echo 'My name is shiyas' | awk '{for (i=1;i<=NF;i++) print $i}'
My
name
is
shiyas

bash-3.00$
NF stands for number of fields (here it is 4)

Now lets work on displaying only the matching word alone, not the entire line
Logic we are going to implement is:
Make the content of file a list of one word each as above and now search for the pattern
step1: awk '{for (i=1;i<=NF;i++) print $i}'
step2: fgrep -i -f MyWords.txt what\ is\ unix.txtFor this lets code one shell script to incorporate both above commands.

bash-3.00$ cat search
#Wrongwords - show a list of commonly misused words in the file
cat $* | \
awk .{for (i=1;i<=NF;i++) print $i}. |\
fgrep -i -f MyWords.txt


bash-3.00$ unalias search

Give execute permission for the above shell script
bash-3.00$ chmod +x search



Saturday, October 15, 2011

Unix Commands

> To sort filenames alphabetically regardless of case

% ls -1 | sort -f

ls -1 will list the files in one single column, this output is passed to sorting, where sort -f makes sure that the list is sorted not considering the case.

> Sorting lines of a file?
sort < shiyas.txt

> find the largest file in your directory
bash-3.00$ ls -s | sort -nr
ls -s list the files and folder along with size
pass this to sort numerically and in reverse order.
the first line gives you the largest file along with its size in blocks.

Here if we need to see only the highest 5 files add head -5
bash-3.00$ ls -s | sort -nr | head -5

I have a file in which there is space after each line. what should be the unix command to remove these spaces?
I i am using "uniq" command for this purpose,  it wont work, as uniq command will consider the duplication of lines only if the duplicants are adjacent. What should i do in this case?

sort text.txt | uniq > new.txt

here the sorting will sort your file having space between each line, in a manner that the blank line will come first. Supply this output to the uniq command and now the blank lines are adjacent, hence uniq command take one among them and place this new content to a new file.
Problem solved !!

I want to display the contents of my file along with the line number. How could I achieve that?
% cat -n text.txt
here the flag n will do the job of giving line numbers

We have one alternative for this
% nl text.txt
"nl" will do the same job of "cat -n"

> test.txt has 10 lines of which 5 are blank lines. When nl test.txt is fired, it is expected that the output is a numbered lines. What will be the last number? 5 or 10
ans: 5. nl by default only numbers the lines that are not blank.

So, what should I do to number the blank lines as well?
% nl -ba test.txt
will number all the lines.

Is there any alternativ for nl command?
% nl -bt test.txt
will number only printable text


I want to number all those lines which has specific pattern.
% nl -bpORA -s: shiyas_new.txt

here "bpORA" looks for the word ORA in each line
"-s:" is used for seperator which seperates number and line like 1:


search for the line in a file for a given pattern:
%grep STATUS ../bin/fatwire.sh
here STATUS is the word you are looking for in the fatwire.sh

Output:
echo "$STATUS"
export STATUS
List down all the files under the directory which has a particular pattern in it
grep 'INGESTION' * */*

* expands your search beyond the files in the current dirctory
and */* expands your search to all files contained one directory below the current point.


I have a filename called 'wha is unix.txt' and I need to display the content of this file in screen for which I am using the command
cat 'what is unix.txt',
will this work and is there any other alternative for this?

Ofcourse this will work and you have one alternative as well. The alternative is:
cat what\ is\ unix.txt
Here what we have done is, we have used the escape character which tells the unix system to interpret the space as space itself.

Tuesday, September 20, 2011

UNIX Command: Looking into files.

file - to identify file types
head- peak at the first few lines
tail - view last few lines
cat - view contenst of file
more - view larger files.


file:
A program that can easily offer you a good hint as to the contents of a file by looking at the first few lines. The disadvantage of this commnad is, it is not 100% accurate
eg:- A text file has executable permission and the initial contents of the files look like a C program the file command interpret it as an executable program rather that an english text file.

% file newtext.txt helloworld.txt 'shiyas resume.doc'
newtext.txt: ascii text
....
...
photos: directory

% file *
will analyze all files in home directory
 
head: Use it to view up to the first few hundred lines of a very long file, actually. You can specify the number as well.
% head newfile.txt
first few lines are displayed

% head -4 newfile.txt
first 4 lines are displayed

Can supply multiple files as well
% head -4 newfile.txt passwd
=====>passwd<====
......................................

=====>newfile.txt<===
.......................................


Can be used along with pipes (|):
% who | head -5
the output of who is supplied to head -5 and lists down first 5 users in the list.


tail:
Provide last lines of the file.
combine the two, head and tail, so you can see just the tenth, eleventh, and twelfth lines of a file?
%

cat:
flags: -v -s
head -12 .cshrc | tail -3

Monday, September 19, 2011

UNIX questions

What is the output of % echo $DATE?
Ans: Nothing is displayed.

What is the output of %date?
ans: date with time

In my directory /u/home/phoenix I have one file name magazine.txt and a directory magazine. What will be the output for ls -l magaz* ? Will it list both the file and directory or only one among them? If one, which one?

ls -l magaz* will list the magazine.txt file only.
If you need to see the directory then you shoudl run ls -ld magaz*

Does the command mkdir has arguments?
ans: No

What if you create a directory with same name as one that exist already?
ans: Will throw error saying file already exist

umask is set to 0222, decode the defaul permission for this value.
ans:
The value for umask is just opposite of what the real privilege is set, hence
0 indicates owner has all access except execute : rw-
2 indicates group has all privilege except write and execute: r--
2:r--
Hence finally we have: -rw-r--r--

What is command use for moving a file?
ans: mv

What is the command used for renaming a file?
ans: mv

I have a directory in which there are many directories and files and some are as below:
diectories:DIR1, DIR2,DIR3
files:DIR1.txt, DIR2.txt
there are no files in DIR1 and DIR2, whereas DIR3 has two files hello.txt and hi.txt
Now i ran ls- l DI*, what will be the output?

ls -ld DI* what will be the output?

What is the result, cp newdir latestdir, where newdir is a directory?
cp: omitting directory 'newdir'

what is the resutl, cp newfile.txt
ans: cp: missing destination file operand after newfile.txt
Try 'cp --help' for more information.

What is the result on executing umask 77777?
bash: umask: 777777: octal number out of range

I have changed the umask value from the default one (022) to 777 and then I exit from the terminal and logged in again. What will the value of umask.

ans:022.
Each time you login to the terminal umask value get reset always.

Why Shell Scripts always require both read and execute permission?

Any program works fine in UNIX even it has only execute permission. By giving execute privilege alone for any program we make sure that, the users of this particular program has the permission to run the program but not to examine or copy or modify the code that we have written.

Shell scripts belongs to a special class of programs, which fails to execute if only execute privilege is granted on those. Why is this becasue, shell script, which acts as a UNIX command line macro facility, which helps in clubbing togther a series of commands into a single file and run them as a single program. Hence for the shell to execute this program (or commands inside the file), it need to read the file first and then execute.

So shell script always need both read and execute permissions!!

UNIX Commands

ls : List files in a directory

ls -a : List hidden dot files as well in a directory

ls -C -F: List files with slash ('/') for current driectory

ls -C -F /: List files with slash ('/') for root directory

ls -l: List files in long format.

ls -lr: List files in long format and in reverse order based on file/directory name.

ls -lt: List files in long format and ordered based on file/directory modified date. The most recently accessed files.

ls -lrt: List files in long format and in reverse order based on file/directory modified date.

ls -s: List the filenames along with its size. The total size of all files in the directory is given in the first line. Size is given in Kilobites rounded upwards.

ls -s will list all the files under the current directory and its size as well. If you are intereseted to see size details of any specific directory you can give the command as below
ls -s <dirname>

we can specify the filename as well
ls -s <filename>

You can specify multiple filenames or dierctories as well and seperate them by space.

ls -s <dir1> <dir2> <file1>
Output:
10 file1

dir1:
 total 456
     2 file1
     4 file2
     etc...
dir2:
 total 100
     45 file1
     50 file2
     etc...
ls -f: Suffices the file type at the end of the filename
Different types of suffices
       - / indicates directory
       - * Indicates program
       - @ indicates a symbolic link to another file or directory.

ls -m: List the contents of a directory comma seperated.

ls -1: List the content of directory in a single column.


-a
List all files, including any dot files.
-F
Indicate file types; / = directory, * = executable.
-m
Show files as a comma-separated list.
-s
Show size of files, in blocks (typically, 1 block = 1,024 bytes).
-C
Force multiple-column output on listings.-1 Force single-column output on listings.
ls -x: Change the default sorting order from column first then row to row first then column

ls -


pwd- stands for present working directory and will  display the absolute path of your current directory.

bc: Used for infix mathematical calcualtions.
$bc
12*4
48
quit
$


-1
Force single-column output on listings.
-a
List all files, including any dot files.-C Force multiple-column output on listings
-d
List directories rather than their contents.
-F
Indicate file types; / = directory, * = executable.
-l
Generate a long listing of files and directories.
-m
Show files as a comma-separated list.
-r
Reverse the order of any file sorting.
-R
Recursively show directories and their contents.
-s
Show size of files, in blocks (typically 1 block = 1,024 bytes).
-t
Sort output in most-recently-modified order.-x

Touch Command:
helps you create new files on the system
The main reason that
updated, as the following example demonstrates.
touch is used in UNIX is to force the last-modified time of a file to be
%
ls -l iecc.list
-rw------- 1 taylor 3843 Oct 6 18:02 iecc.list
%
touch iecc.list
%
ls -l iecc.list-rw------- 1 taylor 3843 Oct 10 16:22 iecc.list

If you try to use the
creates the file:

Compress:
compress textfile.txt

compress -v textfile.txt

Uncompress:
uncompres textfile.txt / uncompress textfile.txt.z
touch command on a file that doesn’t exist, the program
Sort output in row-first order.
Flag Meaning

mv
% ls -1
Payroll
newfile.txt
photos
newphotos

% mv newfile.txt latestfile.txt
% ls -1
Payroll
latestfile.txt
photos
newphotos

All the contents of the newfile.txt is moved into the latestfile.txt and latestfile.txt is removed from directory. The same is applicable in the case of directories also. We can entirely move the content of a directory into a new one. If you look closer you can find that this is nothing but just renaming the folder or file. Then what is actual moving of file or dirctory?

% mv newphotos ./Photos
% ls -1
Payroll
latestfile.txt
helloworld.txt
Photos
% ls -1 ./Photos
newphotos

Here the entire newphotos directory is moved into Photos directory. Similarly we can move the files as well from directory to directory.

What if I execute the below query?
% mv latestfile.txt helloworld.txt
ans: The contents of latestfile.txt is overwritten into helloworld.txt. A popup will come before overriding for which if give 'y' as input itwill override else wont.

rmdir:
rmdir is used to remove any directory. One advantage with rmdir is that, it removes only empty data, hence no need to worry on deleting the content of a directory unknowigly.
On executing rmdir against a directory which is not empty we get a message as below.
rmdir: failed to remove 'dir': Directory not empty

rm:
Used to remove files. Cant be used for directory (will throw error). This command removes any file. This command removes the file permanantly, cant be restored at any point of time. You can remove multiple files at a time.
For the safe side always use -i flag along with rm command, where i stands for interactive, which will ask us before removing the file
rm -r  is a dangerous one as it will delete files recursively

How could you check, what type of login shell you're running?
% grep pnaraya /etc/passwd
pnaraya:x:1000:1000:pnaraya,,,:/u/pnaraya:/bin/bash

on the password entry the login shell is specified. (/bin/bash)