CHAPTER 1
UNIX FOR NONPROGRAMMERS
By Uğur Halıcı
Unix for non programmers
1
When you connected via terminal to a machine running Unix,
you will have a window to enter you commands
$
Enter your command
Terminal window
here
system prompt
MANUAL
2
The man command is used to display the manual entry
associated with word entered as argument.
The -k option is used to display a list of manual entries that
contain entered keyword.
man [chapter] word
man -k keyword
CREATING A FILE
3
Use editors vi, emacs, pico or the cat command
$ cat >myfile # $ is system prompt
CREATING A FILE
4
Use editors vi, emacs, pico or the cat command
$ cat >myfile # $ is system prompt
all characters that
follow up # to a
new line are
command output redirection comment
CREATING A FILE
5
Use editors vi, emacs, pico or the cat command
$ cat >myfile # $ is system prompt
Ali
Ahmet
Can
^D
$
^D is used to
indicate end of
input
CREATING A FILE
6
Use editors vi, emacs, pico or the cat command
$ cat >myfile # $ is system prompt
Ali
Ahmet
Can
^D
$
A file with name “myfile” is created
whose content is :
system prompt Ali
appears to enter a Ahmet
new command Can
CREATING A FILE
7
Use editors vi, emacs, pico or the cat command
$ cat >myfile # $ is system prompt
Ali
Ahmet
Can Current directory
^D home 122
$
halici
myfile
Ali
Ahmet
Can
LISTING THE CONTENTS OF A DIRECTOTY : ls
8
ls –adglR {filename}* {directoryname}*
note: * means zero or more and + means one or more
Options are:
a : list also hidden files, i.e. the filenames starting with .
d : directories
g : include info about file group
l : long listing
R: recursively list the contents of subdirectories
LISTING THE CONTENTS OF A DIRECTOTY : ls
9
$ ls
myfile
$ ls
myfile
$ ls –l myfile
$ ls –l myfile
r w - -rr-- -- rr - -
-rw -- April 15 11:41
1 1 halici 14 14 April 15 11:41 myfile
halici myfile
LISTING THE CONTENTS OF A DIRECTOTY : ls
10
$ ls
myfile
$ ls
myfile
$ ls –l myfile
$ ls –l myfile
r w - -rr-- -- rr - -
-rw -- April 15 11:41
1 1 halici 14 14 April 15 11:41 myfile
halici myfile
file name
# of links length date
file type and owner time
permissions
file type and permissions
11
- rw- r-- r--
file permissions permissions permissions
type for for for
owner group others
LISTING A FILE: cat/more/page/head/tail
12
cat : concatanate
more, page : to display in parts without scroll
head: first n lines, for default n=10
tail : last n lines, for default n=10
$ cat myfile
Ali
Ahmet
Can
$ head -2 myfile
Ali
Ahmet
$ tail -2 myfile
Ahmet
Can
$
RENAMING A FILE : mv
13
mv –i oldFile newFile
mv –i {file name}* directoryName
mv –i oldDirectory newDirectory
The mv command in the first form renames oldFile as newFile.
The second form moves collection files to a directory.
The last form is used to move the files in oldDirectoty to newDirectory.
The option -i prompts confirmation if newFileName already exists
RENAMING A FILE : mv
14
$ mv myfile myNewFile Current directory
home 122
$ ls
myNewFile
$ cat myNewFile halici
Ali
Ahmet
Can myfile myNewfile
$
MAKING A DIRECTORY: mkdir
15
mkdir newDirectoryName
$ mkdir class
$ ls –l
-rw-r--r-- 1 halici 14 April 15 11:41 myNewFile Current
drwxr-xr-x 2 halici 512 April 15 11:50 class/ directory
halici
myNewfile class
MAKING A DIRECTORY: mkdir
16
mkdir newDirectoryName
$ mkdir class
$ ls –l
-rw-r--r-- 1 halici 14 April 15 11:41 myNewFile
Current
drwxr-xr-x 2 halici 512 April 15 11:50 class/
directory
$ mv myNewFile class
$ ls
class
$ ls class halici
myNewFile
$ ls –R
class myNewfile class
class:
myNewFile
myNewfile
MOVING TO A DIRECTORY: cd, chdir
17
mkdir newDirectoryName
$ pwd # print working directory
Current
/home122/halici
directory
halici
class
myNewfile
MOVING TO A DIRECTORY: cd, chdir
18
mkdir newDirectoryName
$ pwd # print working directory
Current
/home122/halici
directory
$ cd class
$ pwd
/home122/halici/class
$ halici
class
myNewfile
COPYING A FILE : cp
19
cp –i oldFileName newFileName
cp –ir {file name}* directoryName
options: Current
i: confirm directory
r: recursively copy subdirectories
halici
$ cp myNewFile mySecondFile
$ ls
myNewFile
mySecondFile class
$
myNewfile mySecondfile
DELETING A DIRECTORY: rmdir
20
Current
directory
$ pwd
/home122/halici/class
halici
class
myNewfile mySecondfile
DELETING A DIRECTORY: rmdir
21
Current
directory
$ pwd
/ home122/halici/class
$ cd .. # change to parent directory
halici
$ pwd
/ home122/halici
$ ls
class class
$ rmdir class
rmdir: class: directory not empty
$
myNewfile mySecondfile
DELETING A DIRECTORY: rmdir
22
Current
directory
$ pwd
/ home122/halici/class
$ cd .. # change to parent directory
halici
$ pwd
/ home122/halici
$ ls
class class
$ rmdir class
rmdir: class: directory not empty
$
myNewfile mySecondfile
An error message by the system is displayed.
The directory is not deleted since it is not empty
DELETING A FILE : rm
23
rm –fir {filename}*
Current
f: inhibit error messages
directory
i: inform each time
r: recursivey (if filename is a directory)
halici
$ ls
class
$ ls class
myNewFile class
mySecondFile
myNewfile mySecondfile
DELETING A FILE : rm
24
rm –fir {filename}*
Current
f: inhibit error messages
directory
i: inform each time
r: recursivey (if filename is a directory)
halici
$ ls
class
$ ls class
myNewFile class
mySecondFile
$rm class/* #remove all files in directory class
$ls class
myNewfile mySecondfile
DELETING A FILE : rm
25
rm –fir {filename}*
Current
f: inhibit error messages
directory
i: inform each time
r: recursivey (if filename is a directory)
halici
$ ls
class
$ ls class
myNewFile class
mySecondFile
$rm class/* #remove all files in directory class
$ls class
$
All the files under the directory class are deleted,
nothing remains to list by ls
PRINTING A FILE : lpr
26
$ cat >myclass
Ali
Amet
Can
^D
$ ls
myclass
$ cat myclass
Ali
Amet
Can
$ lpr myclass # send the content of the file class to printer
COUTING WORDS IN FILE: wc
27
wc -lwc {filename}*
options: myclass
l: lines,
w:words, Ali
Ahmet
Can
$ wc –w myclass
3
$ wc –c myclass
14
$ wc myclass
COUTING WORDS IN FILE: wc
28
wc -lwc {filename}*
options: myclass
l: lines,
w:words, Ali
Ahmet
Can
$ wc –w myclass
3
$ wc –c myclass
no option is used, this is equivalent to
14
–lwc all together
$ wc myclass
COUTING WORDS IN FILE: wc
29
wc -lwc {filename}*
options: myclass
l: lines,
w:words, Ali
Ahmet
Can
$ wc –w myclass
3
$ wc –c myclass
no option is used, this is equivalent to
14
–lwc all together
$ wc myclass
3 3 14
$
COUTING WORDS IN FILE: wc
30
wc -lwc {filename}*
options: myclass
l: lines,
w:words, Ali
Ahmet
Can
$ wc –w myclass
3
$ wc –c myclass
no option is used, this is equivalent to
14
–lwc all together
$ wc myclass
3 3 14
$
l c
w
FILE TYPES
31
- regular file
d directory file
b buffered special file (such as disk drive)
c unbuffered special file (such as disk terminal)
l symbolic link
p pipe
s socket
FILE PERMISSIONS
32
rw– r-- r--
user group others
FILE PERMISSIONS
33
regular file directory special file
The process may The process can read The process may read
r read the contents the directory (i.e. list from the file using the
read the names of the files read( ) system call
that it contains)
The process may The process may add The process may write
w change the contents or remove files to the file using the
write to/from the directory write( ) system call
The process may The process may No meaning
x execute the file access files in the
execute (which only makes directory or any of its
sense if it is a subdirectories
program)
CHANGING FILE’S PERMISSIONS: chmod
34
chmod –R change{,change}* filename+
R: recursively change modes if filename is a directory
Change:
cluster selection operation new permission
u (user) + (add) r (read)
g (group) - (remove) w (write)
o (others) = (assign) x (execute)
a (all)
CHANGING FILE’S PERMISSIONS: chmod
35
Examples for change{,change}*
g+w add group write permission
u-wx remove user write and execute permissions
o+x add others execute permission
u+w,g-r add write permission for user and
remove read permission from group
g=r give group just read permission
CHANGING FILE’S PERMISSIONS: chmod
36
$ ls –l myclass
-rw-r--r-- 1 halici 14 April 15 12:05 myclass
$ chmod o-r myclass # remove read permission from others
-rw-r----- 1 halici 14 April 15 12:05 myclass
CHANGING FILE’S PERMISSIONS: chmod
37
The chmod utility allows you to specify the new permission
setting of a file as an octal number
user group others
rwx rwx rwx
setting rwx r-x ---
binary 111 101 000
octal 7 5 0
CHANGING FILE’S PERMISSIONS: chmod
38
$ chmod 750 myclass
$ ls –l myclass
-rwxr-x--- 1 halici 14 April 15 12:05 myclass
$
CHANGING FILE’S PERMISSIONS: chmod
39
$ chmod 750 myclass
$ ls –l myclass
-rwxr-x--- 1 halici 14 April 15 12:05 myclass
$
Permission is set as desired
CHANGING FILE’S PERMISSIONS: chmod
40
$cat >a
aaa
^D
$ chmod u-w a # remove write permission from user
$ ls –l a #see that it is removed
-r--r--r-- 1 halici 4 April 15 12:10 a
$ rm a #delete the file a
$ ls
CHANGING FILE’S PERMISSIONS: chmod
41
$cat >a
aaa
^D
$ chmod u-w a # remove write permission from user
$ ls –l a #see that it is removed
-r--r--r-- 1 halici 4 April 15 12:10 a
$ rm a #delete the file a
$ ls
$
The file is removed ! Deleting a file depends on not on the file’s
write permission but the write permission of the directory that
contains it (ie udating the content of the directory)
GROUPS
42
Suppose that I am a member of the group “ee”
$ ls –lg myfile
GROUPS
43
Suppose that I am a member of the group “ee”
option g stands for listing
also file’s group
$ ls –lg myfile
GROUPS
44
Suppose that I am a member of the group “ee”
Group information
$ ls –lg myfile
-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile
GROUPS
45
Suppose that I am a member of the group “ee”
Group information
$ ls –lg myfile
-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile
$ groups #list my group
ee
GROUPS
46
Suppose that I am a member of the group “ee”
Group information
$ ls –lg myfile
-rw-r--r-- 1 halici 14 ee April 15 12:20 myfile
$ groups #list my group
ee
If I want to be added to a new group, say named “cls”,
I should request the system administrator to do it.
CHANGING FILE’S GROUP : chgrp
47
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