0% found this document useful (0 votes)
24 views

Chapter 2

This document provides an introduction to UNIX commands. It discusses how commands are implemented as executable files and located using the PATH variable. It describes different types of commands, and how to get help and information on commands using man and type. Finally, it introduces some common UNIX commands like ls, cp, rm, grep, and date and encourages practicing them.

Uploaded by

Rjo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chapter 2

This document provides an introduction to UNIX commands. It discusses how commands are implemented as executable files and located using the PATH variable. It describes different types of commands, and how to get help and information on commands using man and type. Finally, it introduces some common UNIX commands like ls, cp, rm, grep, and date and encourages practicing them.

Uploaded by

Rjo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CHAPTER 2 – BECOMING

FAMILIAR WITH UNIX


COMMANDS
NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem
2.1 Command Basics
2

 Commands are mostly implemented as disk


files which contains executable code
 UNIX Doesn’t require command names to have

extension
 The shell is a special command: runs

continuously as long as you are logged

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


Types of Commands
3

 External commands are


 executed by running programs located in the PATH variable.
 They exist as actual files with execute permission.

 Examples: ls, cat, more

 Internal commands are


 written into the shell and don’t correspond to an executable
file
 an alias defined by the user that invokes the disk or internal
version in a specific manner.
 Examples: echo, type, cd, pwd

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


The PATH: Locating Commands
4

 One of the most important environment variables is


the path.
 Set to list of colon-delimited directories
 It defines where the shell looks for commands.

 Shell looks at PATH only when command is not used


with a pathname and is also not a shell built-in.
 echo $PATH
/usr/local/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/usr/dt/bin:/bi:.

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


How does the Shell know whether a
5
command can be executed or not?
 If command is invoked with a pathname (like
/bin/echo), the shell runs program at the specified
location.
 If command is invoked without a pathname, the shell
first checks whether it is an internal or not
 If alias or built-in, the shell runs it without looking in disk.
 If not, the shell looks at the PATH variable for directories
where the command may reside.
 If found, it is executed. Otherwise, an error is output

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


Where is the command?
6

 The commands that provides help on the location of


another command: which and type
 which provides the location of a command
 searches the directories of PATH in sequence
 Ends on the moment it locates the command, or an error

 Ex: which grep will return/usr/bin/grep

 type Indicates if a command is built into the shell or gives


its location if known.
 For example, type echo will return “echo is a shell built-in”

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.2 Structure of a Command
7

 Command -Option(s) Argument(s)


 Command = the name of the command
 Options = parameters with a fixed meaning that change
how the command works
 Arguments = parameters used by the command.

Ex:
ls -l -t note1 note2

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.2 Structure of a Command (cont.)
8

 A command’s behavior is determined by its


arguments and options.
 Command and arguments must be separated

by whitespace.
 Generally possible to combine multiple options

into a single one (like ls -l -u -t == ls -lut)


 Some options have their own arguments

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.3 Flexibility of Command Usage
9

 Run multiple commands by specifying them in the same line:


date ; echo $PATH
 Split a command into multiple lines:
$ echo “Hello
> Dolly”
 Save command output in a file (not always on the screen):
date > foo.tx
 Use output of one command as input of another:
who | wc -l
 Run a command in the background with &:
ls -lRa / > $HOME/ls-lRa.txt &
 UNIX provides a full-duplex terminal

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.4 man: On-line Help
10

 Displays documentation of commands, configuration


files, system calls and library functions.
 Organized in a number of sections. Commands are
found in Section 1.
 May need to use section number when entry exists in
multiple sections (e.g. man passwd and man -s 5
passwd).
 man pages are available on the Internet
 man documentation not available for most internal
commands of the shell.
 Use man man first to know how man should be used.

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


man: Example
11
Reformatting page. Please Wait... done
User Commands wc(1)
NAME
wc - display a count of lines, words and characters in a
file

SYNOPSIS
wc [-c | -m | -C] [-lw] [file...]

DESCRIPTION
The wc utility reads one or more input files and, by
default, writes the number of newline characters, words and
bytes contained in each input file to the standard output.

The utility also writes a total count for all named files,
if more than one input file is specified.

wc considers a word to be a non-zero-length string of char-


acters delimited by white space (for example, SPACE, TAB).
See iswspace(3C) or isspace(3C).

--More--(23%)

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.6 echo: Displaying Messages
12

 echo: used to display files or diagnostic messages.


 Usage:
echo [string to be displayed or filename]
 $ echo "hello"
hello
 $ echo $SHELL
/bin/bash

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


2.7 printf: Alternative to echo
13

 Like echo, it exists as an external command


 Unlike echo, you must use \n – escape sequence
 $printf "Testing"

Testing$printf "Testing\n"
Testing
$printf "My current shell is %s\n" $SHELL
My current shell is /bin/ksh
 printf uses format specifiers similar to what used in C
language (e.g %s)

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem


Other Commands
14

 passwd: changing your password


 uname: display Machine’s Name and Operating System
 uname –r : shows current release
 uname –m: shows the machine name (hostname or domain
name)
 who: Know the Users currently logged in
 who am i: info about you
 date: display current date and time
 date +%m – display current month
 date +%h – display current month name
NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem
Exercise
15

Use man to find out what the following commands


do then try them on UNIX terminal
 ls  grep
 cd  sort
 rm  who
 rmdir  whoami
 cp  head
 mv  tail
 cat  date
 more  tar

NES 202 : Introduction to UNIX, Done By : Eng. Rana AlQurem

You might also like