Sei sulla pagina 1di 81

UNIX for nonprogrammers

Cygwin tools (http://cygwin.com/) are ports of the popular GNU


development tools and utilities for Windows 95, 98, and NT. They
function by using the Cygwin library which provides a UNIX-like API on
top of the Win32 API.
Cygwin allows the compilation of the X Window System source on
Win32; several patches were made to the X Window System source to
make it actually compile and run under Cygwin.

Cygwin terminal window:


When you connected via terminal to a machine running Unix,
you will have a window to enter you commands
system prompt
$

Terminal window

enter your BASH shell command here

How to run UNIX commands in DOS:


If you want to use all UNIX and Linux commands in your command
prompt of windows (DOS) then you have to add Cygwin to your
Windows Environment path variable.
Steps to Add Cygwin Path to Windows Environment Variable:
1) Right click on the Computer icon on your Desktop and
choose Properties option.

2) In the System window click on Advanced system settings in the left


pane as highlighted below

In the bottom of the dialog box click on Environment Variable.


3) In the System Properties window select Advanced tab and click on
Environment Variables button given at the bottom of the window as
highlighted below

4) In the Environment Variables window you will notice two


columns User variables for a username and System variables.

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

enter Unix/Linux command here

c:\Windows\system32> cd c:\cygwin64\home\jchein1
c:\cygwin64\home\jchein1>
From now on we use $ as system prompt

DOS window

Read manual page: man


The man command displays the online manual pages.
The syntax for the man command is:
man [options] [ [section] subjects]
Options
Description
Output is piped through the more command.
-d
Debug.
-F
Search the MANPATH directories.
-f files
Display a summary (one line) for each file.
-k keywords Displays the header lines that contain any of the
keywords.
-M path
Searchs in the path instead of the MANPATH
directories.
-t
Format the pages with troff.
-T mac
Display using mac.

Read manual page: man


The manual is generally split into 8 sections, organized as

follows (on Research Unix, BSD, OS X and Linux):


Section Description
1 Executable programs or commands
2 System calls ( functions provided by the kernel )
3 Library calls ( functions provided by the library )
4 Special files
5 File formats and conventions ( configuration files )
6 Games
7 Miscellaneous
8 System administration commands

$ man http
HTTP::Tiny(3pm)

Perl Programmers Reference Guide

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)

Manual pager utils

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)

GNU Awk Extens

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

Creatng a fle: cat


Use editors vi, emacs, pico or cat command to do editing
$ cat > hello.c

# $ is system prompt

command output redirection

user comment

Creatng a fle: cat


$ cat >hello.c
#include <stdio.h>
int main () {
printf ("Hello world!\n");
return 0;
}
^D
$ ls hello.c
hello.c

hello.c is created

$ cat hello.c
#include <stdio.h>
int main () {
printf ("Hello world!\n");
return 0;
}
$

press control d to indicate end of input

display hello.c

system prompt appears to enter a new command

Creatng a fle: cat


$ cat >demo
111
222
333

#press control d to indicate end of input

^D
C:\cygwin64\
home\

current directory

jchein1\
demo
$ cat demo2 >> demo5 # append demo onto the end of demo5

Lstng the contents of a drectoty:


ls

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

list hidden files (i.e. the filenames starting with .)

-d

list the name of the current directory

-F

show directories with a trailing '/'


executable files with a trailing '*'

-g

show group ownership of file in long listing

-i

print the inode number of each file

-l

long listing giving details about files


and directories

-R

list all subdirectories encountered

-t

sort by time modified instead of name

Lstng the contents of a drectoty:


ls
$ ls ~
class demo demo3 demo4 hello.c hello1.cpp hello1.exe hello2.c hello2.exe
$ ls /
# List the contents of your home directory
Cygwin-Terminal.ico Cygwin.bat Cygwin.ico bin cygdrive dev etc home lib proc tmp usr var
$ ls ../
jchein1

# List the contents of the parent directory

$ ls */

