Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

UNIX

Developed by Kenneth Lane Thompson, Dennis Ritchie and 3 others. UNIX was written using
C & assembly language. Unix is not free to use. However, some UNIX versions are free for
development use.

 Multi-user system where same resources can be shared by different users.


 Multi-tasking wherein each user can execute many processes at the same time.
 Has built-in networking functions so that fifferent users can easily exchange information.

Kernel : It is the core of the UNIX operating system. It allocates the time and memory to
programs and handles the communications that is response to the system calls.

Shell : acts as an interface to UNIX system.

SYSTEM CALLS

1. ls(List) : lists the contents of current working directory both files and folders.
ls –a : list files that are normally hidden.
ls –l : symbolic permissions of the files & folders
2. mkdir (make directory) : creates new directory.
3. cd (change directory) : changes the current working directory to ‘directory’.
%cd . --> current directory
%cd .. --> parent directory
4. pwd (print working directory)
5. cp (copy) : $cp file1 file2 which makes a copy of file1 in the current working directory
and calls it file2.
6. mv ( move) : $mv file1 file2 where the contents of file is moved to file2.
$mv new.txt renamefile.txt where name of file is renamed as new.txt
7. rm (remove) : $rm file.txt , to delete a file
8. rmdir(remove directory) : $rmdir dir , to remove a directory (make sure it is empty first)
9. clear(clear screen) : will clear the terminal window of the previous commands.
10. cat(concatenate) : used to display the contents of a file on the screen.
11. touch : create a new file or upload its timestamp.
12. less : wrires the contents of a file onto the screen a page at a time. Press [space-bar] to
display the another page and type [q] to quite reading. Less is used in preference to cat
for long files
13. head : writes first 10 lines of a file to the screen.
$head data1.txt --> 10 lines
$head -5 data1.txt --> first 5 lines
14. tail : writes last 10 lines of a file to the screen.
$tail data1.txt --> 10 lines
$tail -5 data1.txt --> last 5 lines
15. grep : Global search for the regular expression. Grep command is a filter that is used to
search for lines matching a specified pattern and print the matching lines to standard
output.
Syntax : $grep –[options] “pattern” fil;ename.
Options :
-i : performs a case-insensitive search
-n : displays lines containing the pattern along with the line number.
-v : displays the lines not containing the specified pattern.
-c : displays the count of matching patterns.
-w : match whole word.
-l : displays the list of files that matches the pattern present in files.
1. Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are used to
anchor the pattern to the start of the line, and to the end of the line respectively.
Example : “^Name” matches all lines that start with the string “Name”. The strings “\<”
and “\>” are used to anchor the pattern to the start and end of a word respectively.
2. Wildcard Character: ‘.’ Is used to match any character.
Example : “^.$” will match all lines with any single character.
3. Escaped Characters: Any of the special characters can be matched as a regular
character by escaping them with a ‘\’.
Example: “\$\*” will match the lines that contain the string “$*”
4. Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a range of
characters to be matched.
Example: “[aeiou]” will match all lines that contain a vowel. A hyphen can be used while
specifying a range to shorten a set of consecutive characters. E.g. “[0-9]” will match all
lines that contain a digit. A carat can be used at the beginning of the range to specify a
negative range. E.g. “[^xyz]” will match all lines that do not contain x, y or z.

Examples :
 Match all lines that start with ‘hello’. E.g: “hello there”
$ grep “^hello” file1

 Match all lines that end with ‘done’. E.g: “well done”
$ grep “done$” file1

 Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
$ grep “[a-e]” file1

 Match all lines that do not contain a vowel


$ grep “[^aeiou]” file1

 Match all lines that start with a digit following zero or more spaces. E.g: “ 1.” or
“2.”
$ grep “ *[0-9]” file1

 Match all lines that contain the word hello in upper-case or lower-case
$ grep -i “hello”

16. Wc(word count) : used to find out no of lines, word count, byte, charcters count in the
files specified in the file arguments.
$cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
$wc state.txt
5 7 63 state.txt
$wc state.txt capital.txt
5 7 63 state.txt
5 5 45 capital.txt
10 12 108 total

17. crontab - Schedule a command to run at a later time.

* * * * * Command_to_execute
-���-
|||||
| | | | +�� Day of week (0�6) (Sunday=0) or Sun, Mon, Tue,...
| | | +���- Month (1�12) or Jan, Feb,...
| | +����-� Day of month (1�31)
| +������� Hour (0�23)
+��������- Minute (0�59)

