15
Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4010: Operating Systems Lab Lab # 7 Shell Commands Eng. Haneen El-Masry November, 2013

Lab # 7 Shell Commands - site.iugaza.edu.ps

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab # 7 Shell Commands - site.iugaza.edu.ps

Islamic University of Gaza Faculty of Engineering

Department of Computer Engineering ECOM 4010: Operating Systems Lab

Lab # 7

Shell Commands

Eng. Haneen El-Masry

November, 2013

Page 2: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

2

Shell

Shell is a special Linux component that acts as interface between user and kernel.

Shell accepts user commands in English and passes them to the kernel to

execute.

Shell is a command language interpreter that executes commands read from

keyboard or from a file.

Shell is not part of the kernel; it uses it to execute programs.

Shell Types

BASH (Bourne-Again Shell): it is a freeware shell and most common shell in Linux.

CSH (C Shell): The C shell's syntax and usage are very similar to the C

programming language.

KSH (Korn Shell): it was designed to take the best features of the Bourne shell

and the C shell and extend them further.

TCSH: It’s a fairly popular shell in some circles, but no major Linux distributions

make it the default shell.

ZSH (Z shell): it takes shell evolution further than the Korn Shell, incorporating

features from earlier shells and adding still more.

BSH (Bourne shell): it upon which bash is based also goes by the name bsh. It’s

not often used in Linux.

Linux has several other types of shells, each with its own prompt. A shell prompt marks

the beginning of the command line. By default, the BASH shell has a dollar sign ($)

prompt.

To find all available shells in your system, use the following command:

cat /etc/shells

Page 3: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

3

Linux File System Structure

For more details, Revise Lab 1.

Page 4: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

4

Types of Users in Linux

The root user: a special administrative account. Also called the super user. The

root has near complete control over the system and a nearly unlimited capacity

to damage it!

Normal (unprivileged) user: potential to do damage is more limited.

Paths

A path is the general form of a filename or of a directory name, specifies a unique

location in a file system. A path points to a file system location by following the

directory tree hierarchy expressed in a string of characters in which path components,

separated by a delimiting character (/), represent each directory.

Systems can use either absolute or relative paths:

A full (absolute) path: is a path that points to the same location on one file

system regardless of the working directory or combined paths. It is usually

written in reference to a root directory (/).

A relative path: is a path relative to the working directory of the user or

application, so the full absolute path will not have to be given.

Example:

If Lab7 directory is in Labs directory that is in haneen’s Home, then the full path of Lab7

is:

/home/haneen/Labs/Lab7

But if you are currently in haneen’s home, then the relative path of Lab7 is:

Labs/Lab7

Converting from relative path to absolute path

There are two ways to convert relative path to absolute path and they are:

readlink -f [relative_path]

realpath [relative_path]

Page 5: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

5

Note:

If realpath command isn’t installed, you can install it by writing this:

sudo apt-get install realpath

Structuring Commands

Commands have the following syntax:

command options arguments

Each item is separated by a space.

Options modify a command's behavior.

Single-letter options usually preceded by -.

Can be passed as -a -b -c or –abc.

Full-word options usually preceded by --. Example: --help

Arguments are filenames or other data needed by the command.

Multiple commands can be separated by ; .

Page 6: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

6

Simple Commands

Command Description

whoami Display The Name Of The Current User.

pwd Print The Name Of Current Working Directory.

date Display Date And Time.

cal Display Calendar.

clear Clear The Terminal Screen.

init 0 Shutdown The System.

init 6 Reboot The System.

Changing Directories

To change a directory to a specified directory: cd full/relative path of Dir

To change a directory one level up: cd ..

To change a directory to current user home: cd

To change a directory to your previous working directory: cd –

Page 7: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

7

Creating Directories

mkdir creates directories.

To make a new directory, change your directory where you want to add a new directory, then execute the following command:

mkdir DirName

Or execute the following command directly:

mkdir full/relativePathOfNewDir

Listing Directory Contents

ls lists the contents of the current directory or a specified directory.

ls Options:

-a : to include hidden files.

-l : display extra information using long listing format.

-R : recursive through directories.

-c : sort by time.

Page 8: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

8

To list the content of current directory:

ls

To list the contents of a specified directory:

ls full/relative path of dir

Page 9: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

9

Removing Directories

rmdir removes empty directories.

rmdir full/relative PathOfDir

rm -r recursively removes directory trees.

rm -r full/relative PathOfDir

Creating, Editing and Displaying Files

touch create empty files or update file.

touch full/relativePathOfNewFile

We can use this command in multiple ways:

To create three files named : file1, file2, file3, We can execute any one of the following:

touch file1 file2 file3

touch file{1,2,3}

for i in $(seq 1 3) ; do touch file$i ; done

Page 10: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

10

cat overwrite or append to a file.

cat > full/relativePathOfFile (overwrite the file)

cat >> full/relativePathOfFile (Append to the file)

After finishing, press Ctrl +D to save and exit the file.

less Display the contents of a file on the screen.

less full/relativePathOfFile

Press q to exit the file.

Page 11: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

11

GUI Files Create, editing and displaying

Gedit create, edit and display files.

gedit full/relativePathOfFile

Removing Files

rm remove files.

rm full/relativePathOfFile

rm –i full/relativePathOfFile Interactive Removing

Copying Files and Directories

cp copy files and directories.

cp PathOfSource PathOfDestination

Notes:

If the destination is a directory, the copy is placed there.

If the destination is a file, the copy overwrites the destination.

If the destination does not exist, the copy creates the destination.

Page 12: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

12

Moving and Renaming Files and Directories

mv move and/or rename files and directories.

mv PathOfSource PathOfDestination

If the destination does not exist, the source will be renamed.

Page 13: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

13

Determining File Content

Files can contain many types of data.

Check files type with file before opening to determine appropriate command or

application to use.

file PathOfFile

Printing

echo Display a line of a text

Page 14: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

14

Common special Characters

Character Purpose

~ Shorthand for the current user’s home directory.

\ Ignore next character (esc character).

/ Directory separator.

$ Variable. It Precedes any variable.

? Single-character wildcard.

* None-to-many wildcard.

&& If cmd1 exits 0 (Success), then do cmd2.

| Pipe output to a program.

|| If cmd1 exits 1 (Fails), then do cmd2.

; Execute multiple commands.

[] Ranges of letters/numbers.

> Redirect output to a file.

< Redirect input to a program.

Page 15: Lab # 7 Shell Commands - site.iugaza.edu.ps

Operating Systems Lab Eng.Haneen

15

Exercise

1- Make the following tree using shell commands described in the Lab.

2- Display the content of each file on the screen.

3- Copy File4 into DIR3.

4- Move File5 to DIR1 and rename it to MovedFile.

Best Wishes