Sunteți pe pagina 1din 14

WELCOME TO UNIX

BASIC TUTORIAL ON UNIX:

1. Every one needs an ID and a password to access UNIX system.


2. At the login prompt type your ID.
3. Next type your password. (Do not worry if you do not see anything on the
screen.)That is unix way of hiding your password. Hit enter, and valla! You
are there.
4. Logging Out :When you're ready to quit, type the command :exit
5. To know where you are (your current/present working directory the
command we use is ‘pwd’ in short for present working directory. This
command displays your current working directory.)
6. To create a dir the command we use is ‘mkdir <name>
7. Lets create a directory say abc: mkdir abc
8. We have created the directory but we are not in the directory we have
created. To check where we are currently in the unix file system type ‘pwd’
and the system will display where you are! If you want to go into the
directory we have created you have to type the command cd abc
9. To change your location in the filesystem heirarchy, use the cd (change
directory) command, followed by an argument defining where you want to
go. The argument can be either an absolute path to the destination, or a
relative path.
10. Now we are in the directory we created. If you do not trust where we are
use the previous command to chk your current position.
11. Now lets create some files in our directory.
12. In order to create files we use file editors like vi or pico or ed.
13. just type in the editors name followed by the name of your file.
14. ex:pico myfile.txt Or vi myfile.txt. (Use google to find more abt text editors)
15. Type anything you want and exit the editor . do not forget to save your
work!!
16. What if we want to go back to the parent directory. We use the cd
command.
17. cd .. will place you one level higher in your path. Or if you want to go to a
different dir you have to mention the complete path.
18. Try it. Now lets say you want to go into the dir. That we have created. You
know the command.
19. Once you are back in the dir. We created lets say we want to see how
many files there are in the dir.
20. To list the contents of your current directory:
21. ls (just gives names of files)
22. ls -al (gives all file details like size, date_last_modified...)
23. ls myfile.txt (to search for a specific file, myfile.txt)
ls *.txt (to search for all files with extension .txt)
ls m*.* (to list all files names that start with letter a
24. File permissions:
25. Try various options with chmod command
26. Here is a hint:

27. So far so good. But I want to read the contents of my file myfile.txt.
28. the command we use is ‘cat <file name>

29. The more command


The more command displays a text file, one screenful at a time. You can
scroll forward a line at a time by pressing the return key, or a screenful at
a time by pressing the spacebar. You can quit at any time by pressing the
q key.

The head and tail commands


The head command allows you to see the top part of a file. You may specify the
number of lines you want, or default to ten lines.

EXAMPLE: Type

head -15 myfile.txt


to see the first fifteen lines of the myfile.txt file.

The tail command works like head, except that it shows the last lines of file.

EXAMPLE: Type

tail myfile.txt
30. to see the last ten lines of the file myfile.txt. Because we did not specify
the number of lines as an option, the tail command defaulted to ten lines.
31. Copying files
cp <source_file_name> <destination_file_name>
32. The Move command moves a file from one location to another.
mv <source_file_name> <destination_file_name>
33. To delete a file/files.
rm <file_name>
34. Redirecting Input and Output:

CONCEPT: A program can be told where to look for input and where to send
output, using input/output redirection. UNIX uses the "less than" and "greater
than" special characters (< and >) to signify input and output redirection,
respectively.

Redirecting input

Using the "less-than" sign with a file name like this:


< myfile.txt
in a shell command instructs the shell to read input from a file called "myfile.txt"
instead of from the keyboard.

EXAMPLE:Use standard input redirection to send the contents of the file


myfile.txt to the more command:

more < myfile.txt

EXAMPLE: To see the first ten lines of the myfile.txt file, the command:

head myfile.txt
will work just the same as the command:
head < myfile.txt

Redirecting output

Using the "greater-than" sign with a file name like this:


> file2
causes the shell to place the output from the command in a file called "file2"
instead of on the screen. If the file "file2" already exists, the old version will be
overwritten.

EXAMPLE: Type the command

ls myfile.txt > ls.out


to redirect the output of the ls command into a file called "ls.out" in your home
directory.

Use two "greater-than" signs to append to an existing file. For example:

>> file2
causes the shell to append the output from a command to the end of a file called
"file2". If the file "file2" does not already exist, it will be created.

EXAMPLE: In this example, lets list the contents of the abc directory, and put it
in a file called myls. Then, Iets list the details of the myfile.txt file, and append it
to the file myls:

ls abc > myls


ls myfile.txt >> myls
35. Pipelines and Filters:
UNIX allows you to connect processes, by letting the standard output of one
process feed into the standard input of another process. That mechanism is
called a pipe.

Connecting simple processes in a pipeline allows you to perform complex tasks


without writing complex programs.

How could you use head and tail in a pipeline to display lines 5 through 10 of a
file?

ANSWER: The command

cat myfile.txt | head -10 | tail -5

would work. The cat command feeds the file into the pipeline. The head
command gets the first 15 lines of the file, and passes them down the pipeline to
tail. The tail command then filters out all but the last 5 lines of the input it
received from head. It is important to note that in the above example, tail never
sees the original file, but only sees the part of the file that was passed to it by the
head command. Now try to write the above lines into another file say myfile2.txt.

HIne:Use out put redirection…

Grep

The grep utility is one of the most useful filters in UNIX. Grep searches line-by-
line for a specified pattern, and outputs any line that matches the pattern. The
basic syntax for the grep command is grep [-options] pattern [file]. If the file
argument is omitted, grep will read from standard input. It is always best to
enclose the pattern within single quotes, to prevent the shell from misinterpreting
the command.

The grep utility recognizes a variety of patterns. Here are some of the characters
you can use to build grep expressions:

• The carat (^) matches the beginning of a line.


• The dollar sign ($) matches the end of a line.
• The period (.) matches any single character.
• The asterisk (*) matches zero or more occurrences of the previous
character.
• The expression [a-b] matches any characters that are lexically between a
and b.

EXAMPLE: Type grep 'abc' myfile.txt

to search the myfile.txt file for any lines containing the string "abc".

Type the command

grep '^abc' myfile.txt

to see the lines in myfile.txt that begin with the character string "abc".

Process Control and Multitasking:

The UNIX kernel can keep track of many processes at once, dividing its time
between the jobs submitted to it. Each process submitted to the kernel is given a
unique process ID.

The UNIX operating system will simultaneously perform multiple tasks for a
single user. Activating an application does not have to cause other applications
to be suspended.

Actually, it only appears that UNIX is performing the jobs simultaneously. In


reality, it is running only one job at a time, but quickly switching between all of its
ongoing tasks. The UNIX kernel will execute some instructions from job A, and
then set job A aside, and execute instructions from job B. The concept of
switching between queued jobs is called process scheduling.

Viewing processes

UNIX provides a utility called ps (process status) for viewing the status of all the
unfinished jobs that have been submitted to the kernel. The ps command has a
number of options to control which processes are displayed, and how the output
is formatted.

EXAMPLE: Type the command

ps
to see the status of the "interesting" jobs that belong to you. The output of the ps
command, without any options specified, will include the process ID, the terminal
from which the process was started, the amount of time the process has been
running, and the name of the command that started the process.

EXAMPLE: Type the command

ps -ef
to see a complete listing of all the processes currently scheduled. The -e option
causes ps to include all processes (including ones that do not belong to you),
and the -f option causes ps to give a long listing. The long listing includes the
process owner, the process ID, the ID of the parent process, processor
utilization, the time of submission, the process's terminal, the total time for the
process, and the command that started the process.

EXERCISE: Use the ps command, and the grep command, in a pipeline to find
all the processes owned by you.

EXPLANATION: The command

ps -ef | grep yourusername


where "yourusername" is replaced by your user name, will cause the output of
the ps -ef command to be filtered for those entries that contain your username.

Killing processes

Occasionally, you will find a need to terminate a process. The UNIX shell
provides a utility called kill to terminate processes. You may only terminate
processes that you own (i.e., processes that you started). The syntax for the kill
command is kill [-options] process-ID.

EXAMPLE: To force termination of a job whose process ID is 111, enter the


command

kill 111
(Do not try this as this wont work on your system).

Interaction and Job Control


When you log in to a UNIX system, the kernel starts a shell for you, and connects
the shell to your terminal. When you execute a command from the shell, the shell
creates a child process to execute the command, and connects the child process
to your terminal. By connecting the child process to your terminal, the shell
allows you to send input to the child process, and receive output from it. When
the child process finishes, the shell regains access to the terminal, redisplays the
shell prompt, and waits for your next command.
Any task that requires you to actively participate (like word processing) must be
in the foreground to run. Such jobs, termed "interactive," must periodically update
the display, and accept input from you, and so require access to the terminal
interface.

Other jobs do not require you to participate once they are started. For example, a
job that sorts the contents of one file and places the results in another file, would
not have to accept user commands, or write information to the screen while it
runs. Some UNIX shells allow such noninteractive jobs to be disconnected from
the terminal, freeing the terminal for interactive use.

Note that it is even possible to log out, and leave a background process running.
Unfortunately, there is no way to reconnect a background job to the terminal after
you've logged out.

Jobs that are disconnected from the terminal for input and output are called
"background" jobs. You can have a large number of background jobs running at
the same time, but you can only have one foreground job. That is because you
only have one screen, and one keyboard at your terminal.

Background and foreground jobs

The process that is connected to the terminal is called the foreground job. A job
is said to be in the foreground because it can communicate with the user via the
screen, and the keyboard.

A UNIX process can be disconnected from the terminal, and allowed to run in the
background. Because background jobs are not connected to a terminal, they
cannot communicate with the user. If the background job requires interaction with
the user, it will stop, and wait to be reconnected to the terminal.

Jobs that do not require interaction from the user as they run(like sorting a large
file) can be placed in the background, allowing the user to access the terminal,
and continue to work, instead of waiting for a long job to finish.

Starting a job in the background

To place a job in the background, simply add the ampersand character (&) to the
end of your shell command.

EXAMPLE: To sort a file called "foo", and place the results in a file called "bar",
you could issue the command

sort < foo > bar &

TRY: create a file called foo and enter numbers 1-10 in random order.
Using the above command sort these numbers and send the output to a
file called bar. Lets say I do not like the command sequence sort-foo-bar.
And I want the command sequence to be foo-sort-bar. Can we accomplish
this task.. Think abt it…

Examining your jobs

The UNIX command jobs allows you to see a list of all the jobs you have invoked
from the current shell. The shell will list the job ID of each job, along with the
status (running, stopped, or otherwise), and the command that started the job.
The shell considers the most recently-created (or most recently-manipulated) job
to be the current job, marked with a plus sign. Other jobs are referred to as
previous jobs, and are marked with a minus sign. The commands related to job
control will apply to the current job, unless directed to do otherwise. You may
refer to jobs by job ID by using the percent sign. Thus, job 1 is referred to as %1,
job 2 is %2, and so forth.

Suspending the foreground job

You can (usually) tell UNIX to suspend the job that is currently connected to your
terminal by typing Control-Z (hold the control key down, and type the letter z).
The shell will inform you that the process has been suspended, and it will assign
the suspended job a job ID.

There is a big difference between a suspended job, and a job running in the
background. A suspended job is stopped, and no processing will take place for
that job until you run it, either in the foreground, or in the background.

Placing a foreground job into the background

If you started a job in the foreground, and would like to place it in the
background, the first thing you must do is suspend the job with a Control-Z,
freeing your terminal. Then, you can issue the UNIX command
bg
to place the suspended job in the background. The bg command can accept a
job ID as an argument. If no job ID is given, bg assumes you are referring to the
current (suspended) job.

Bringing a background job to the foreground

You can reconnect a background job to your terminal with the UNIX command
fg
The fg command will accept a job ID as an argument. Make sure to include the
percent sign:
fg %2
will bring job 2 into the foreground. If no job ID is given, fg will assume you are
referring to the current (suspended) job.

Starting a suspended job

If you have a suspended job that you'd like to resume running, first you must
decide whether you want it running in the foreground, or the background. Find the
job ID of the suspended job with the jobs command, and then use bg (to run the
job in the background), or fg (to run the job in the foreground).

Misc:

1.wc

Use this command to count the number of characters, words, and lines in a file.
Suppose, for example, that we have a file dict with contents

red rojo
green verde
blue azul
white blanco
black negro
Then we can do this
% wc dict
5 10 56 tmp

This shows that dict has 5 lines, 10 words, and 56 characters.

The word count command has several options, as illustrated below:

% wc -l dict
5 tmp
% wc -w dict
10 tmp
% wc -c dict
56 tmp

2. sort

Use this commmand to sort a file. For example, suppose we have a file dict with
contents
red rojo
green verde
blue azul
white blanco
black negro
Then we can do this:
% sort dict
black negro
blue azul
green verde
red rojo
white blanco
Here the output of sort went to the screen. To store the output in file we do this:
% sort dict >dict.sorted
3.The awk utility is used for processing columns of data

A simple example shows how to extract column 5 (the file size) from the output of
ls -l

ls -l | awk '{print $5}' # try various combinations like $2 or $1 etc..

4.Find: The "find" command is very powerful. It can search the entire filesystem for
one or more files that you specify to look for. You can also use the find command to
locate files, and then perform some type of action on the files after they've been
located. With this capability, you can locate files using powerful search criteria, and
then run any Unix command you want on the files you locate.
Example:

find /usr -name Chapter1 -type f -print

This command searches through the "/usr" directory for the file named "Chapter1".
These file location is then printed to the screen.

find htdocs cgi-bin -name "*.cgi" -type f


-exec chmod 755 {} \;

This command searches through the "htdocs" and "cgi-bin" directories for files that
end with the extension ".cgi". When these files are found, their permission is
changed to mode 755 (rwxr-xr-x). This example shows that the find command can
easily search through multiple sub-directories (htdocs, cgi-bin) at one time.

How to search for a string in a selection of files (-exec grep ...).


TRY: find . -exec grep "my" '{}' \; -print

This command will search in the current directory and all sub directories. All files that
contain the string will have their path printed to standard output.

Lets do this on our file myfile.txt..


Use this command to find our file and change its permission to 777.

SED:
>cat file
I have three dogs and two cats
>sed -e 's/dog/cat/g' -e 's/cat/elephant/g' file
# s stands for substitution and g stands for global inside the quotes
I have three elephants and two elephants
>
OK. So what happened ? Firstly, sed read in the line of the file and executed
s/dog/cat/g
which produced the following text:
I have three cats and two cats
and then the second command was performed on the edited line and the result was
I have three elephants and two elephants

UNIX SHELL SCRIPTING:


A shell script, in its most basic form, is simply a collection of operating system
commands put into a text file in the order they are needed for execution. Using
any of the commands mentioned so far, a text file containing the commands of
choice to accomplish a specific task can be made. To execute the shell script just
type sh followed by the name of the script. Or just a period and the script name.

OR make the file an executable (by using chmod and then directly type its name
to execute it!!)

A simple example
Here's a very simple example of a shell script. It just runs a few simple
commands
#!/bin/bash
echo "hello, $USER. I wish to list some files of yours"
echo "listing files in the current directory, $PWD"
ls # list files

Firstly, notice the comment on line 4. In a bash script, anything following a pound
sign # (besides the shell name on the first line) is treated as a comment. ie the
shell ignores it. It is there for the benefit of people reading the script.

$USER and $PWD are variables. These are standard variables defined by the
bash shell itself, they needn't be defined in the script. Note that the variables are
expanded when the variable name is inside double quotes. Expanded is a very
appropriate word: the shell basically sees the string $USER and replaces it with
the variable's value then executes the command.

Example 2:
An example

#!/bin/bash
echo -n '$USER=' # -n option stops echo from breaking the line
echo "$USER"
echo "\$USER=$USER" # this does the same thing as the first two lines
The output looks like this (assuming your username is username)
$USER=username

$USER=username
Example3:
OK. Here's a potential problem situation. Suppose you want to echo the value of
the variable X, followed immediately by the letters "abc". Question: how do you
do this ? Let's have a try :
#!/bin/bash
X=ABC
echo "$Xabc"
THis gives no output. What went wrong ? The answer is that the shell thought
that we were asking for the variable Xabc, which is uninitialised. The way to deal
with this is to put braces around X to seperate it from the other characters. The
following gives the desired result:
#!/bin/bash
X=ABC
echo "${X}abc"
Example 4:
Execute the following and see what happens.. `` the ticks or the back quotes are
used for command substitution.. try this program once without the back quotes
around whoami and again with the backquotes and see the difference..

#!/bin/bash
echo “The current user is `whoami`”

Example 5:

Before we put date function into the script at the command prompt type the date
function and try different options with it like the following
1. date "+%Y-%h-%d %H:%M:%S " #Returns string "YYYY-MM-DD HH:MM:SS"
2. date +%Y%m%d%H%M%S #Returns string "YYYYMMDDHHMMSS"
3. date +%Y%m%d #Returns string "YYYYMMDD"
Try to put these functions in your script…Also observe the difference between
the quotes and no quotes..

Example 6:Using a while loop


alphabet="a b c d e" # Initialize a string
count=0 # Initialize a counter
while [ $count -lt 5 ] # Set up a loop control
do # Begin the loop
count=`expr $count + 1` # Increment the counter
position=`bc $count + $count - 1` # Position of next letter
letter=`echo "$alphabet" | cut -c$position-$position` # Get next
echo "Letter $count is [$letter]" # Display the result
done # End of loop

Example 7:
alphabet="a b c d e" # Initialize a string
count=0 # Initialize a counter
for letter in $alphabet # Set up a loop control
do # Begin the loop
count=`expr $count + 1` # Increment the counter
echo "Letter $count is [$letter]" # Display the result
done # End

mail (Gets you into the mail facility)


mail>n (Displays the next mail message starting with the most recent)
mail>q (Gets you out of the mail facility back to the operating system)
mail>d (Deletes the current message that you are reading)
mail>s <filename> (Saves the current message with header to a file specified
by <filename>)
mail>w <filename> (Writes the current message without header to a file
specified by <filename>)

1.find,
2.awk
3.cut
4.sed…
5.a full fledged script..

S-ar putea să vă placă și