Unix Basics (Lab-1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

UNIX BASICS

shell

a utility program that enables the user to interact with the UNIX operating system.
Commands entered by the user are passed by the shell to the operating system
which carries them out. The results are then passed back by the shell and
displayed on the user's display.
There are several shells available. The user may select which one they wish to
use.

shell prompt

a character at the start of the command line which indicates that the shell is ready
to receive your commands. The character is usually a '%' (percent sign) or a $
(dollar sign). It may be different on your system.

standard input

the source of information for a command. This is assumed to be the keyboard


unless input is redirected or piped from a file or another command.

standard output

the destination for information from a command. This is assumed to be the


terminal display unless output is redirected or piped to a file or another command.

Where are commands located?

Unix commands are executable binary files located in directories with the name bin (for
binary). Many of the commands that you use are located in the directory /usr/bin.
When you enter the name of a command the shell checks to see if it is a built-in
command. If it is not, it looks for the the binary file that the command name refers to in
each of the directories that are defined in the PATH environment variable.

COMMONLY USED UNIX COMMANDS

1. LISTING OF FILES
SYNTAX: ls –[options][filelist][directory list]
ls list visible files in the working directory.
ls -a list all enteries including filenames beginning with a period.
ls -l list visible files using long format ( includes modes, size, date etc).
ls / will display the name of the files in the root directory.
2. CREATING A FILE
SYNTAX : cat >filename
……………
……………
^D

3. SHOWING THE CONTENTS OF A FILE


SYNTAX : cat / more / page / head / tail filename

4. TO COPY THE CONTENTS FROM ONE FILE TO ANOTHER


SYNTAX: cp sourcefile targetfile

5. TO CHANGE THE NAME OF THE EXISTING FILE


SYNTAX : mv oldfile newfile

6. TO DELETE THE FILE/FILES


SYNTAX : rm [options][filelist]
rm – i [filelist] to prompt the user to confirm the action of deleting the
file.

7. DISPLAY THE ID’S OF THE USERS CURRENTLY LOGGED IN.


SYNTAX : who

8. TO CREATE A DIR IN THE CURRENT WORKING DIR.


SYNTAX: mkdir dirname

9. TO CHANGE THE SPECIFIED DIR TO BECOME THE CURRENT DIR.


SYNTAX: cd dirname

10. TO PLACE THE USER IN THE HOME DIR FROM ANYWHERE IN THE
FILE SYSTEM.
SYNTAX : cd

12. TO CHANGE THE WORKING DIR TO THE PARENT OF THE CURRENT


WORKING DIR.
SYNTAX : cd ..

13. PRINTING THE CURRENT WORKING DIRECTORY


SYNTAX: pwd
14. TO CHANGE THE MODE OF THE FILE.
SYNTAX : chmod new-mode filename
The new-mode is a 3-digit number computed by adding together the numeric
equivalents of the desired permissions, r(read), w(write) & x(execute).

User(owner) group others


r=4 r=4 r=4
w=2 w=2 w= 2
x=1 x =1 x=1
777 gives all the permissions to the owner , group, others.
000 denies all permissions.
644 gives read & write permission to the owner, read only to the group & the
others.

15. TO DISPLAY CURRENT DATE & TIME


SYNTAX: date

16. TO CLEAR THE SCREEN


SYNTAX: clear

17. OBTAINING ON-LINE HELP


SYNTAX: man command
man –k keyword

18. SETTING YOUR PASSWORD


SYNTAX: passwd

19. ELECTRONIC MAIL


SYNTAX: SENDING MAIL
mail userid
…………
…………
^D
RECEIVING MAIL
mail

20. COUNTING THE WORDS IN A FILE:


SYNTAX: wc filename

21. TERMINATING A PROCESS


SYNTAX: ^C
22. PAUSING OUTPUT
SYNTAX: ^S ……suspend terminal output
^Q ……resume terminal output

23. END OF INPUT


SYNTAX: ^D

24. LOGGING OUT


SYNTAX: ^D

25. DISPLAYING INFORMATION


SYNTAX: echo {arg}*

SCREEN EDITOR vi

SYNTAX vi file_name
vi operates in 2 modes – command mode & edit mode.
- In the command mode it execute the commands.
- In order to enter text, we have to press i or a to switch to edit mode.
- In order to execute the command after entering the text mode, press ESC
to switch to the command mode.

CURSOR-MOVEMENT COMMANDS
j moves the cursor down one line
k moves the cursor up one line
h moves the cursor left one space character
l moves the cursor right one space character
0 moves the cursor to the beginning of a line
$ moves the cursor to the end of a line

TEXT-DELETION COMMANDS
x deletes character under cursor
dw deletes from cursor to beginning of next word
dd deletes line containing cursor
d) deletes rest of sentence
d} deletes rest of paragraph

TEXT MOVING AND ALTERATION COMMANDS


u undo last command
yy save line(s) in temporary buffer
p put saved buffer after cursor
P put saved buffer before the cursor
SAVING THE TEXT AND QUITTING COMMANDS
Esc :w write file
Esc: q quit
Esc :wq save and quit
Esc :q! quit without saving

COMPILATION & EXECUTION OF PROGRAMS

COMPILING PROGRAMS
cc filename .c
compiles the c program in the file named filename.c and produces an executable
file named a.out.

cc filename.c –o filename
compiles the c program in the file named filename.c and produces an executable
file named filename.

EXECUTING PROGRAMS
./a.out
to execute the c program that has been compiled into the executable file named
a.out

EXERCISE:
1. Write a simple C program to display the message “Hello There!”.
2. Write a C program to read and display n numbers.
3. Write a C program to sort n numbers using any sorting algorithm.
4. Write a C program to search a number in the array.
REFERENCE:

DOS to UNIX Translation


A quick reference for users of DOS who are moving to UNIX. Look for a task or DOS
command and then select the UNIX command for further details.
To ... DOS UNIX

display list of files dir/w dir ls


dir ls -l
display contents of file type cat
display file with pauses type filename | more more
copy file copy cp
find string in file find grep
fgrep
compare files comp diff
rename file rename OR ren mv
delete file erase OR del rm
delete directory rmdir OR rd rmdir
change file protection attrib chmod
create directory mkdir OR md mkdir
change working directory chdir OR cd cd
get help help man
apropos
display date and time date, time date
display free disk space chkdsk df
print file print lpr
display print queue print lpq

******************************

You might also like