<<

Islamic University of Gaza

Faculty of Engineering

Department of Computer Engineering

ECOM 4010: Operating Systems Lab

Eng. Mahmoud A. Ammar Eng. Hala O. Abu Radi

Shell Commands

Lab. 02

2011-2012 ECOM 4310 Lab.2

Some Simple Commands

Simple Commands Display the name of the current user print the name of current working date display date and display calendar Clear the terminal screen init 0 the system init 6 Reboot the system Concatenate files and print on the standard output Display a line of a text Number lines of files

File and Directory Names

. Names may be up to 255 characters. . All characters are valid, except the forward slash. . It may be unwise to use certain special characters in or directory names. . Some characters should be protected with quotes when referencing them. . Names are case-sensitive.

Listing Directory Contents

. Lists the contents of the current directory or a specified directory. . Usage: [options] [files_or_dirs]

1

Operating System ECOM 4310 Lab.2

. Example: ls -a (--all, include hidden files). ls -l (display extra information using long listing format). ls -R (recurse through directories). ls -c ( by time).

Changing Directories changes directories: . To an absolute or relative path: cd /etc/squid/ . To a directory one level up: cd .. . To your home directory: cd . To your previous working directory: cd –

Creating and Removing Directories

. creates directories. . Usage: mkdir [options] [name of dir] . removes empty directories. . Usage: rmdir [options] [name of dir] . Example: rmdir -p dir1/dir2/dir3 (-p : --parent).

. -r [dir name] recursively removes directory trees. . NOTE: we can use another options with these commands like:  –v: (--verbose) explain what is being done.  –f: (--force) do not ask any thing.

2

Operating System ECOM 4310 Lab.2

Creating and Removing Files

. touch create empty files or update file. . We can use this command in multiple ways:  To create three files named : file1, file2, file3 :

1. touch file1 file2 file3 2. touch file{1,2,3} 3. for i in $( 1 3) ; do touch file$i ; done . rm - remove files. . Usage: rm [options] ... . Example: rm -i file (interactive) rm -r directory (recursive) rm -f file (force)

Copying Files and Directories

. - copy files and directories. . Usage: cp [options] file destination . than one file may be copied a time if the destination is a directory: cp [options] file1 file2 destination . Note:  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.

Moving and Renaming Files and Directories

. - move and/or rename files and directories

3

Operating System ECOM 4310 Lab.2

. Usage: mv [options] file destination . More than one file may be moved at a time if the destination is a directory: mv [options] file1 file2 destination . If the destination does not exist, the source will be renamed. Determining File Content

. Files can contain many types of data. . Check files with file before opening to determine appropriate command or application to use. . Usage: file [options]

Changing Your Password

. To change your password using GNOME, navigate to Application- >Preferences-> and then click Password. . To change your password from a terminal: < username>

Changing Identities

. - creates new shell as root. . su - creates new shell as . . id shows information on the current user.

Getting

. Don't try to memorize everything! . Many levels of help:  whatis  command --help

4

Operating System ECOM 4310 Lab.2

 man and  /usr/share/doc/  Red Hat documentation

The whatis Command

. Displays short descriptions of commands . Uses a database that is updated nightly . Often not available immediately after . $ whatis cal cal (1) - displays a calendar

The --help Option

. Displays usage summary and argument list . Used by , but not all, commands . $ date --help Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date. ...argument list omitted...

The man Command

. Provides documentation for commands . Almost every command has a man "page" . Pages are grouped into "chapters" . Collectively referred to as the Manual . man

5

Operating System ECOM 4310 Lab.2

The info Command

. Similar to man, but often more in-depth . Run info without args to list all page . info pages are structured like a web site . Each page is divided into "nodes" . Links to nodes are preceded by * . info [command]

Special Characters in the Shell

There are numerous characters with special meaning to the shell, a smattering of are defined in the table below.

Common special Characters Character Purpose Example Shorthand for the current ~ ~/.bashrc user’s home directory Ignore next character \ echo $PRD is \$5 (esc character) / Directory separator cd /home/rbrunson Variable, precedes any $ echo $VAR var ? Single-character wildcard ls *.t?t Single (absolute) ‘ echo „Cost: $100‟ quotation mark Back tick, used for ` echo `date` substitution Double (soft) quotation “ echo “Cost: $VAR” marks * None-to-many wildcard ls dar*.* & Background a job in shell mozilla & If cmd1 exits 0 && cmd1 && cmd2 (Success), then do cmd2

6

Operating System ECOM 4310 Lab.2

| Pipe output to a program ls –l | If cmd1 exits 1 (Fails), || cmd1 || cmd2 then do cmd2 Execute multiple ; cmd1 ; cmd2 commands Ranges of [] ls file[0-9] letters/numbers > Redirect output to a file prog1 > file Redirect input to a < prog1 < file program

Notes:

. Don’t forget to use: button to complete any command. < backslash <\> button to break any long command. < Semicolon<;> button to multiple commands in the same line. button to any opened file. . You can edit text files using graphical editors like : gedit. . You learned how to use help commands in Linux to read about any command, try to use these commands: vi, , more, , hostname.

7

Operating System ECOM 4310 Lab.2 Exercise:

Create the following of directories and files using commands that you learned in this lab.

DIR1

File1 DIR3 DIR2

Welcome to Linux *** File2 DIR5 File3 File5 2 Your name Odd numbers & ID DIR4 from 1 to 30

File4

Any comment from you

***

8