Curso Unix Primer Nivel

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 61

Subdirección de Tecnología

Curso Unix.
Why UNIX

Multiuser with efficient multitasking


Effective use of resources
Security
Available on many systems from PCs to Supercomputers
Flavors of UNIX
All the basic commands are the same. Differ mainly at the programmer's level.
Major versions are the Berkeley Standard Distribution (BSD) and AT&T System
V.
Some Workstation Variants: SunOS & Solaris(SUN), AIX(IBM), IRIX(SGI),
ULTRIX(DEC), HP- UX(HP)
Some PC & Mac Variants: BSD/OS, Linux, SCO, ESIX, XENIX, AUX
Case Sensitivity

•UNIX is case sensitive.


•Most commands and usernames are in lower case.
•command: cd and CD are different
•username: smith, SMITH and Smith are all different
On-line UNIX help

•The man command displays information from the UNIX manual set.
•Use the command man man to see the options available on your system.
UNIX Command Syntax

Commands in UNIX are of the following form

command options parameters

Example:

ls -l *.dat

Use the man command to find detailed information about UNIX


commands
Kernel
The Unix Environment

•A shell is a program that reads commands and executes


them.
•The common shells are the C shell (csh), the Bourne shell
(sh), and the Korn shell (ksh).
•The shells differ in scripting syntax and a few other minor
ways.
•You may be able to change your login shell using the chsh
command.
The Unix Environment

• If the following files exist in your home directory, they will


be executed whenever you login:
csh: .cshrc and .login
sh and ksh: .profile

•Place commands or tasks into these files that you would like
performed whenever you login.
The Unix Environment

It is possible to create aliases for commands in your login


files. For example, to alias the command ls -l to be dir:
csh:
alias dir 'ls -l'
sh:
dir() /bin/ls -l $*
ksh:
alias -x dir='ls -l'
Shell Variables

In general, shell variables that are special to the shell are


written in uppercase, and user-assigned shell variables are
written in lowercase:
Examples:
$HOME
$myvar
To see the value of a specific shell variable, use the echo
command:
echo $HOME
Shell Variables

Several environmental variables are special to the shell. These


include:
$HOME your login directory
$PATH directories that will be searched for
commands
$TERM your terminal type
$DISPLAY your local hostname for X windows
applications
Shell Variables

To set shell environmental variables:


csh:
setenv PATH /usr/bin:/bin
setenv PATH $PATH\:$HOME/bin
sh, ksh:
PATH=/usr/bin:/bin
export PATH
PATH=$PATH\:/usr/bin:/bin
Export PATH

** Explain the . In the PTAH


Shell Variables

To set shell environmental variables:


csh:
setenv PATH /usr/bin:/bin
setenv PATH $PATH\:$HOME/bin
sh, ksh:
PATH=/usr/bin:/bin
export PATH
PATH=$PATH\:/usr/bin:/bin
Export PATH

** Explain the . In the PTAH


General Command Syntax

The general form of a command in UNIX is:


command -options parameters

command is the name of any file both executable and


locatable. A file is locatable if it is in one of the directories
defined by the PATH variable or the path is supplied in the
name.
Example:
ls -l /home/jones/dir1
General Command Syntax

Filename shorthand: there are some special characters that


the shell recognizes as shorthand for filenames. These
include:

? - substitute any character at that position: file?.txt (file1.txt,


file2.txt, ...)

* - substitute any number of any character(s) at that position:


file*.txt (file1.txt, file1a.txt, file.txt, ...)
General Command Syntax

• Shell variables in commands are expanded by the shell.

• Shell variables are indicated by a "$" as the first character


(e.g. $PATH, $HOME).

• Examples:
echo $PATH
cd $HOME
ls $dir1
General Command Syntax
It is possible to redirect command input and output away from the
keyboard and screen and from/to files:
<
Read standard input from file
>
Write standard output to new file
>>
Append standard output to file
|
Send standard output from the first command to the standard input
of the next
Examples:
ls > dir.list
ls $dir1 | more
The UNIX Filesystem

• The general form is a tree.

• The top of the tree, commonly called the root, is denoted by


a single forward slash (/). All files are located "below" the
root directory.
The UNIX Filesystem

Filenames are specified in one of three forms:


Absolute paths begin with a slash:
more /home/jones/adir/kermit.txt
Relative paths (no slash at the beginning) start their
search in the current directory:
more adir/kermit.txt

Filenames with no paths are presumed to be in the current


directory:
more kermit.txt
The UNIX Filesystem

Show the current directory with the pwd command.

The cd command (change directory) is used to move around