# List the contents of all subdirectories

$ 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

Lstng the contents of a drectoty


: ls
$ ls
class hello.c hello1.cpp hello1.exe hello2.c
this is a sub-dir
$ ls hello.c
hello.c
$ ls -l hello.c
-rwxr-xr-x
1

group
jchein1 None 91 Oct 23 20:15 hello.c
# of links

length

file

name
date
file type and permissions

owner

time

$ ls -d .*/

# get the list of hidden directory

../ ./

$ ls -f .*/

# get the list of all files

../:
. .. 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?

Editing text files with Vi (Unix text


editor)
Vi is a text editor included with most Linux systems. If you have to edit a text
file that doesnt include a user friendly text editor, so knowing Vi is essential.
Vi doesnt provide a list of keyboard shortcuts on the screen. Its a modal text
editor, and it has both an insert and command mode.
Command Mode

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.

Vi wont let you quit if youve modified the file since


you last saved, but you can type :q! and press
enter to ignore this warning.
Most Linux distributions come with Nano (an easierto-use terminal text edito) installed, but embedded
systems and other stripped-down environments
often only include vi.

Find
Lists all of the files within a directory and its subdirectories that

match a set of conditions. This command is most commonly used to


find all of the files that have a certain name.
To find all of the files named myfile.txt in your current directory and
all of its subdirectories, enter:
$ find . -name demo2 -print
./demo2
To look in your current directory and its subdirectories for all of the
files that end in the extension .txt, enter:
$ find . -name "*.c" -print
./hello.c
./hello2.c

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

looks for files called 444 throughout your home directory

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

# display line numbers

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

# - compares file1 to file2

# compares two files ignoring letter case and spaces

c:\cygwin64\home\jchein1>diff -iw hello.c hello1.cpp


1c1,4
< #include <stdio.h>
-->
>
> #include <iostream>
>
3c6
<
printf ("Hello world!\n");
--> std::cout << "Hello World!" << std::endl;
5a9
>

Sort
$ cat >demo6
33
22
11
$ sort demo6
11
22
33
$ sort -o sorted demo6 # sorts the file demo6 and writes
output to sorted

File type and permissions


Use ls -l to see the permissions of files (list-long). They will appear like
this, note that I have added spaces between permissions to make it easier
to read: where
r = read
4
w = write
2
x = execute 1
Value Permission
Directory Listing
0 No read, no write, no execute --1 No read, no write, execute
--x
2 No read, write, no execute
-w3 No read, write, execute
-wx (1+2)
4 Read, no write, no execute
r-5 Read, no write, execute
r-x
(4+1)
6 Read, write, no execute
rw(4+2)
7 Read, write, execute
rwx
(4+2+1)

File type and


permissions
These are the examples from the symbolic notation section
given in octal notation:
Symbolic
Description
Octal Notation
---------no permissions
0000
---x--x--x execute
0111
--w--w--w- write
0222
--wx-wx-wx write & execute
0333
-r--r--r-- read
0444
-r-xr-xr-x read & execute
0555
-rw-rw-rw- read & write
0666
-rwxrwxrwx read, write, & execute
0777

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
$

$ cat demo demo > demo2


$ cat demo2
11
22
33
11
22
33
$ cat demo > demo3
$ cat demo3
11
22
33
$

# concatanate

# copy demo3 from demo

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
$

Making a new directory: mkdir


The syntax for the mkdir command is:
mkdir [options] directories
Options Option Description
-m mode Sets the access mode for the new directory.
-p
If the parent directories don't exist, this command
creates them.
$ mkdir 444 class2
$ ls
444 class class2 demo2 demo3 demo4 hello.c hello1.cpp hello1.exe
hello2.c hello2.exe
# two new directories created
$ ls l class2
total 0
$

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

Movng to a drectory: chdir


fchdir is identical to chdir, only that the directory is given as an open file
descriptor.
chdir()
Change the current working directory to that specified in path.
Synopsis:
#include <unistd.h>
int chdir( const char *path );
Arguments:
path is the new current working directory.
Return value
On success, zero is returned. On error, -1 is returned, and errno is set
appropriately.
Errors
Depending on the file system

