Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Terminal window
add
;C:\cygwin\bin
at end of Path
5) Click ok and close the all active windows and open command
prompt (DOS) to test the Unix commands.
system prompt
c:\Windows\system32> cd c:\cygwin64\home\jchein1
c:\cygwin64\home\jchein1>
From now on we use $ as system prompt
DOS window
$ man http
HTTP::Tiny(3pm)
NAME
HTTP::Tiny - A small, simple, correct HTTP/1.1 client
VERSION
version 0.012
SYNOPSIS
use HTTP::Tiny;
my $response = HTTP::Tiny->new->get('http://example.com/');
die "Failed!\n" unless $response->{success};
print "$response->{status} $response->{reason}\n;
while (my ($k, $v) = each %{$response->{headers}}) {
for (ref $v eq 'ARRAY' ? @$v : $v) {
print "$k: $_\n";
}
}
-----------------------Manual page HTTP.Tiny(3pm) line 1 (press h for help or q to quit)
HTTP::Tiny(3pm)
$ man man
MAN(1)
MAN(1)
NAME
man - an interface to the on-line reference manuals
SYNOPSIS
man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M
path] [-S
list] [-e extension] [-i|-I] [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager]
[-r
prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification] [-p string] [-t] [-T[device]]
[-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
man -k [apropos options] regexp ...
man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
man -f [whatis options] page ...
man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [7] [-E
encoding] [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
man -w|-W [-C file] [-d] [-D] page ...
man -c [-C file] [-d] [-D] page ...
man [-?V]
-----------------------Manual page man(1) line 1 (press h for help or q to quit)
$ man 3 fork
FORK(3am)
NAME
fork, wait, waitpid - basic process management
SYNOPSIS
@load "fork
pid = fork()
ret = waitpid(pid)
ret = wait();
DESCRIPTION
The fork extension adds three functions, as follows.
----
$ man grep
cp,
http,
help,
printf,
ls,
exec
3 fork
-P more printf
# $ is system prompt
user comment
hello.c is created
$ cat hello.c
#include <stdio.h>
int main () {
printf ("Hello world!\n");
return 0;
}
$
display hello.c
^D
C:\cygwin64\
home\
current directory
jchein1\
demo
$ cat demo2 >> demo5 # append demo onto the end of demo5
The ls command lists all files in the directory that match the name. If name is left blank, it will list all of the files in the
directory.
Option
Action
-a
-d
-F
-g
-i
-l
-R
-t
$ ls */
$ ls l
# Lists the total files in the directory and subdirectories
total 533
drwxr-xr-x+ 1 jchein1 None
0 Oct 17 10:26 class
-rwxr-xr-x 1 jchein1 None
9 Oct 23 22:16 demo
-rwxr-xr-x 1 jchein1 None
9 Oct 23 22:33 demo3
-rwxr-xr-x 1 jchein1 None
18 Oct 23 22:19 demo4
-rwxr-xr-x 1 jchein1 None
91 Oct 23 20:15 hello.c
-rwxr-xr-x 1 jchein1 None 110 Oct 17 14:26 hello1.cpp
-rwxr-xr-x 1 jchein1 None 467084 Oct 17 15:37 hello1.exe
-rwxr-xr-x 1 jchein1 None
97 Oct 17 14:10 hello2.c
-rwxr-xr-x 1 jchein1 None 65454 Oct 17 14:22 hello2.exe
group
jchein1 None 91 Oct 23 20:15 hello.c
# of links
length
file
name
date
file type and permissions
owner
time
$ ls -d .*/
../ ./
$ ls -f .*/
../:
. .. Jchein1
./:
. .bashrc
.bash_profile .profile a
demo2 demo4 demo6 hello1.cpp hello2.c
.. .bash_history .inputrc
444
class3 demo3 demo5 hello.c hello1.exe hello2.exe
$ ls -l demo*
-rwxr-xr-x 1 jchein1 None 17 Oct 24 08:45 demo2
-rwxr-x--- 1 jchein1 None 9 Oct 23 22:33 demo3
-rwxr-xr-x 1 jchein1 None 8 Oct 24 08:40 demo4
-rwxr-xr-x 1 jchein1 None 22 Oct 24 10:27 demo5
-rwxr-xr-x 1 jchein1 None 8 Oct 24 22:43 demo6
Text editor: vi
To edit a file named demo in the current directory, enter:
$ vi demo
~
~
~
~
~
~
~
~
~
~
~
~
# type :quit and press enter key to get out vim
"demo" [New File]
The vi editor works fairly differently from other text editors. If you have not used it
before, you should probably look at a tutorial, such as How do I use the vi text editor?
While in command mode, you can move the cursor around with the arrow
keys. Press the x key to delete the character under the cursor. There are a
variety of other delete commands for example, typing dd (press the d key
twice) deletes an entire line of text.
You can select, copy, cut and paste text in command mode.
Position the cursor at the left or right side of the text you want
to copy and press thevkey. Move your cursor to select text,
and then pressyto copy the selected text orxto cut it.
Position your cursor at the desired location and press
thepkey to paste the text you copied or cut.
Insert Mode
Entering insert mode you just press the i key once after youve
positioned the cursor in command mode. Start typing and vi will
insert the characters you type into the file rather than trying to
interpret them as commands. Once youre done in insert mode,
press the escape key to return to command mode.
Saving and Quitting
You can save and quit vi from command mode. First, ensure youre
in command mode by pressing the escape key (pressing the escape
key again does nothing if youre already in command mode.).
Type :wq and press enter to write the file to disk and quit vi. You
can also split this command up for example, type :w and press
enter to write the file to disk without quitting or type :q to quit vi
without saving the file.
Find
Lists all of the files within a directory and its subdirectories that
Find
$ find . -name "*" -print
./.bashrc
./.bash_profile
./.inputrc
./.profile
./444
./a
./class3
./demo2
./demo3
./demo4
./demo5
./hello.c
./hello1.cpp
./hello1.exe
./hello2.c
./hello2.exe
$ find . ~/ -name 444 print
./444
/home/jchein1/444
GREP
Grep searches the input files for lines containing a match to a given
pattern list. When it finds a match in a line, it copies the line to output
screen, or an output you have requested.
It can match arbitrary characters within a line. If the final byte of an input
file is not a newline, grep silently supplies one.
$ 2 demo3
22
$ grep 2 demo*
demo2:22
demo2:22
demo3:22
demo4:22
demo5:22
demo5:22
demo6:22
GREP
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
$ grep -n 2 demo2
2:22
5:22
Dif
Comparing two files or directories. Indicates which lines need be added (a), deleted (d) or changed (c). Lines in file1
are identified with a (<) symbol: lines in file2 with a (>) symbol
$ diff demo2 demo3
4,6d3
< 11
< 22
< 33
\ No newline at end of file
$ diff -iw demo2 demo3
Sort
$ cat >demo6
33
22
11
$ sort demo6
11
22
33
$ sort -o sorted demo6 # sorts the file demo6 and writes
output to sorted
Lstng a fle:
cat/more/page/head/tail
cat
: concatanate
more, page : display in parts without scroll
head : display first n lines, for default n=10
tail
: display last n lines, for default n=10
$ cat demo
11
22
33
$ head -2 demo
11
22
$ tail -2 demo
22
33
$
# concatanate
Renamng a fle : mv
mv i oldFile newFile
mv i {file name}* directoryName
mv i oldDirectory newDirectory
The form renames oldFile to newFile.
The second form moves a collection of files into a directory.
The third form is used to move the files from oldDirectoty to
newDirectory.
The option -i prompts confirmation if newFileName already exists
Renamng a fle : mv
$ ls
class demo demo2 demo3 hello.c hello1.cpp hello1.exe hello2.c
hello2.exe
$ mv demo demo4
$ ls
class demo2 demo3 demo4 hello.c hello1.cpp hello1.exe hello2.c
hello2.exe
$ cat demo4
11
22
33
$
Copyng a fle : cp
This command copies a file, preserving the original and creating an
identical copy. If you already have a file with the new name, cp will
overwrite and destroy the duplicate.
cp i oldFileName newFileName
cp ir {file name}* directoryName
$ cp demo demo6
$ cp -I demo demo6
#i: confirm
cp: target 'demo6' is not a directory
$ ls
444 class class2 class3 demo2 demo3 demo4 demo5 demo6
hello.c hello1.cpp hello1.exe hello2.c hello2.exe
$
Movng to a drectory: cd
cd [option] [directory]
$ pwd
# print working directory
/home/jchein1
$ cd class
$ pwd
/home/jchein1/class
$ ls
$ cd ..
$ cd class2
$ ls
child_class2
$ cd /
c:\>
c:\>cd \cygwin64\home\jchein1
Deletng a file: rm
rm fir {filename}*
f: inhibit error messages
i: inform each time
r: if filename is a directory
$ rm demo6
$ ls
444 class3 demo2 demo3 demo4 demo5 demo6 .
$ rm class3
# remove all files in directory class
$ ls class3
# All the files under the directory class are deleted
$
$ lpr a
-bash: lpr: command not found
File Names
UNIX permits file names to use most characters, but avoid spaces, tabs and characters that have a special
meaning to the shell, such as:
&;()|?\'"`[]{}<>$-!/
Case Sensitivity: uppercase and lowercase are not the same.
Length: can be up to 256 characters
Extensions: may be used to identify types of files
libc.a
Fle types
d
b
c
l
p
s
regular file
directory file
buffered special file (such as disk drive)
unbuffered special file (such as disk terminal)
symbolic link
pipe
socket
Fle permssons
Every Unix file has an owner and a group. You can find out the groups
of all of the files in a directory by using the command ls -lg in the
directory. Each file will be listed, along with its owner and group, and
some other information.
$ cat >a
aaa
# owner group
$ ls -lg a
-rw-r--r-- 1 None 3 Oct 24 19:52 a
$ ls -l a
-rw-r--r-- 1 jchein1 None 3 Oct 24 19:52 a
Fle permssons
The output from the ls -l command shows all permissions for all levels as
three groups of three according to the scheme:
mode string (10 chars)
symbolic link
owner read (r)
owner write (w)
owner execute (x)
group read (r)
group write (w)
group execute (x)
public read (r)
public write (w)
public execute (x)
which are displayed as:
-rwxrwxrwx
$ ls -bl /
total 361
-rw-r--r-- 1 jchein1 root 53342 Sep 25 16:05 Cygwin-Terminal.ico
-rwxr-xr-x 1 jchein1 root 59 Sep 25 16:05 Cygwin.bat
-rw-r--r-- 1 jchein1 root 157097 Sep 25 16:05 Cygwin.ico
drwxr-xr-x+ 1 jchein1 None
0 Sep 25 16:03 bin
dr-xr-xr-x 1 jchein1 None
0 Oct 25 10:19 cygdrive
drwxr-xr-x+ 1 jchein1 None
0 Sep 25 16:04 dev
drwxrwxr-x+ 1 jchein1 None
0 Sep 25 16:04 etc
drwxrwxrwt+ 1 jchein1 None
0 Oct 17 13:14 home
drwxr-xr-x+ 1 jchein1 None
0 Sep 25 16:03 lib
dr-xr-xr-x 9 jchein1 None
0 Oct 25 10:19 proc
drwxrwxrwt+ 1 jchein1 None
0 Oct 23 18:25 tmp
drwxr-xr-x+ 1 jchein1 None
0 Sep 25 16:00 usr
drwxr-xr-x+ 1 jchein1 None
0 Sep 25 16:02 var
Fle permssons
File ownership is an important component of UNIX that provides a secure method for
storing files. Every file in UNIX has the following attributes:
Owner permissions: determine what actions the owner of the file can perform on the
file.
Group permissions: determine what actions a user, who is a member of the group that a
file belongs to, can perform on the file.
Other (world) permissions: indicate what action all other users can perform on the file.
# owner
group
$ls -l aaa
-rwxr-xr-- 1 jchein1 None 3 Oct 24 19:52 aaa
Characters (2-4) represent the permissions for the file's owner. For example -rwxr-xr-represents that onwer has read (r), write (w) and execute (x) permission.
Characters (5-7) consists of the permissions for the group to which the file belongs. For
example -rwxr-xr-- represents that group has read (r) and execute (x) permission but no
write permission.
Characters (8-10) represents the permissions for everyone else. For example -rwxr-xr-represents that other world has read (r) only permission.
Fle permssons
r
read
regular file
Read only
process
process may
w
change the
write contents
process may
x
execute the
execut program file
e
directory
process can read
directory (i.e. list
the files)
special file
process may read
from the file using
the read( ) system
call
process may add or process may write
remove files in the
to the file using the
directory
write( ) system call
process may access none
files in the directory
or any of its
subdirectories
operation
new
permission
u (user)
+ (add)
r (read)
g (group)
- (remove)
w (write)
o (others)
= (assign)
x (execute)
selection
a (all)
permission_triads
filename
action
u = user
+ = add
r = read
g = group
- = remove
w = write
o = other
a = all
permissions
x = execute
group
others
rwx
rwx
rwx
setting
rwx
r-x
---
binary
111
101
000
octal
GROUPS
You need to use the groups command to show the groups a user
belongs to on Linux/Unix shell prompt.
The basic syntax is as follows:
groups
groups [UserNameHere]
groups --help
groups version
$ groups
None Users
$ groups | wc w
2
GROUPS
Recommend alternative to groups command
The groups command has been obsoleted by the id
GROUPS
$ ls lg a #option g stands for listing also files group
ls: cannot access -lg: No such file or directory
a
$ ls -l hello.c
# group information
-rwxr-xr-x 1 jchein1 None 91 Oct 23 20:15 hello.c
$ groups
None Users
# If I want to be added to a new group, you should request the
system administrator to do it.
$ ps
PID PPID PGID
WINPID TTY
UID STIME COMMAND
6056
1 6056
6056 ?
1000 07:59:42 /usr/bin/mintty
5448
1 5448
5448 cons0 1000 10:51:42 /usr/bin/ps
I 4340 6056 4340
3940 pty0 1000 07:59:42 /usr/bin/bash
$ kill 6056
# shut down unwanted you own processes
$ ps
PID PPID PGID
WINPID TTY
UID STIME COMMAND
2132
1 2132
2132 cons0 1000 11:01:21 /usr/bin/ps
$ kill -9 6056
# If shut down doesn't work you can use the -9
switch
$ ps -af | more # give a detail list of all the processes currently active
on the system
UID
PID PPID TTY
STIME COMMAND
jchein1 4340
1 cons0 11:06:42 /usr/bin/ps
$ users
$ env
!C:=c:\cygwin64\home\jchein1
!ExitCode=00000000
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\jchein1\AppData\Roaming
CATALINA_HOME=c:\apache-tomcat-6.0.41
classpath=.;c:\jdk1.6.0_25\bin;c:\sqljdbc_4.0\enu\sq
lapi.jar;c:\javamail-1.4\lib\dsn.jar;c:\javamail-1.4
3.jar;;c:\Program Files\Java\jdk1.6.0_25\jre\lib\smt
m Files\Java\jdk1.6.0_25\jre\lib\imap.jar;
--------JAVA_HOME=c:\jdk1.6.0_25
LOCALAPPDATA=C:\Users\jchein1\AppData\Local
LOGONSERVER=\\JCHEIN1-PC
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
PATH=/cygdrive/c/Windows/system32:/cygdrive/c/Window
------------
$ setup ingres
-bash: setup: command not found
Installing GCC
1. Make sure that you are not running Cygwin
2. Run Cygwin setup again
3. Click on "Next" until you reach the "Select Packages" screen
4. Type Devel , the find the "Devel" (development) entry (about fifth down),
and click on the text "Default" which should change to say "install"
Installing GCC
5. Click on the next button, and let Cygwin download and install
the development tools
6. This will take a while, have a cup of tea.
7. Once setup is finished, run Cygwin again, and type "g++ -v"
to confirm the GCC C++ compiler is installed. This should come
up with some version information text.
$ g++ -v
Reading specs from C:\cygnus\cygwin-b20\H-i586cygwin32\bin\..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
$cat open.c
#include<stdio.h>
int main()
{
FILE *ptr_file;
int x;
ptr_file =fopen("demo.txt", "w");
if (!ptr_file)
return 1;
for (x=1; x<=10; x++)
fprintf(ptr_file,"%d\n", x);
fclose(ptr_file);
return 0;
}
$ cat >demo.txt
1
2
3
4
5
6
7
8
$cat read.c
#include<stdio.h>
int main()
{
FILE *ptr_file;
char buf[1000];
ptr_file =fopen("demo.txt","r");
if (!ptr_file)
return 1;
while (fgets(buf,1000, ptr_file)!=NULL)
printf("%s",buf);
fclose(ptr_file);
return 0;
}
$ cat >demo.txt
choice cygwin
You now need appending c:\cygnus\cygwin-b20\H-i586cygwin32\bin; to the PATH environment variable. Make sure that
you do not lose the original value.
PATH = .. ; c:\cygnus\cygwin-b20\H-i586-cygwin32\bin;
The following are summary of commands and utilities related to the UNIX
system. See the corresponding man pages for detailed information.
awk
-search for and process patterns in a file,
cat
-display, or join, files
cd
-change working directory
chgrp -change the group that is associated with a file
chmod -change the access mode of a file
chown -change the owner of a file
comm -compare sorted files
cp
-copy files
df
-display the amount of available disk space
diff -display the differences between two files
du
-display information on disk usage
file -display file classification
find -find files
fsck -check and repair a file system
grep -search for a pattern in files
head -display the first few lines of a file
ln
-create a link to a file
lp
-print files (System V)
lpr
-print files (Berkeley)
ls
-list information about files
mkdir -create a directory
more -display a file one screen at a time (System V)
mv
-move and/or rename a file
od
-dump a file
pg
-display a file one screen at a time (Berkeley)
pr
-paginate a file
pwd
-print the working directory
rm
-remove (delete) files
rmdir -remove (delete) a directory
sed
-stream editor (non-interactive)
sort -sort and/or merge files
spell -check a file for spelling errors
tail -display the last few lines of a file
tar
-store or retrieve files from an archive file
umask -set file creation permissions
uniq -display the lines in a file that are unique
wc
-counts lines, words and characters in a file
whatis -list man page entries for a command
whereis -show where executable is located in path
which -locate an executable program using "path"
The following table lists about 40 of the more important system call:
General class
Specific class
File Structure
elated Calls
Creating a Channel
Input/Output
Random Access
Channel Duplication
Aliasing and Removing
Files
File Status
Access Control
Device Control
System call
fopen()
open()
close()
read()
write()
lseek()
dup()
link()
unlink()
stat()
fstat()
access()
chmod()
chown()
umask()
ioctl()
Process Related
alls
exec()
fork()
wait()
exit()
Process Owner and Group getuid()
geteuid()
getgid()
getegid()
Process Identity
getpid()
getppid()
Process Control
signal()
kill()
alarm()
Change Working Directory chdir()
Interprocess
Communication
Pipelines
Messages
Semaphores
Shared Memory
pipe()
msgget()
msgsnd()
msgrcv()
msgctl()
semget()
semop()
shmget()
shmat()
shmdt()