http://studyhacktivism.blogspot.
com 1
UNIX/LINUX
SHELL PROGRAMMING - 1
--by Nataraja Rao
http://studyhacktivism.blogspot.com
Objectives
2
Unix file system
Creating files in UNIX
File manipulation
File type meanings & permission types
Links and inode numbers
File permissions
Masking file permissions
Some useful commands
http://studyhacktivism.blogspot.com
Unix File System
3
Unix treats everything it knows and understands, as
a file.
All utilities, applications, data in Unix is stored as
files.
Even a directory is treated as a file which contains
several other files.
http://studyhacktivism.blogspot.com
Unix File System (contd…)
4
The file system begins with a directory called root.
The root directory is denoted as “forward slash”( / ).
Branching from root there are several other
directories called bin, lib, usr, etc, tmp, and etc..
These directories are called sub-directories, their
parent being the root ( / ) directory.
Each of these sub-directories contain several files
and directories called sub-sub-directories.
http://studyhacktivism.blogspot.com
5
http://studyhacktivism.blogspot.com
Creating Files
6
$ touch file1
This creates a file called ‘file1’.
The size of the file would be zero bytes since touch
doesn’t allow you to store anything in a file.
When we want to create several empty files quickly.
$ touch file1 file2 file3 file4
If we want to store a few lines in a file ?
$ cat > file1 then press enter and type data whatever
you wants to write in that “file1”.
http://studyhacktivism.blogspot.com
Creating Files (contd…)
7
To save & close that ‘file1’ press enter and then press
Ctrl d.
In Unix the keys Ctrl d indicate the EOF or end of file
character.
To see the contents of the created ‘file1’
$ cat file1
cat command can concatenate the contents of two files
and store them in third file.
$ cat file1 file2 > file3
To append data into a file, we will replace ‘>’ with
‘>>’.
http://studyhacktivism.blogspot.com
File Manipulation
8
$ cp file1 file2
This will copy the contents of ‘file1’ into a file ‘file2’.
If ‘file2’ does not exist, it will be created.
$ cp file1 file2 folder
This will copy all files mentioned are copied to
‘folder’ named directory. Provided that the
directory ‘folder’ exists.
http://studyhacktivism.blogspot.com
File Manipulation (contd…)
9
In Unix you can copy files from or to different
directories by specifying their name along with the
path.
$ cp /tmp/folder1/file1 /tmp/folder2/file2
rm removes the given file or folder.
$ rm –fv file -r recursive
$ rm –rfv folder -f forcefully
-v verbose
http://studyhacktivism.blogspot.com
File Manipulation (contd…)
10
mv move or rename from one file to another.
$ mv file1 file2
Above command will cut file1 and then paste it as
file2
$ mv file1 file2 folder1
Above command will cut “file1” and “file2” named
files, then paste them into “folder1” named folder.
http://studyhacktivism.blogspot.com
Listing Files and Directories
11
ls to see the list of files, like windows dir command
Windows dir command also works in Linux.
$ ls
$ dir
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
12
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
13
Any filename which begins with a ‘.’ is treated as a
hidden file.
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
14
$ ls p*
The above command will list the files, which is starts
with letter “p”.
$ ls ?ile
When the shell comes across a ‘?’. It understands
that the symbol signifies any single character.
The * is interpreted by the shell as presence of any
number of characters.
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
15
$ ls [aeiou]*
This indicates that the first character of the filename
to be listed must be any one of the letters given
within the square brackets, and the remaining can
be anything.
$ ls [!aeiou]*
The ! Symbol will exclude specified characters
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
16
$ ls [a-m][c-z][4-9]??
This will list all 5 character filenames in the current
directory whose first character is in the range a to
m, the second character is in the range c to z, the
third character is in the range 4 to 9, whereas the
fourth and fifth are any valid characters.
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
17
$ ls –l (long listing)
Unix treats all entities – files, directories, devices as
files.
To differentiate between all of them it uses file
types.
http://studyhacktivism.blogspot.com
Listing Files and Directories (contd…)
18
http://studyhacktivism.blogspot.com
File type meanings & Permission types
19
File Type Meaning
- Ordinary file
d Directory file
c Character special file
b Block special file
l Symbolic link
s Semaphore
p Named pipe
m Shared memory file
Permission type Permission meaning Permission weight
r read 4
w write 2
x execute 1
http://studyhacktivism.blogspot.com
Links and Inode numbers
20
When a file has two links, it is not physically present
at two places, but can be referred to by either of
the names.
Unix will identify files using Inode numbers, these
numbers are unique.
$ ln file1 file2
The above command will create link for ‘file1’, with
the name of ‘file2’ , but the Inode numbers are
same for the above two files.
http://studyhacktivism.blogspot.com
Links and Inode numbers (contd…)
21
http://studyhacktivism.blogspot.com
File Permissions
22
The existing file permissions can be changed by owner
of the file or by the superuser(root).
The way to change these permissions is by using the
chmod command.
$ chmod 755 file1
First digit is Owner permissions
7 = read(4) + write(2) + execute(1)
Second digit is Group permissions
5 = read(4) + execute(1)
Third digit is Other users permissions
5 = read(4) + execute(1)
http://studyhacktivism.blogspot.com
File Permissions (contd…)
23
The above way of changing file permissions is
referred to as the ‘absolute mode’.
There is another syntax for chmod that changes
permissions, which constitutes the ‘symbolic mode’.
Syntax:
$ chmod [who] [+/-/=] [permissions] file
The who here refers to whom the permissions are to
be assigned. It may be the user or owner(u), the
group(g) or others(o). If none is specified, all are
assumed.
http://studyhacktivism.blogspot.com
File Permissions (contd…)
24
+ means add permissions
- means remove permissions
= means instructs chmod to add the specified
permission and take away all other, if present.
Ex: $ chmod +w file1
Add write permissions for all(owner, group, others)
Ex: $ chmod go-x file2
Remove executable permissions to group and others.
Ex: $ chmod go+r,go-w file3
Add read permissions to group and others, remove
write permissions to group and others.
http://studyhacktivism.blogspot.com
File Permissions (contd…)
25
Ex: $ chmod go=r,u=rw file1
This removes all existing permissions and replaces
them with read permission for group and others and
read & write permission for owner of the file file1.
Ex: $ chmod ugo=rwx file2
This removes all existing permissions and replaces
them with full permissions for owner, group and others.
http://studyhacktivism.blogspot.com
Masking File Permissions
26
How come that the file permissions for file1 file have
been set to 644 ?
How come that the folder permissions for folder1 have
been set to 755?
What unix does is it uses the value stored in a variable
called umask to decide the default permissions.
http://studyhacktivism.blogspot.com
Masking File Permissions (contd…)
27
Umask stands for user file creation mask, the term
mask implying which permissions to mask or hide.
The umask value tells unix which of the three
permissions are to be denied rather than granted.
The current value of umask can be easily
determined by just typing umask.
http://studyhacktivism.blogspot.com
Masking File Permissions (contd…)
28
Whenever a file is created Unix assumes that the
permissions for this file should be 666.
But since our umask value is 022, Unix subtracts this
value from the default system wide permissions (666)
resulting in a value 644.
This value is then used as the permissions for the file
that you create.
Similarly system-wide default permissions for a
directory are 777.
This means that when we create a directory its
permissions would be 777 – 022, i.e. 755.
http://studyhacktivism.blogspot.com
Masking File Permissions (contd…)
29
If a directory doesn’t have an execute permission
we can never enter into it.
We can change current umask value using umask
command
$ umask 242
http://studyhacktivism.blogspot.com
Directory Related Commands
30
“pwd” stands for ‘present working directory’
“mkdir” to create an empty directory.
“mkdir –p”, allows you to create multiple
generations of directories, at one go.
That means, it creates all the parent directories
specified in the given path too.
Ex: $ mkdir –p study/hacktivism/books
http://studyhacktivism.blogspot.com
Useful commands
31
Unix knows each user not only by the login name, but
also by two numbers called the user and group identity
numbers.
“id” to show identity numbers
“uname” to find the name of the Unix system.
“tty” to find out the name of your terminal file
“whoami” to find some information about yourself.
“who” to see who all are currently sharing the network
with you.
“date” to display the current date and time.
http://studyhacktivism.blogspot.com
Useful commands (contd…)
32
http://studyhacktivism.blogspot.com
Reference books
33
Unix Shell Programming
--by Yeshavant P. Kanetkar
http://studyhacktivism.blogspot.com
Questions ?
34