net prog1-shell2-3

28

Upload: maria-sawaby-nazehat

Post on 13-Apr-2017

133 views

Category:

Technology


0 download

TRANSCRIPT

Technical Foundation of Computer Science 5

Network Programming I

Maria Sawaby

Department of Communication and Operating System

April 14, 2015

Net-Dep (Network) Net-Prog I April 14, 2015 1 / 28

Contents

1 Monitoring Programs

2 killing processes

3 Mounting Disk Space

4 Working with Data Files

5 Searching for data

6 Compressing data

bzip2 utility

gzip utility

zip utility

tar utility

7 Summary

Net-Dep (Network) Net-Prog I April 14, 2015 2 / 28

Monitoring ProgramsPeeking at the processes

When a program runs on the system, it's referred to as a process

ps command is used to work with processes

it only shows the processes running on the current terminal

to see everything running on the system:

$ ps -ef

Net-Dep (Network) Net-Prog I April 14, 2015 3 / 28

Net-Dep (Network) Net-Prog I April 14, 2015 4 / 28

Monitoring ProgramsPeeking at the processes

This example uses two parameters, the -e parameter, which shows allof the processes running on the system, and the -f parameter, whichexpands the output to show a few useful columns of information

I UID: The user responsible for launching the processI PID: The process ID of the processI PPID: The PID of the parent process (if a process is started by

another process)I C: Processor utilization over the lifetime of the processI STIME: The system time when the process startedI TTY: The terminal device from which the process was launchedI TIME: The cumulative CPU time required to run the processI CMD: The name of the program that was started

The -H parameter organizes the processes in a hierarchical format

try: $ps -efH

Net-Dep (Network) Net-Prog I April 14, 2015 5 / 28

Monitoring ProgramsReal-time process monitoring

The top command displays process information similarly to the ps

command, but it does it in real-time mode

Net-Dep (Network) Net-Prog I April 14, 2015 6 / 28

Monitoring ProgramsReal-time process monitoring

The top command shows the following information:

PID: The process ID of the process

USER: The user name of the owner of the process

PR: The priority of the process

NI: The nice value of the process

VIRT: The total amount of virtual memory used by the process

RES: The amount of physical memory the process is using

SHR: The amount of memory the process is sharing with other

processes

S: The process status (D = interruptible sleep, R = running, S =

sleeping, T = traced or stopped, or Z = zombie)

%CPU: The share of CPU time that the process is using

%MEM: The share of available physical memory the process is using

TIME+: The total CPU time the process has used since starting

COMMAND: The command line name of the process (program

started)Net-Dep (Network) Net-Prog I April 14, 2015 7 / 28

killing processes

The kill command allows you to send signals to processes based on

their process ID (PID)

To send a process signal, you must either be the owner of the process

or be logged in as the root user

e.g the process ID of Firefox is 10959, the command below kills the

process and closes the Firefox

kill 10959

The killall command can be used to end processes based on their

names rather than PID

Net-Dep (Network) Net-Prog I April 14, 2015 8 / 28

Killing processes

Caution!

Be extremely careful using the killall command when logged in as the root

user. It's possible to stop important system processes. This could lead to a

damaged �lesystem

Net-Dep (Network) Net-Prog I April 14, 2015 9 / 28

Mounting Disk SpaceMounting Media

Before you can use a new media disk on your system, you need to

mount it

the command used to mount media is called mount

By default, the mount command displays a list of media devices

currently mounted on the system

There are four pieces of information the mount command provides:1 The device location of the media2 The mount point in the virtual directory where the media is mounted3 The �lesystem type4 The access status of the mounted media

e.g.

$ mount/dev/sda8 on / type ext4 (rw)

Net-Dep (Network) Net-Prog I April 14, 2015 10 / 28

Mounting Disk SpaceMounting Media

The basic command for manually mounting a media device is:

mount -t type device directory

# mount -t vfat /dev/sdb1 /media/maria/new/

# umount /media/maria/new

Net-Dep (Network) Net-Prog I April 14, 2015 11 / 28

Mounting Disk SpaceMounting Media

to mount a .iso image to a location inside your system without havingto burn it to a CD:

I use -o parameter which lets you use other options like "loop" to mountan .iso �le

# mount -t iso9660 -o loop .iso-image dir

Net-Dep (Network) Net-Prog I April 14, 2015 12 / 28