the directory tree.

Examples for the tree given above are:


cd /
cd /usr/jones
cd
The UNIX Filesystem

There are two special relative path symbols, '.' and '..' . These
indicate the current directory (.) and its parent (..).
Examples:
ls ../smith
cd ..

Another resource: “-”


mkdir: Create a New Directory

Create new directories with the mkdir command. It makes


the named directory.

Some UNIX systems allow creation of many levels of


directory at once.

Examples:
mkdir project
mkdir project/data
mkdir –p /home/pelos/delfin
rmdir: Remove a Directory

Delete a directory with the rmdir command.

The directory to be deleted must be empty of both files and


other directories.

Directories must be deleted from the bottom up.


Examples:
rmdir project/data
rmdir project
cat : Print a File to the Screen
cat is an all-purpose command:
• Print one or more files to the screen:
Example:
cat filename(s)
• Create a file:
Example:
cat > x.y <ENTER>
type in
some text
^d (ends the input)
• Concatenate (append) files:
Example:
cat a b c > d
mv : Move a File
The mv command moves and/or renames a file.
Rename a file:
Example:
mv a.dat a.old
Move a file to a different place in the filesystem:
Examples:
mv a.dat ../save (directory)
mv a.dat ../save/a7.old
The destination file is replaced if it exists.
The original file is removed, so use with caution.
cp : Copy a File

The cp command copies a file from one name or place to


another.
Examples:
cp a.dat a.old
cp a.dat ../save (directory)
cp a.dat ../save/a.old

The destination file is replaced if it exists.


rm : Remove/Delete a File

The rm command is used to remove or delete one or more


files.

The -i option will ask you about each file to be removed.


There is no way to recover a file after it has been removed.
Examples:
rm x.y
rm a.dat b.dat c.dat d.dat
rm -i *.o
ls : List Files in Directories
The ls command lists the files in directories. Some of the more
common options are:

-F Flag type of file with "/", "*", "=", "@" -l List files
in a long, descriptive format -a Show all entries,
including "dot" files -t List files in reverse order of
time created
Examples:
ls
ls -Fla
ls -Fla *.c
ls -Fla adir (directory)
find : Find Files and Manipulate Them

• The find command locates specified files and lets you perform a number
of actions on (or to) them.
• find will search recursively down the directory tree starting at the
directory specified on the command line.
• find uses words for options, not single letters. Some of the more common
options are:
-name fn look for the filename specified
-print print the filenames found
-exec cmd run the command "cmd"
-newer fn find files newer than the file "fn"
Examples:
find . -name slides.txt -print
find / -name \*.c -print
find / -name \*.old -exec rm -i {} \;
file : Determine the Type of a File

The file command is used to determine the type of contents of


a file.

Use this before using either cat or more to look at a file at a


terminal.

Types are: text, binary executable, commands, data, etc.


Never bring binary executable files to a terminal and be
careful with data.
Examples:
file x file * | more
more : Look at a Screen of Output

The more command is used to look at the contents of a file,


one screenful at a time.

more also allows you to break up output to standard output


to one screen at a time.

To continue, press the spacebar; to quit, "q"


For help, press "h" while more-ing a file.
Examples:
more letter.doc
more *.doc
ls /usr/lib | more
tail: Look at the End of a File

• The tail command displays the end of a file.


• Specify the number of lines displayed by including a
number preceded by a "-".
•The tail of a file being created by another process may be
viewed continuously (this flag varies from system to system;
check the tail man page).
Examples:
tail letter.doc
tail -100 letter.doc | more
diff: Compare Files

diff reports differences between files.

Output lines are flagged with "<" for the first file and ">" for
the second file.
Example:
diff file1 file2 1c1
< this is file1
---
> this is file2
Processes and Job Control

•Unix is a multiuser, multitasking operating system (task = process).


•On a Unix system, it is possible to run many programs and commands (processes) at the
same time.
•Each user can have multiple processes running at the same time.
•The ps command is used to look at processes running on the system.
•The arguments to and output from this command depends on the flavor of Unix that you
are running.
•ps by itself will show all running processes that are associated with your current terminal.

Example of output from ps PID TTY TIME CMD 16035 pts/1 0:00 ps 19628 pts/1 0:00 ksh
System V systems (AIX, IRIX, Solaris, Linux):
ps -ef all processes, full listing
ps -fu user all processes associated with user, full listing
BSD systems (SunOS, AIX, BSD/OS):
ps aux all processes, full listing
ps u all processes associated with running user, full listing
Processes and Job Control