Deletng a drectory: rmdir


$ pwd
/home/jchein1
$ rmdir class
$ rmdir class2
The directory is not empty.
# The directory is not deleted since it is not empty
$ cd class2
$ ls
child_class2
$ del child_class2
c:\cygwin64\home\jchein1\class2\child_class2\*, Are you sure (Y/N)? Y
$ cd ..
# goback to parent directory
$ pwd
/home/jchein1
$ rmdir class2
The directory is not empty.
$ rmdir child_class2
$ rmdir class2
$ cd ..
class
$ rmdir class
$ ls
444 class3 demo2 demo3 demo4 demo5 demo6 hello.c hello1.cpp hello1.exe

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
$

Prntng a fle : lpr


$ cat >demo6
11
22
33
^D
$ cat demo6
11
22
33
$ lpr demo6
# send file demo6 to default printer
'lpr' is not recognized as an internal or external command,
operable program or batch file.
#LPR.EXE is not available by default in Windows Server 2008 or Windows
Server 2008 R2

$ lpr a
-bash: lpr: command not found

Coutng words n fle: wc (word count)


The wc command is used to find out number of newline count,
word count, byte and characters count in a files specified by
the file arguments.
wc [options] filenames
wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.

Coutng words n fle: wc (word count)


$ cat demo3
11
22
33
$ wc -l demo3
3 demo3
$ wc -w demo3
3 demo3
$ wc -c demo3
9 demo3
$ wc -m demo3
9 demo3
$ wc -L demo3
2 demo3
$ wc demo3
3 3 9 demo3

# this is equivalent to wc lwc demo3

Hierarchical File Structure


A family tree is an example of a hierarchical structure that represents how the
UNIX file system is organized.

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

- archive, library file

program.c - C language source file


alpha2.f
xwd2ps.o

- Fortran source file


- Object/executable code

mygames.Z - Compressed file


Hidden Files: have names that begin with a dot (.) i.g, .cshrc .login .mailrc .mwmrc
Uniqueness: as children in a family, no two files with the same parent directory can have the same name.
Files located in separate directories can have identical names.
Reserved Filenames:
/
.
..
~

- the root directory (slash)


- current directory (period)
- parent directory (double period)
- your home directory (tilde)

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

How do create a symbolic link in


Unix ?
A symbolic link is a pointer to another file or directory. A symbolic link
appears in a long listing (ls -l) with a reference to the original
file/directory. A symbolic link is required when linking from one
filesystem to another and can be used within a filesystem as well.
To create a symbolic link, the syntax of the command is
similar to a copy or move command: existing file first,
destination file second.
$ ln -s /usr/local/apache/logs ./logs
This points a symbolic link "./logs" to "/usr/local/apache/logs"
A directory is marked with a d as the first letter in the
mode field in the output of ls -dl or stat, e.g.
$ ls -dl /
drwxr-xr-x+ 1 jchein1 None 0 Sep 25 16:05 /

$ 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

Changng fles permssons:


chmod
chmod R change{,change}* filename+

R: recursively change modes if filename is a directory


Change:
cluster

operation

new
permission

u (user)

+ (add)

r (read)

g (group)

- (remove)

w (write)

o (others)

= (assign)

x (execute)

selection

a (all)

Changng fles permssons:


chmod
The chmod command is used to change access permissions for files
which you own. The syntax is:
chmod

permission_triads

filename

[who] [action] [permissions]


where:
who

action

u = user

+ = add

r = read

g = group

- = remove

w = write

o = other
a = all

permissions

x = execute

Changng fles permssons:


