Curso Unix Primer Nivel
Curso Unix Primer Nivel
Curso Unix Primer Nivel
Curso Unix.
Why UNIX
•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
Example:
ls -l *.dat
•Place commands or tasks into these files that you would like
performed whenever you login.
The Unix Environment
• 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
There are two special relative path symbols, '.' and '..' . These
indicate the current directory (.) and its parent (..).
Examples:
ls ../smith
cd ..
Examples:
mkdir project
mkdir project/data
mkdir –p /home/pelos/delfin
rmdir: Remove a Directory
-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
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
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 &
Example:
kill 12345
kill %jobid kill the background job
Example:
kill %1
^z suspend the current interactive job
Processes and Job Control
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
pack and unpack - usually included with Unix. These files have
the suffix ".z"
ps: Check on the status of processes
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
• 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
#!/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