When a command or program is run from the command line, in most cases you will
have to wait for it to finish before you can enter other commands. This is known as
running interactively.

It is possible, however, to run commands and programs detached from the controlling
terminal; this is known as running in the background.

To run a command in the background, place an ampersand (&) at the end of the
command line.
Example: Find all the files in your directories that end in .txt and list them, but don't
wait for the command to complete:
find $HOME -name \*.txt -print > txt.list &
Note that the output is redirected to a file - otherwise, it would be sent to standard
output and may interfere with what else you are doing.
If the program expects input from the keyboard, this input must be redirected
from a file or else the program will fail.
Processes and Job Control

Typical session:
runme > cmd.out &
[1] 19518 jobs [1] + Running runme > cmd.out &
[1] + 19518 Done runme > cmd.out &

The number in square brackets is the jobid and is identified


by a leading "%"
Processes and Job Control

Other useful commands:


jobs -- list your background jobs
kill pid -- kill the specified process

Example:
kill 12345
kill %jobid kill the background job
Example:
kill %1
^z suspend the current interactive job
Processes and Job Control

bg put a suspended job in the background

fg %jobid bring a background job to the foreground

nohup continue to execute the command after you log out,


i.e., make the command immune to hangups:
Example:
nohup command &
See the manual page for your shell (csh, ksh etc.) for
additional job control commands
Processes and Job Control
File Archiving Commands

There are a number of commands built into Unix to provide


the ability to archive and compress files.

A number of public domain programs are also available.


File Archiving Commands

The tar command is used to write data to tape and create


multi-file archives on disk.
Examples:
Create file containing all files in a directory:
tar cvf files.tar adir

Extract these files:


tar xvf files.tar

List the files in the tar archive:


tvf files.tar
File Archiving Commands

Use the compress command to shrink files. The compress command will
add a ".Z" to the end of the compressed file:
compress letter.doc
To look at the contents without uncompressing, use the zcat command:
zcat letter.doc.Z |more
Run uncompress to restore them:
uncompress letter.doc.Z
Often, tar and compress are used together to create file archives for
storage or distribution.
These files will have a name of the form "something.tar.Z"
To look at the contents of a ".tar.Z" file:
zcat files.tar.Z | tar tvf - | more
To untar the file:
zcat files.tar.Z | tar xvf -
File Archiving Commands

gzip (Gnu zip) is a public domain compression program. The standard


suffix added is ".gz" or ".z"
To zip files:
gzip file.txt
To unzip files:
gunzip file.txt.gz
To look at the contents without unzipping the file:
gunzip -c file.txt.gz

Other commands may be available, including:


zip and unzip - compatible with DOS zip. File suffix is ".zip".
This is a public domain program and not included with the
standard Unix operating system.

pack and unpack - usually included with Unix. These files have
the suffix ".z"
ps: Check on the status of processes

The options to the ps command depend on the flavor of


UNIX:
System V systems (AIX, IRIX, Solaris, Linux, DEC Unix):
% ps -ef
all processes, full listing
% ps -fuuser
all processes associated with user, full listing
BSD systems (SunOS, AIX, BSD/OS, DEC Unix):
% ps aux
all processes, full listing
% ps u
all processes associated with running user, full
listing
at: Run a command at a specified time

User can specify date and time to run a command or several


commands in a file:
% at 15:00 command_file
% at now + 2 days command_file
% at 3pm June 8 command_file
% echo 'halt -q' | at now + 1 minute
To list your jobs in the at queue:
% at -l
To remove the job jobid from the queue:
% at -r jobid
The cron subsystem

• The cron susbystem is used to run recurring commands


(daily backups, accounting, etc.)

• The list of commands to run is found in the user's crontab


file, usually located in the file:
/var/spool/cron/crontabs/userid

• Use the command crontab -e to create or modify the crontab


file.

• crontab -l lists the contents of the crontab file.


crontab File format

• Comments begin with a # in the first column


• There are six fields in a crontab entry:
Minute: 0-59
Hour: 0-23
Day of Month: 1-31
Month: 1-12
Day of Week: 0-6 (0 = Sunday)
Command to run
• Possible wildcards are
,
Comma: To separate multiple values
-
Dash: To specify a range
*
Asterisk: To specify all values in field
Example: # run backups at 9:00pm Monday through Friday 0 21 * * 1-5 /backup_files 2> /dev/null
# run a program every 10 minutes: 0,10,20,30,40,50 * * * * /home/jsmith/bin/send_data
# run a program at 7:21am on the 1st day of every month: 21 7 1 **/home/jsmith/bin/monthly_program
nice: Change the priority of a process