Mounting Disk SpaceThe df Command

Sometimes you need to see how much disk space is available on an

individual device

$ df

using it with -h will show the disk space with human readable form

Net-Dep (Network) Net-Prog I April 14, 2015 13 / 28

The du command

The du command shows the disk usage for a speci�c directory (by

default, the current directory)

you can use the following other options and many more:I -h Print sizes in human-readable formI -m print bloc sizes of 1MI -l count sizes many times if hard linkedI �si Like -h Use power of 1000 instead of 1024

Net-Dep (Network) Net-Prog I April 14, 2015 14 / 28

Working with Data FilesSorting Data

The Linux system provides several command line tools to help us

manage large amounts of data

The sort command does what it says - it sorts data

Net-Dep (Network) Net-Prog I April 14, 2015 15 / 28

Working with Data FilesSorting Data

The -n option is used with sort command to sorting numbers based

on numerical values

$ sort -n �le2

-M is used for month sort $ sort -M �le3

The -n parameter is great for sorting numerical outputs, such as the

output of the du command: $ du -sh * | sort -nr

Net-Dep (Network) Net-Prog I April 14, 2015 16 / 28

Searching for data

Often in a large �le you have to look for a speci�c line of data

somewhere in the middle of the �le, grep command does it easily

format of the command:

grep [options] pattern [�le]

-n option is used to show line number

Net-Dep (Network) Net-Prog I April 14, 2015 17 / 28

Searching for data

-e option is used to �nd more than one patter

If you just need to see a count of how many lines contain the

matching pattern, use the -c parameter

$ grep [tf] �le2 will work like using -e option

Net-Dep (Network) Net-Prog I April 14, 2015 18 / 28

Compressing Data

The zip utility allows you to easily compress large �les (both text and

executable) into smaller �les that take up less space

Linux contains several �le compression utilities:I The bzip2 utilityI The gzip utilityI The zip utilityI Archiving data (tar) its the standard utility used in the linux world

Net-Dep (Network) Net-Prog I April 14, 2015 19 / 28

bzip2 utility

The bzip2 utility is a relatively new compression package that is

gaining popularity, especially when compressing large binary �les

The utilities in the bzip2 package are:I bzip2 for compressing �lesI bzcat for displaying the contents of compressed text �lesI bunzip2 for uncompressing compressed .bz2 �les

Net-Dep (Network) Net-Prog I April 14, 2015 20 / 28

bzip2 examples

Net-Dep (Network) Net-Prog I April 14, 2015 21 / 28

gzip utility

By far the most popular �le compression utility in Linux is the gzip

utility

This package includes the �les:I gzip for compressing �lesI zcat for displaying the contents of compressed text �lesI gunzip for uncompressing �les

Net-Dep (Network) Net-Prog I April 14, 2015 22 / 28

gzip examples

Net-Dep (Network) Net-Prog I April 14, 2015 23 / 28

zip utility

The power of the zip utility is its ability to compress entire directories

of �les into a single compressed �le

This makes it ideal for archiving entire directory structures

Net-Dep (Network) Net-Prog I April 14, 2015 24 / 28

tar utility

The format of the tar command is:

tar function [options] object1 object2 ...

This creates an archive �le called test.tar containing the contents of

both the test directory and the test2 directory

tar -cvf test.tar test/ test2/

This lists (but doesn't extract) the contents of the tar �le: test.tar

tar -tf test.tar

This extracts the contents of the tar �le test.tar

tar -xvf test.tar

TIP

If you download open source software, often you'll see �lenames that end in

.tgz. These are gzipped tar �les, and can be extracted using the command

tar -zxvf �lename.tgz.Net-Dep (Network) Net-Prog I April 14, 2015 25 / 28

Net-Dep (Network) Net-Prog I April 14, 2015 26 / 28

Summary

The ps and top commands are vital in determining the status of the

system, allowing you to see what applications are running and how

many resources they are consuming

The mount command allows you to mount a physical storage device

into the Linux virtual directory structure. To remove the device, use

the umount command.

The sort utility easily sorts large data �les to help you organize data

the grep utility allows you to quickly scan through large data �les

looking for speci�c information

The Linux tar utility is a popular way to archive directory structures

into a single �le that can easily be ported to another system

Net-Dep (Network) Net-Prog I April 14, 2015 27 / 28

Net-Dep (Network) Net-Prog I April 14, 2015 28 / 28