chmod
You can change a file's permissions using the chmod command. See man 1 chmod
for more information. Here are some examples:
chmod g+w add group write permission
chmod u-wx remove user write and execute permissions
chmod o+w file adds write permission for everyone else (o = others)
chmod u+w,g-r add write permission for user and remove read permission from
group
chmod g=r give group just read permission
chmod o-r remove read permission from others
chmod g+r file adds read permission for members of the file's group
chmod o-r file removes read permission for everyone else
chmod u-x file removes execute permission for the owner(u = user/owner)

Changng fles permssons:


chmod
The chmod utility allows you to specify the new permission
setting of a file as an octal number. For example:
user

group

others

rwx

rwx

rwx

setting

rwx

r-x

---

binary

111

101

000

octal

Changng fles permssons:


chmod
$ chmod 750 demo3
$ ls l demo3
-rwxr-x--- 1 jchein1 None 9 Oct 23 22:33 demo3 $
$cat >a
# Permission is set as desired
aaa
^D
$ chmod u-w a
# remove write permission from user
$ ls -l a
# check if it is removed ?
-r-xr-xr-x 1 jchein1 None 3 Oct 24 19:34 a
$ rm a
# delete the file a
rm: remove write-protected regular file 'a'? Y
$ ls
444 class3 demo2 demo3 demo4 demo5 hello.c hello1.cpp
$
# file a is removed. Deleting a file depends
on not on the files write permission but the write
permission of the directory that contains it (ie udating the
content of the directory)

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

# count total number of groups

GROUPS
Recommend alternative to groups command
The groups command has been obsoleted by the id

command. You need to use the following command which


is equivalent to id -Gn:
$ id -Gn
None Users
$ id | wc w
3

# count total number of groups

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.

Changng fles group : chgrp


chgrp R groupId {filename}*
R: recursively changes the group of the files in a
directory
$ ls lg myfile
-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile
$ chgrp cls myfile
$ ls lg myfile
-rw-r--r-- 1 halici 14 cls April 15 12:20 myfile

Changng fles group :


chgrp
To change a file's group using the chgrp command. For example, to
change the group of the demo3 file from None Users
to joseph, use
chmod joseph demo3
An ls -lg should then show this:
-rwxrw---- 1 kmsalem cs350 1155 Sep 27 1993 writer
Notice that the permissions have not changed. Prior to the chgrp
command, users who are part of the prof group would have had read and
write access to the writer file. After the change, users who are part of the
cs350 group will have read and write access to the file.

$ 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

# see a list of the users currently logged on to the system

$ 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

Compile and run C programs


$ gcc hello2.c -o hello2.exe

-bash: gcc: command not found


# if this error occurred
You need install gcc by running setupx86_64.exe again. The gcc package is in the
Devel category:

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)

Compile and run C programs


$ cat hello2.c
#include <stdio.h>
int main () {
printf ("Hello world!\n");
return 0;
}
$ gcc hello2.c -o hello2.exe
$ hello2
Hello world!

$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

Compile and run C programs


$ gcc open.c -o open.exe
$ open
$ gcc read.c -o read.exe
$ read
1
2
3
4
5
6
7
8
9
10

Inatall C++ compiler


If you get "make: g++: Command not found error message
means that C++ is not Installed In Cygwin. Please download full.exe
to C:\full.exe and double-clicking on it. This will install g++ on your hard drive.

choice cygwin

c++ installation was created in the following path:


c:\cygnus\cygwin-b20\H-i586-cygwin32\bin

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;

Compile and run C++ programs


$ cat hello1.cpp
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
$ g++ -g hello1.cpp -o hello1 -lm
$ hello1
Hello World!
$

Compile and run C++ programs


$ cat >write.cpp
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int filedesc = open("test.txt", O_WRONLY | O_APPEND);
if (filedesc < 0) { return -1; }
if (write(filedesc, "This will be output to test.txt\n", 36) != 36) {
write(2, "There was an error writing to test.txt\n", 43);
return -1;
}
return 0;
}
^D
$ g++ -g write.cpp -o write lm
$write
$cat test.txt
Hello world! This is output!

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

Process Creation and


Termination

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()

Potrebbero piacerti anche