EXAMPLES

To run /usr/bin/sample.sh at 12.59 every day and supress the output


59 12 * * * simon /usr/bin/sample.sh > /dev/null 2>&1
To run sample.sh everyday at 9pm (21:00)
0 21 * * * sample.sh 1>/dev/null 2>&1
To run sample.sh every Tuesday to Saturday at 1am (01:00)
0 1 * * 2-7 sample.sh 1>/dev/null 2>&1
To run sample.sh at 07:30, 09:30 13:30 and 15:30
30 07,09,13,15 * * * sample.sh
Appending To A File :

We use > symbol to redirect the output of a command.

$cat > users

abc

xyz

$cat users

abc

xyz

The form >> appends standard output to a file.

$cat >> users

pqr

def

$cat users

abc

xyz

pqr

def

$who : is used to get information about user currently logged in to the system

$top : displays a line status of current processes.

$bg : continue running a job that was previously suspended ( using ctrl –Z) in the back ground

$fg : bring a previous back ground job to foreground.

$cal : displays the calendar.

Syntax : cal [[month]year]

E.g: display the calendar for April 2018

$cal 4 2018
$date : displays the system date and time

$date +$d/$m/$y

$history : show lists of previous commands that were entered.

$whoami : displays the user id of the currently logged in user.

$man : interface for working with the online reference manuals.

Syntax : man [-s section] item E.g: $man cat

$diff : compares the contents of two files and displays the differences. Suppose a file1, you edit
some part of it and save it as file2. To see differences

$diff file1 file2

Lines beginning with < denotes file1

Lines beginning with > denotes file2

$find : searches through the directories for files and directories with a given name, date, size

E.g : To search for all files with extension .txt, starting at current directory (.) & working through
all sub-directories

$find .-name “*.txt” –print

E.g : To find files over 1MB in size and display result

$find .-size+1M –ls

UNIX PROCESS CONTROL COMMANDS :

Every time when a command or program is run, a new process is created. The process is active
for as long as the program is in an active state.

Each time a new process is created, the kernel assigns a unique identification number called PID
(process identification number) which lies b/w 0 – 32,767. Other properties of processes include
their PPID(parent PID), TTY(controlling terminal from where they were launched), UID(user id
that owns this process) and GID(group that is associated with the process)

1) Foreground process : A process that is launched from a terminal and disallows


further commands until it completes. In such a process, the stdin and stdout are
attached to the terminal by default.
2) Background process : It is a process that was launched from a terminal, but is run
in the background, thus allowing further commands while it runs. In such a
process, the stdin and stdout should typically be redirected so they don’t interfere
with other foreground processes.
3) Daemon process : It is a process that is not associated with a terminal session.
Such processes are usually launched for system services such as networking and
printing.

Control –C : terminates the currently running foreground process.

Control –D : terminates the currently login or terminal session.

Control –Z : suspends the currently running foreground process to the background.

Command Description :

bg : to send a process to the background.

f g : to run a stopped process in the foreground.

top : details on all active processes.

ps : status of processes running for a user.

ps pid : status of particular id of a process

kill id : kills a process.

nice : starts a process with a given priority.

renice : changes priority of an already running process.

df : gives free hard disk space on your system.

free : gives free ram on your system.

FILE PERMISSIONS : UNIX is a multi-user operating system, so it has security to prevent


people from accessing each other’s confidential files.

Mode : d r w x rwx rwx


User group others

The first character will almost always be either a ‘-‘, which means it’s a file, or a ‘d’, which means it’s a
directory.

The next nine characters (rw-r–r–) show the security


Read, write, execute and –

The ‘r’ means you can “read” the file’s contents.


The ‘w’ means you can “write”, or modify, the file’s contents.
The ‘x’ means you can “execute” the file. This permission is given only if the file is a program.
If any of the “rwx” characters is replaced by a ‘-‘, then that permission has been revoked.

User, group and others

user – The user permissions apply only the owner of the file or directory, they will not impact the
actions of other users.
group – The group permissions apply only to the group that has been assigned to the file or
directory, they will not effect the actions of other users.
others – The others permissions apply to all other users on the system, this is the permission
group that you want to watch the most.

E.g : For granting all permissions -> chmod ugo+rwx file.txt or chmod 777 file.txt
E.g : For revoking all permissions -> chmod ugo-rwx file.txt

You might also like