0% found this document useful (0 votes)
40 views37 pages

Lab - 7-OS

The document discusses various Linux utility programs like grep, sort, gzip and more. It also covers commands like less, more, cat and pipes. The document then talks about C compilers and provides steps to write a simple C program using nano editor, gcc compiler and executing the output file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views37 pages

Lab - 7-OS

The document discusses various Linux utility programs like grep, sort, gzip and more. It also covers commands like less, more, cat and pipes. The document then talks about C compilers and provides steps to write a simple C program using nano editor, gcc compiler and executing the output file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Lab7

Utility Programs

T / Ebtisam ALselwi 1
Function
Command

grep Search for a pattern in a file or program output.

Sort Sort a file or program output. Example: sort mp3files.txt

gzip zip a file

Pipe used to combine between two or more commands in a single


command .

T / Ebtisam ALselwi 2
grep Command

std@ubuntu:~$ grep Linux sample.txt

Search for lines containing the word Linux

Linux: is a Unix-like operating system that


std@ubuntu:~$

T / Ebtisam ALselwi 3
grep Command

std@ubuntu:~$ grep –v Linux sample.txt

Search for lines that do not contain the word Linux

was designed to provide personal computer


users a free or very low-cost operating system
comparable to traditional and usually
more expensive Unix systems,
developed by Linus Torvalds.

T / Ebtisam ALselwi 4
grep Command

std@ubuntu:~$ grep ^Linux sample.txt

To search for lines beginning with the word "Linux"

Linux: is a Unix-like operating system that


std@ubuntu:~$

T / Ebtisam ALselwi 5
grep Command

std@ubuntu:~$ grep Linux $ sample.txt

To find the lines that end with the word "Linux"

operating system Linux

T / Ebtisam ALselwi 6
sort command Display content of file

std@ubuntu:~$ cat sample1.txt

ebtisam
amal
sahar
kawkab
manal
hayam
zinab

T / Ebtisam ALselwi 7
Sort a file

std@ubuntu:~$ sort sample1.txt

amal
ebtisam
hayam
kawkab
manal
sahar
zinab

T / Ebtisam ALselwi 8
Sort a file inverse

std@ubuntu:~$ sort -r sample1.txt

zinab
sahar
manal
kawkab
hayam
ebtisam
amal

T / Ebtisam ALselwi 9
Move content of sample 1 To sample in order with delete content of
sample
std@ubuntu:~$ sort sample1.txt>sample.txt

std@ubuntu:~$ cat sample1.txt


ebtisam
std@ubuntu:~$ cat sample1.txt amal
computer sahar
kawkab
manal
hayam
zinab

Befor After

T / Ebtisam ALselwi 10
Marge content of sample and sample1 To sample2 in order

std@ubuntu:~$ sort sample.txt sample1.txt


>sample2.txt
std@ubuntu:~$ cat sample2.txt
amal
amal
ebtisam
ebtisam
hayam
hayam
kawkab
kawkab
manal
manal
sahar
sahar
zinab
zinab
T / Ebtisam ALselwi 11
Zip sample 1 and sample

std@ubuntu:~$ gzip sample.txt sample1.txt

T / Ebtisam ALselwi 12
unZip sample 1 and sample

std@ubuntu:~$ gzip -d sample.txt sample1.txt

T / Ebtisam ALselwi 13
Piping and Redirection

T / Ebtisam ALselwi 14
Page by page : press space key

Line by line :: press Enter key

Function
Command

more Display a file, or program output one page at a time(page by


page or line by line), display content in same page .
. Examples:
more mp3files.txt
ls -la | more
less Display a file, or program output (page by page or line by line),
display content in other page .

An improved replacement for the “more” command. Allows you to


scroll backwards as well as forwards

