Thursday 8 December 2011

Commands in UNIX

Basic UNIX Commands and there description.

cat --- for creating and displaying short files
Syntax: cat filename
Ex: cat > filename
cat file1 >> file2

chmod --- change permissions for three classes of users: the user (u), group (g), or others (o) by entering
Syntax: chmod ugo filename

cd --- change directory
Syntax: cd directory
EX: From the subdirectory, /users/jones/memos, change to the directory, /users/jones/samples with the command,
cd /users/jones/samples

cp --- for copying files
Syntax: cp [options] old_filename new_filename
Ex: cp file1 file2

ftp --- connect to a remote machine to download or upload files
Syntax: ftp options hostname
options
-d enable debugging.
-g disable filename globbing.
-i turn off interactive prompts.
-v verbose on. show all responses from remote server.
ftp hostname by default will connect you to the system, you must have a login id to be able to transfer the files. Two types of files can be transferred, ASCII or Binary. bin at ftp> prompt will set the transfer to binary. Practice FTP by ftping to nic.funet.fi loggin in as anomymous with password being your e-mail address.

head --- display first 10 lines of a file.
Syntax: head [-count] filename
Ex: If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename.

ls --- see what files you have
Examples: ls List files in current directory
ls -l Lists files in “long” format

lpr --- standard print command (see also print )
Syntax: [ lpr -P(<--capital P) printername ]

more --- use to read files
Syntax: more filename
More is a command used to read text files. For example, we could do this:
% more poems
The effect of this to let you read the file "poems ". It probably will not fit in one screen, so you need to know how to "turn pages". Here are the basic commands:
q --- quit more
spacebar --- read next page
return key --- read next line
b --- go back one page
For still more information, use the command man more.


mkdir --- create directory
Syntax: [ mkdir directoryname ]

mv --- for moving and renaming files
Syntax:[ mv filename directory OR mv filename newfilename ]

pwd --- find out what directory you are in (will print your home directory on screen).
Syntax: [ pwd ]

rm --- remove a file
Syntax: [ rm filename ]

rmdir --- remove directory
Options:
rm -r directory_name will remove all files even if directory is not empty.
rmdir sandeep is how you use it to remove sandeep directory.
rmdir -p will remove directories and any parent directories that are empty.
rmdir -s will suppress standard error messages caused by -p.

sort --- sort file
Syntax: sort filename
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
You can check the contents of the file dict.sorted using cat , more , or emacs .
tail --- display last 10 lines of a file.
Syntax: tail [-count] filename
Ex: last 50 lines then use ----- tail -50 filename.

telnet --- log in to another machine( remote system)
Syntax:[ telnet hostname ]

wc --- count characters, words, lines in a file depending upon the option.
Syntax: wc [options] filename
Options
wc -l filename will print total number of lines in a file.
wc -w filename will print total number of words in a file.
wc -c filename will print total number of characters in a file.



tar --- create an archive, add or extract files
Use create compressed archives of directories and files, and also to extract directories and files from an archive. Example:
% tar -tvzf foo.tar.gz
displays the file names in the compressed archive foo.tar.gz while
% tar -xvzf foo.tar.gz
extracts the files.


rsh --- remote shell
Use this command if you want to work on a computer different from the one you are currently working on. One reason to do this is that the remote machine might be faster. For example, the command
% rsh solitude
connects you to the machine solitude. This is one of our public workstations and is fairly fast.


setenv --- set an environment variable
Ex:   % echo $PRINTER
labprinter
% setenv PRINTER myprinter
% echo $PRINTER
myprinter


ncftp --- especially good for downloading files via anonymous ftp.
Use ncftp for anonymous ftp --- that means you don't have to have a password.
% ncftp ftp.fubar.net
Connected to ftp.fubar.net
> get jokes.txt
The file jokes.txt is downloaded from the machine ftp.fubar.net.

print --- custom print command (see also lpr )
This is a moderately intelligent print command.
% print foo
% print notes.ps
% print manuscript.dvi
In each case print does the right thing, regardless of whether the file is a text file (like foo ), a postcript file (like notes.ps, or a dvi file (like manuscript.dvi. In these examples the file is printed on the default printer. To see what this is, do
% print
and read the message displayed. To print on a specific printer, do this:
% print foo jwb321
% print notes.ps jwb321
% print manuscript.dvi jwb321
To change the default printer, do this:
% setenv PRINTER jwb321

grep(Global search for Regular Expression and Print.) --- search file
Use this command to search for information in a file or files. For example, suppose that we have a file dict whose contents are
red rojo
green verde
blue azul
white blanco
black negro
Then we can look up items in our file like this;
% grep red dict
red rojo
% grep blanco dict
white blanco
% grep brown dict
%
Notice that no output was returned by grep brown. This is because "brown" is not in our dictionary file.
Grep can also be combined with other commands. For example, if one had a file of phone numbers named "ph", one entry per line, then the following command would give an alphabetical list of all persons whose name contains the string "Fred".
% grep Fred ph | sort
Alpha, Fred: 333-6565
Beta, Freddie: 656-0099
Frederickson, Molly: 444-0981
Gamma, Fred-George: 111-7676
Zeta, Frederick: 431-0987
The symbol "|" is called "pipe." It pipes the output of the grep command into the input of the sort command.
For more information on grep, consult
% man grep


date --- display date (Use this command to check the date and time.)
Syntax : % date
Fri Jan 6 08:52:42 MST 1995

echo --- echo argument
Ex: $ echo This is a test.

UNIX for Software Testers

WHAT IS UNIX?

UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.
UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren't covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session.

TYPES OF UNIX
There are many different versions of UNIX, although they share common similarities. The most popular varieties of UNIX are Sun Solaris, GNU/Linux, and MacOS X.


THE UNIX OPERATING SYSTEM

The UNIX operating system is made up of three parts; the kernel, the shell and the programs.

The kernel

The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

The shell


The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).
The adept user can customise his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.
The tcsh shell has certain features to help the user inputting commands.
Filename Completion - By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.
History - The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.

FILES AND PROCESSES

Everything in UNIX is either a file or a process.
A process is an executing program identified by a unique PID (process identifier).
A file is a collection of data. They are created by users using text editors, running compilers etc.
Examples of files:
a document (report, essay etc.)
the text of a program written in some high-level programming language
instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);
a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files.


THE DIRECTORY STRUCTURE

All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )



Commands in UNIX