• CPU time on a UNIX computer is given to processes based on their


priority.
• The nice command is used to run a command at a lower priority:
% nice mycommand &
% nice -15 mycommand &
• The root user can raise the priority of a process
% nice --10 wall <<end System shutdown in 2 minutes\! end
nohup: Protect a process from hangups

• In some shells, all background processes are terminated when


you logout.

• To protect a process from hangups, run it with the nohup


command:
Example:
% nohup cc -c *.c &
• By default, both standard output and standard error are
redirected to the file nohup.out.
Other process-related commands

jobs
list your background jobs
Kill pid
kill the specified process
Kill %jobid
kill a background job
^z
suspend the current interactive job
bg
put a suspended job in the background
fg %jobid
bring a job to the foreground
batch
run a command when the system load permits. The syntax is
similar to that of the at command.
Advanced Commands

• UNIX command philosophy

• Most commands perform only simple tasks. This leads to


small and efficient commands.

• Several commands are used together to perform more


complex tasks.

• The following commands, used alone or with other


commands, are very useful.
awk: A powerful file processing tool

• awk matches patterns in files and performs operations on them.


• awk is also a programming language with a syntax similar to C.
• It is commonly used as a filter for many problems.
• The awk utility is covered in detail in the UNIX Utilities II: awk training course.
• Examples:
Delete the second field of every line in the file a.dat
% awk '{$2= ""; print}' a.dat > a.dat.new
Print the second field of every line in the file a.dat
% awk '{print $2}' a.dat | more
Print out all lines in the file a.dat that are longer than 72 characters:
% awk 'length > 72' a.dat
Kill all processes belonging to user jdoe
% ps aux | awk '$1=="jdoe" {print "kill " $2}' | sh
cut: Write out selected parts of a line

• The cut command is used to write out selected characters


or fields from each line of a file.
• Examples:
Print the second field of every line in the file a.dat
% cut -f2 -d\ a.dat | more
Print the first 5 charcters of every line in the file a.dat
% cut -c1-5 a.dat | more
grep: Match patterns in a file

• grep searches a file (or a group of files) for a specified


regular expression

• case matters in the matching unless the -i option is specified

• The -v option prints all lines not containing the pattern


Examples:
% grep Smith *.letter
% grep -i smith *.letter
% ps aux | grep -v root
sort: Sorts and merges files
• The sort utility sorts files based on user-defined keys.
• If multiple files are specified, they are first merged, then sorted as a
single file.
• Common options:
-r
sort in reverse order
-b
ignore leading spaces
+f.c
specify start of sort key (field.column)
-f.c
specify end of sort key (field.column)
-n
sort numerically
ln: Link files

• It is often advantageous to assign multiple names to a single


file.

• The ln command links two files or directories. Either name


can be used to access the file.

• Examples:
create fort.1 which points to input.file
% ln input.file fort.1
Link the directory /usr/local/lib/X11 to /usr/lib/X11
% ln -s /usr/local/lib/X11 /usr/lib/X11
sort: Sorts and merges files
• The sort utility sorts files based on user-defined keys.
• If multiple files are specified, they are first merged, then sorted as a
single file.
• Common options:
-r
sort in reverse order
-b
ignore leading spaces
+f.c
specify start of sort key (field.column)
-f.c
specify end of sort key (field.column)
-n
sort numerically
stty: Set/Change terminal settings

• The stty command is used to set or change the terminal/line


parameters for a UNIX session.

• There are over 40 parameters you may change.

• stty -a will show the current value of all terminal settings.

• The sequence ^q^jstty sane^j can often be used to return


control of your terminal to you after a problem.

• Consult the stty man page for system-dependent options.


UNIX Shell scripts

• A shell script is a collection of UNIX commands which are


executed together.

• Similar to .BAT files in DOS and .COM files in VMS.

• Shell scripts are the primary tool for combining the


specialized UNIX commands to perform more complex and
useful tasks.

• Shell scripts also allow users to make use of the simple


programming languages built into the different shells.
Sample C shell (csh) script:

#!/bin/csh
# # Check for all processes owned by the specified
# user ($1) on a set of machines
#
foreach comp ( sa sb sc sd se sf sg sh si sj sk )
echo $comp
rsh $comp ps aux | grep $1
echo " "
end
Same sample in Korn shell (ksh):

#!/bin/ksh
# # Check for all processes owned by the specified
# user ($1) on a set of machines
#
for comp in sa sb sc sd se sf sg sh si sj sk do
echo $comp
rsh $comp ps aux | grep $1
echo " "
done

You might also like