T / Ebtisam ALselwi 15
To more : press space key
std@ubuntu:~$ more /etc/*.conf To exit :press q key
Display content in same page Can not use pgup,pgdn

T / Ebtisam ALselwi 16
To more : press space key
std@ubuntu:~$ more /etc/*.conf To exit :press q key
Display content in same page Can not use pgup,pgdn

T / Ebtisam ALselwi 17
To more : press space key
std@ubuntu:~$ less /etc/*.conf To exit :press q key
Display content in another page Can use pgup,pgdn

T / Ebtisam ALselwi 18
To more : press space key
std@ubuntu:~$ less /etc/*.conf To exit :press q key
Display content in another page Can use pgup,pgdn

T / Ebtisam ALselwi 19
Pipe command
std@ubuntu:~$ ls /etc/ | less

T / Ebtisam ALselwi 20
Pipe command(con…)

std@ubuntu:~$ cat /etc/passwd | grep ebtisam3

ebtisam3:x:1003:1006::/home/ebtisam3:

T / Ebtisam ALselwi 21
Pipe command(con…)

std@ubuntu:~$ cat /etc/passwd | grep home

syslog:x:100:103::/home/syslog:/bin/false
saned:x:108:116::/home/saned:/bin/false
std:x:1000:1000:ebtisam,,,:/home/std:/bin/bash
ebtisam:x:1001:1001:,,q,exit:/home/ebtisam:/bin/bash
ebtisam3:x:1003:1006::/home/ebtisam3:
newuser2:x:1050:1050:newuser2,123,1452,4521,4521:/home/lab:/bin/bash
user:x:1005:1011:user,4521,1254,125,4152:/home/user:/bin/bash
user11:x:1082:1005::/home/uh2:

T / Ebtisam ALselwi 22
Redirection

1. Redirecting to a File : ls > test.txt

2. Redirecting from a File : wc -c < test.txt

std@ubuntu:~$ wc -c < test.txt

188

T / Ebtisam ALselwi 23
Redirection(con…) put content of passwd file into
output.txt file
std@ubuntu:~$ cat /etc/passwd > output.txt
std@ubuntu:~$ cat output.txt

T / Ebtisam ALselwi 24
Redirection(con…) put content of passwd file into
output.txt file
std@ubuntu:~$ cat /etc/passwd > output.txt
std@ubuntu:~$ cat output.txt

T / Ebtisam ALselwi 25
T / Ebtisam ALselwi 26
Redirection(con…) Add content of hosts file into
output.txt file
std@ubuntu:~$ cat /etc/hosts >> output.txt
std@ubuntu:~$ cat output.txt

T / Ebtisam ALselwi 27
C compiler

Function
Command

whereis gcc Display the path of compiler on Linux .


Or which gcc
gcc -- version Display the version number of compiler on Linux .

T / Ebtisam ALselwi 28
Verify compiler is exist
C compiler(con…)
Display the path of compiler on
Linux
std@ubuntu:~$ whereis gcc

gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

std@ubuntu:~$ which gcc

/usr/bin/gcc

T / Ebtisam ALselwi 29
Display the version number of
C compiler(con…) compiler on Linux

std@ubuntu:~$ gcc --version

gcc (Ubuntu 4.9.1-16ubuntu6) 4.9.1


Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

T / Ebtisam ALselwi 30
C compiler (con…) The Steps

1. Open file in nano editor


nano f.c
2. Write program and save it:
3. Compile f.c with g++ or gcc :
gcc f.c -o f
4- Execute it:
./f

T / Ebtisam ALselwi 31
Nano editor :
open file in nano editor
std@ubuntu:~$ nano f.c

T / Ebtisam ALselwi 32
Write program in C language

#include<stdio.h>
int main()
{
printf("Hello Word\n");
return 0;
}

1- Ctrl + O Save
2- Press Enter Key
3- Ctrl + X Exit from Nano editor
T / Ebtisam ALselwi 33
T / Ebtisam ALselwi 34
Compile f.c with g++ or gcc

std@ubuntu:~$ gcc f.c -o f

T / Ebtisam ALselwi 35
Execute f.out

std@ubuntu:~$ ./f Here the "./" means the exe


file is under the current dir

Hello Word

T / Ebtisam ALselwi 36
End…
T / Ebtisam ALselwi 37

You might also like